<template>
  <div class="table-box">
    <ProTable :config="config" ref="ProTableRef" :api="getReducePage">
      <template #left_buttons>
        <!-- <el-button type="primary">下载申请 </el-button> -->
        <el-button
          type="primary"
          @click="changeStatus"
          :disabled="!selectdList || selectdList.length > 1"
          >审批
        </el-button>
        <!-- <el-button type="primary">下载 </el-button>
        <el-button type="primary">批量修复上传 </el-button> -->
      </template>
    </ProTable>
    <vxe-modal
      v-model="showModal"
      title="审批提示"
      height="282"
      width="450"
      show-footer
      esc-closable
    >
      <div class="w-full px-3 h-full overflow-auto flex-col flex mytable">
        是否审批通过选中的数据？
      </div>
      <template #footer>
        <el-button type="default" @click="showModal = false">取消</el-button>
        <el-button type="danger" @click="submitForm('fail')">不通过</el-button>
        <el-button type="primary" @click="submitForm('pass')">通过</el-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script setup name="systemLog" lang="jsx">
  import { computed } from 'vue';
  import { reactive, ref } from 'vue';
  import { getReducePage, flowStatusByIds } from '@/api/property';
  import { onMounted } from 'vue';
  import { ElMessageBox, ElMessage, ElButton } from 'element-plus';

  const ProTableRef = ref();
  const showModal = ref(false);
  const selectdList = ref([]);
  const onCheckboxChange = (row) => {
    console.log('row', row);
    selectdList.value = row.records;
  };
  const reduceTypeOpt = [
    { label: '结清减免', value: 'settle' },
    { label: '分期减免', value: 'by_stages' },
  ];
  const flowStatusOpt = [
    { label: '待审核', value: 'pending' },
    { label: '通过', value: 'pass' },
    { label: '未通过', value: 'fail' },
  ];
  const changeStatus = async () => {
    showModal.value = true;
  };
  const submitForm = async (type) => {
    const ids = selectdList.value.map((v) => v.id).join(',');
    await flowStatusByIds({
      ids: ids,
      flowStatus: type,
    });

    ElMessage.success({
      message: '审核成功',
      plain: true,
    });
    showModal.value = false;

    query();
  };
  const config = computed(() => {
    return {
      columns: [
        { type: 'checkbox', width: 50 },
        {
          field: 'caseId',
          title: '案件ID',
          search: { el: 'input' },
          width: 80,
        },
        {
          field: 'borrower.name',
          title: '借款人名字',
          search: { el: 'input', key: 'borrower' },
          width: 90,
        },
        {
          field: 'reduceAmount',
          title: '减免金额',
          search: { el: 'input' },
          width: 80,
        },
        {
          field: 'totalReduceAmount',
          title: '减免金额(总计)',
          search: { el: 'input' },
          width: 120,
        },
        {
          field: 'totalNumber',
          title: '减免案件数',
          search: { el: 'input' },
          width: 120,
        },
        {
          field: 'remainingAmount',
          title: '剩余待还金额',
          search: { el: 'input' },
          width: 120,
        },
        {
          field: 'totalPayAmount',
          title: '累计还款金额',
          search: { el: 'input' },
          width: 120,
        },
        {
          field: 'reduceType',
          title: '减免类型',
          width: 80,
          enum: reduceTypeOpt,
          search: { el: 'select', props: { filterable: true }, span: 1 },
          fieldNames: { label: 'label', value: 'value' },
          slots: {
            default: ({ row }) => {
              return (
                <>
                  {row.reduceType
                    ? reduceTypeOpt.find((v) => v.value === row.reduceType).label
                    : ''}
                </>
              );
            },
          },
        },
        {
          field: 'flowStatus',
          title: '审核状态',
          width: 80,
          enum: flowStatusOpt,
          search: { el: 'select', props: { filterable: true }, span: 1 },
          fieldNames: { label: 'label', value: 'value' },
          slots: {
            default: ({ row }) => {
              return (
                <>
                  {row.flowStatus
                    ? flowStatusOpt.find((v) => v.value === row.flowStatus).label
                    : ''}
                </>
              );
            },
          },
        },
        {
          field: 'id',
          title: '减免申请ID',
          search: { el: 'input' },
          width: 100,
        },
        {
          field: 'applyDate',
          title: '减免申请时间',
          search: {
            el: 'date-picker',
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
        },
        {
          field: 'inEffectDate',
          title: '减免生效时间',
          search: {
            el: 'date-picker',
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
        },
      ],
      onCheckboxChange: onCheckboxChange,
    };
  });
  const query = () => ProTableRef.value?.search();

  onMounted(() => {
    query();
  });
</script>
