<template>
  <div class="table-box">
    <ProTable :config="config" ref="ProTableRef" :api="getRepayRecordPage" :selectdList="selectdList">
      <template #left_buttons>
        <!-- <el-button type="primary">下载申请 </el-button> -->
        <el-button
          type="primary"
          @click="changeStatus"
          :disabled="!selectdList || !selectdList.length"
          >审批
        </el-button>
        <!-- <el-button type="primary">下载 </el-button>
        <el-button type="primary">批量修复上传 </el-button> -->
      </template>
    </ProTable>
    <vxe-modal
      resize
      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="repaymentApprove" lang="jsx">
  import { computed } from 'vue';
  import { reactive, ref } from 'vue';
  import { getRepayRecordPage, repayRecordFlowStatusByIds } from '@/api/property';
  import { onMounted } from 'vue';
  import { ElMessageBox, ElMessage, ElButton } from 'element-plus';
  import { useDict } from '@/hooks/useDict';
  import { getTenantPage } from '@/api/tenant';
  const { RepayType, FlowStatus } = useDict("RepayType", "FlowStatus");
  const ProTableRef = ref();
  const showModal = ref(false);
  const selectdList = ref([]);
  const onCheckboxChange = (row) => {
    console.log('row', row);
    selectdList.value = row.records;
  };
  const onCheckboxAll = (flag) => {
    if (flag.checked) {
      selectdList.value = flag.records;
    } else {
      selectdList.value = [];
    }
  };

  const changeStatus = async () => {
    showModal.value = true;
  };
  const submitForm = async (type) => {
    const ids = selectdList.value.map((v) => v.id).join(',');
    await repayRecordFlowStatusByIds({
      ids: ids,
      flowStatus: type,
    });

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

    query();
  };
  const config = computed(() => {
    return {
      columns: [
        { type: 'checkbox', width: 60,fixed: 'left' },
        {
          field: 'caseId',
          showOverflow: 'tooltip',
          title: '案件ID',
          search: { el: 'input', labelWidth: 90 },
          slots: {
            default: ({ row }) => {
              if (row.reduce && row.reduce.reduceRecords) {
                return (
                  <>
                    {Array.from(new Set(row.reduce.reduceRecords.map((v) => v.loan.caseId))).join(
                      ','
                    )}
                  </>
                );
              } else if (row.stages && row.stages.loans) {
                return <>{Array.from(new Set(row.stages.loans.map((v) => v.caseId))).join(',')}</>;
              } else {
                return <>{row.loan?.caseId}</>;
              }
            },
          },
          width: 80,
        },
        {
          field: 'borrower.name',
          title: '借款人名字',
          showOverflow: 'tooltip',
          search: { el: 'input', key: 'borrower', labelWidth: 90 },
          width: 90,
        },
        {
          title: 'CPE',
          showOverflow: 'tooltip',
          width: 60,
          slots: {
            default: ({ row }) => {
              if (row.reduce && row.reduce.reduceRecords) {
                return (
                  <>
                    {Array.from(new Set(row.reduce.reduceRecords.map((v) => v.loan?.cpe?.username))).join(
                      ','
                    )}
                  </>
                );
              } else if (row.stages && row.stages.loans) {
                return <>{Array.from(new Set(row.stages.loans.map((v) => v?.cpe?.username))).join(',')}</>;
              } else {
                return <>{row.cpe?.username}</>;
              }
            },
          },
          search: { el: 'input', props: { clearable: true }, key: 'cpeName', labelWidth: 105 },
        },
        {
          showOverflow: 'tooltip',
          slots: {
            default: ({ row }) => {
              if (row.reduce && row.reduce.reduceRecords) {
                return (
                  <>
                    {Array.from(new Set(row.reduce.reduceRecords.map((v) => v.loan?.tenant?.name))).join(
                      ','
                    )}
                  </>
                );
              } else if (row.stages && row.stages.loans) {
                return <>{Array.from(new Set(row.stages.loans.map((v) => v?.tenant?.name))).join(',')}</>;
              } else {
                return <>{row.tenant?.name}</>;
              }
            },
          },
          enum: () => getTenantPage({ current: 1, size: 999999999, status: 'enable' }),
          search: { el: 'select', props: { filterable: true }, key: 'tenantId', labelWidth: 105 },
          fieldNames: { label: 'name', value: 'id' },
          title: '调解中心',
          width: 100,
        },
        {
          field: 'id',
          title: '还款记录ID',
          showOverflow: 'tooltip',
          search: { el: 'input', labelWidth: 90 },
        },
        {
          field: 'realRepayAmount',
          title: '实际还款金额',
          showOverflow: 'tooltip',
          search: { el: 'input', labelWidth: 90 },
        },
        {
          field: 'realRepayTime',
          showOverflow: 'tooltip',
          title: '实际还款时间',
          search: {
            el: 'date-picker',
            labelWidth: 90,
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
          width: 115,
        },
        {
          field: 'reduce.totalPayAmount',
          showOverflow: 'tooltip',
          title: '应还金额',
          search: { el: 'input', key: 'reduce.totalPayAmount', labelWidth: 90 },
          width: 115,
        },
        {
          field: 'repayType',
          showOverflow: 'tooltip',
          title: '还款方式',
          enum: RepayType,
          search: { el: 'select', props: { filterable: true }, span: 1, labelWidth: 90 },
          fieldNames: { label: 'label', value: 'value' },
          slots: {
            default: ({ row }) => {
              return (
                <>{row.repayType ? RepayType.value?.find((v) => v.value === row.repayType)?.label : ''}</>
              );
            },
          },
          width: 80,
        },
        {
          field: 'stages.loans.loanPlatform.name',
          title: '借款平台',
          showOverflow: 'tooltip',
          // search: { el: 'input', key: 'payOrg', labelWidth: 90 },
          width: 100,
        },
        {
          field: 'flowStatus',
          title: '审核状态',
          showOverflow: 'tooltip',
          width: 80,
          enum: FlowStatus,
          search: { el: 'select', props: { filterable: true }, span: 1, labelWidth: 90 },
          fieldNames: { label: 'label', value: 'value' },
          slots: {
            default: ({ row }) => {
              return (
                <>
                  {row.flowStatus
                    ? FlowStatus.value?.find((v) => v.value === row.flowStatus)?.label
                    : ''}
                </>
              );
            },
          },
        },
      ],
      onCheckboxChange: onCheckboxChange,
      onCheckboxAll: onCheckboxAll,
    };
  });
  const query = () => ProTableRef.value?.search();

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