<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>
      <template #expand_content="{ row }">
        <div class="expand-box">
          <div>案件明细</div>
          <div class="expand-table">
            <div v-for="(item, index) in row.reduceRecords" :key="index">
              <div class="public-header">
                {{ item?.loan?.caseId }}
              </div>
              <table
                ><tbody>
                  <tr>
                    <td class="label">案件ID</td>
                    <td>{{ item?.loan?.caseId }}</td>
                    <td class="label">借款机构</td>
                    <td>{{ item?.loan?.loanPlatform?.name }}</td>
                    <td class="label">资管公司</td>
                    <td>{{ item?.loan?.manageOrg.orgName }}</td>
                  </tr>
                  <tr>
                    <td class="label">身份证</td>
                    <td>{{ item?.loan?.borrower?.idCard }}</td>
                    <td class="label">手机号</td>
                    <td>{{ item?.loan?.borrower?.phone }}</td>
                    <td class="label">折扣</td>
                    <td>{{ item?.loan?.discount }}</td>
                    <td class="label">委案金额</td>
                    <td>{{ item?.loan?.commissionAmount }}</td>
                  </tr>
                  <tr>
                    <td class="label">累计还款金额</td>
                    <td>{{ item?.loan?.sumRepayAmount }}</td>
                    <td class="label">累计减免金额</td>
                    <td>{{ item?.loan?.sumReductionAmount }}</td>
                    <td class="label">减免金额</td>
                    <td>{{ item?.reduceAmount }}</td>
                    <td class="label">剩余待还金额</td>
                    <td>{{ item?.loan?.remainingAmount }}</td>
                  </tr>
                  <tr>
                    <td class="label">CPE</td>
                    <td>{{ item?.loan?.cpe?.username }}</td>
                    <td class="label">分派CPE日期</td>
                    <td>{{ item?.loan?.cpeDate }}</td>
                    <td class="label">本金余额</td>
                    <td>{{ item?.loan?.principalBalance }}</td>
                    <td class="label">案件状态</td>
                    <td>
                      {{
                        item?.loan?.caseStatus
                          ? CaseStatus?.find((v) => v.value === item?.loan?.caseStatus)?.label
                          : ''
                      }}</td
                    >
                  </tr>
                  <tr>
                    <td class="label">手续费</td>
                    <td>{{ item?.loan?.commission }}</td>
                    <td class="label">跟进结果</td>
                    <td>{{
                      item?.loan?.followStatus
                        ? FollowStatus?.find((v) => v.value === item?.loan?.followStatus)?.label
                        : ''
                    }}</td>
                    <td class="label">跟进状态</td>
                    <td>{{
                      item?.loan?.phoneResultStatus
                        ? PhoneResultStatus?.find(
                            (v) => v.value === item?.loan?.phoneResultStatus
                          )?.label
                        : ''
                    }}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </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="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';
  import { getTenantPage } from '@/api/tenant';
  import { useDict } from '@/hooks/useDict';

  const { PhoneResultStatus, ReduceType, FlowStatus, FollowStatus, CaseStatus } = useDict("PhoneResultStatus","ReduceType", "FlowStatus", "FollowStatus", "CaseStatus");

  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 flowStatusByIds({
      ids: ids,
      flowStatus: type,
    });

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

    query();
  };
  const config = computed(() => {
    return {
      columns: [
        { type: 'checkbox', width: 50 },
        { type: 'expand', width: 40, slots: { content: 'expand_content' } },
        {
          field: 'caseId',
          title: '案件ID',
          showOverflow: 'tooltip',
          search: { el: 'input', labelWidth: 80 },
          slots: {
            default: ({ row }) => {
              return (
                <>{Array.from(new Set(row.reduceRecords.map((v) => v.loan.caseId))).join(',')}</>
              );
            },
          },
          width: 120,
        },
        {
          field: 'borrower.name',
          title: '借款人名字',
          showOverflow: 'tooltip',
          search: { el: 'input', key: 'borrower', labelWidth: 80 },
          width: 90,
        },
        {
          field: 'totalReduceAmount',
          showOverflow: 'tooltip',
          title: '减免金额',
          search: { el: 'input', labelWidth: 80 },
          width: 120,
        },
        {
          field: 'totalNumber',
          showOverflow: 'tooltip',
          title: '减免案件数',
          search: { el: 'input', labelWidth: 80 },
          width: 120,
        },
        {
          field: 'remainingAmount',
          showOverflow: 'tooltip',
          title: '剩余待还金额',
          search: { el: 'input', labelWidth: 80 },
          width: 120,
        },
        {
          field: 'totalPayAmount',
          showOverflow: 'tooltip',
          title: '累计还款金额',
          search: { el: 'input', labelWidth: 80 },
          width: 120,
        },
        {
          title: 'CPE',
          showOverflow: 'tooltip',
          width: 60,
          slots: {
            default: ({ row }) => {
              if (row.cpe?.username) {
                return (
                  <>{row.cpe?.username}</>
                );
              } else {
                return (
                  <>{Array.from(new Set(row.reduceRecords.map((v) => v.loan?.cpe?.username))).join(',')}</>
                );
              }
            },
          },
          search: { el: 'input', props: { clearable: true }, key: 'cpeName', labelWidth: 78 },
        },
        {
          showOverflow: 'tooltip',
          slots: {
            default: ({ row }) => {
              if (row.tenant?.name) {
                return (
                  <>{row.tenant?.name}</>
                );
              } else {
                return (
                  <>{Array.from(new Set(row.reduceRecords.map((v) => v.loan?.tenant?.name))).join(',')}</>
                );
              }
              
            },
          },
          enum: () => getTenantPage({ current: 1, size: 999999999, status: 'enable' }),
          search: { el: 'select', props: { filterable: true }, key: 'tenantId', labelWidth: 78 },
          fieldNames: { label: 'name', value: 'id' },
          title: '调解中心',
          width: 100,
        },
        {
          field: 'flowStatus',
          title: '审核状态',
          showOverflow: 'tooltip',
          width: 80,
          enum: FlowStatus,
          search: { el: 'select', props: { filterable: true }, span: 1, labelWidth: 80 },
          fieldNames: { label: 'label', value: 'value' },
          slots: {
            default: ({ row }) => {
              return (
                <>
                  {row.flowStatus
                    ? FlowStatus.value?.find((v) => v.value === row.flowStatus)?.label
                    : ''}
                </>
              );
            },
          },
        },
        {
          field: 'applyDate',
          title: '减免截止时间',
          showOverflow: 'tooltip',
          search: {
            el: 'date-picker',
            labelWidth: 80,
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
        },
        {
          field: 'inEffectDate',
          title: '减免生效时间',
          showOverflow: 'tooltip',
          search: {
            el: 'date-picker',
            labelWidth: 80,
            props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
          },
        },
      ],
      onCheckboxChange: onCheckboxChange,
      onCheckboxAll: onCheckboxAll,
    };
  });
  const query = () => ProTableRef.value?.search();

  onMounted(() => {
    query();
  });
</script>
<style lang="scss" scoped>
  .expand-box {
    background: #fff;
    padding: 10px;
    .expand-table {
      color: #000;
      table {
        width: 100%;
        margin-top: 10px;
        border-collapse: collapse;
      }
      td {
        border: 1px solid rgba(5, 5, 5, 0.06);
        background: #fff;
        padding: 8px;
        width: 240px;
        &.label {
          text-align: center;
          background: var(--el-color-primary-light-9);
        }
      }
    }
  }
</style>
