<template>
  <div class="table-box">
    <ProTable :config="config" ref="ProTableRef" :api="getByStagesPage">
      <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 { getByStagesPage, byStagesflowStatusByIds } 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: 2,
      label: '2',
    },
    {
      value: 3,
      label: '3',
    },
    {
      value: 4,
      label: '4',
    },
    {
      value: 5,
      label: '5',
    },
    {
      value: 6,
      label: '6',
    },
  ];
  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 byStagesflowStatusByIds({
      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: 'payOrg',
          title: '借款机构',
          search: { el: 'input' },
        },
        {
          field: 'borrower.name',
          title: '借款人名字',
          search: { el: 'input', key: 'borrower' },
          width: 90,
        },
        {
          field: 'totalRepayAmount',
          title: '还款总额',
          search: { el: 'input' },
          width: 80,
        },
        {
          field: 'applyDate',
          title: '分期申请时间',
          search: {
            el: 'date-picker',
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
          width: 115,
        },
        {
          field: 'effectiveTime',
          title: '分期生效时间',
          search: {
            el: 'date-picker',
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
          width: 115,
        },
        {
          field: 'totalPeriod',
          title: '还款期数',
          enum: options,
          search: { el: 'select', props: { filterable: true }, span: 1 },
          fieldNames: { label: 'label', value: 'value' },
          width: 80,
        },
        {
          field: 'firstApplyDate',
          title: '首期还款日',
          search: { el: 'input' },
          search: { el: 'date-picker', props: { type: 'date', valueFormat: 'YYYY-MM-DD' } },
          width: 100,
        },
        {
          field: 'remainingAmount',
          title: '剩余待还金额',
          search: { el: 'input' },
          width: 110,
        },
        {
          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>
