<template>
  <div class="table-box">
    <ProTable :config="config" ref="ProTableRef" :api="getRepayRecordPage">
      <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 { getRepayRecordPage, repayRecordFlowStatusByIds } 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 options = [
    {
      value: 'repay',
      label: '直接还款',
    },
    {
      value: 'by_stages',
      label: '分期还款',
    },
    {
      value: 'settle',
      label: '结清减免还款',
    },
  ];
  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 repayRecordFlowStatusByIds({
      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: 'id',
          title: '还款记录ID',
          search: { el: 'input' },
        },
        {
          field: 'realRepayAmount',
          title: '实际还款金额',
          search: { el: 'input' },
        },
        {
          field: 'realRepayTime',
          title: '实际还款时间',
          search: {
            el: 'date-picker',
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
          width: 115,
        },
        {
          field: 'reduce.totalPayAmount',
          title: '应还金额',
          search: { el: 'input', key: 'reduce.totalPayAmount' },
          width: 115,
        },
        {
          field: 'repayType',
          title: '还款方式',
          enum: options,
          search: { el: 'select', props: { filterable: true }, span: 1 },
          fieldNames: { label: 'label', value: 'value' },
          width: 80,
        },
        {
          field: 'stages.loans.payOrg',
          title: '借款机构',
          search: { el: 'input', key: 'stages.loans.payOrg' },
          width: 100,
        },
        {
          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
                    : ''}
                </>
              );
            },
          },
        },
      ],
      onCheckboxChange: onCheckboxChange,
    };
  });
  const query = () => ProTableRef.value?.search();

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