<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!-- 行业分类 -->
      <template #form-suspect="{ model, field }">
        <a-select
          v-model:value="model[field]"
          placeholder="请选择行业分类"
          :fieldNames="{ label: 'text', value: 'value' }"
          :options="isSuspectOptions"
          @change="changeSuspect"
        ></a-select>
      </template>
      <template #form-category="{ model, field }">
        <a-tree-select
          v-model:value="model[field]"
          show-search
          placeholder="请选择二级"
          allow-clear
          tree-default-expand-all
          :field-names="{ label: 'name', value: 'id', children: 'children' }"
          :tree-data="treeData"
          tree-node-filter-prop="label"
        >
          <template #suffixIcon>
            <SmileOutlined />
          </template>
        </a-tree-select>
      </template>
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-auth="'fxpg-dps-jbxx'" @click="showModal('jbxx')">查看基本信息</a-button>
        <a-button type="primary" v-auth="'fxpg-dps-psclsc'" @click="showModal('psclsc')">评审材料上传</a-button>
        <a-button type="primary" v-auth="'fxpg-dps-th'" @click="showModal('th')">退回</a-button>
        <a-button type="primary" @click="onExportXls" v-auth="'fxpg-dps-dc'">导出</a-button>
        <a-button type="primary" @click="handelStep" v-auth="'fxpg-dps-lct'">流程图</a-button>
      </template>
    </BasicTable>
    <backModal @register="registerModal" @success="handleSuccess"></backModal>
    <!-- 流程图 -->
    <Step @register="registerlcModal" />
  </div>
</template>

<script lang="ts" name="dkgl-fxpg-Review" setup>
  import { BasicTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import backModal from '@/components/customComponent/backModal.vue';
  import Step from '/@/components/customComponent/steps/step.vue';
  import { columns, searchFormSchema, useSuspectsData, useCategoryData } from './data';
  import { reviewList, getImportUrl, getExportUrl } from './api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import { tbzjxx } from '/@/api/common/api';
  import { chTip } from '@/utils/checkTip';

  const { createMessage } = useMessage();
  const router = useRouter();

  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerlcModal, { openModal: openlcModal }] = useModal();
  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '风险评估-待评审',
      api: reviewList,
      columns,
      canResize: false,
      clickToRowSelect: true,
      showActionColumn: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'wrdkid',
      rowSelection: { type: 'radio' },
      beforeFetch() {
        let res = [...columns];
        res.splice(7, 0, {
          title: '组织评审提醒',
          dataIndex: 'tx',
          resizable: true,
        });

        setColumns(res);
      },
      formConfig: {
        labelWidth: 120,
        schemas: [
          ...searchFormSchema,
          {
            label: '阶段内状态',
            field: 'jdnzt',
            component: 'Input',
            defaultValue: 'T2',
            show: false,
          },
        ],
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
    },
    exportConfig: {
      name: '风险评估-待评审',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });
  const isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法
  const [registerTable, { reload, setColumns }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;
  /**
   * 查看基本信息
   * 评审材料
   * 退回
   */
  async function showModal(type) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (type === 'jbxx') {
      router.push(`/dkgl/fxpg/ViewDetail?wrdkid=${selectedRowKeys.value[0]}`);
    }
    if (type === 'psclsc') {
      if (!chTip(selectedRows.value)) return;
      const params = {
        scjdbm: selectedRows.value[0].scjdbm,
        wrdkid: selectedRowKeys.value[0],
      };
      // 评审材料
      await tbzjxx(params);
      router.push(
        `/dkgl/fxpg/modal/ReviewMaterialUpload?wrdkid=${selectedRowKeys.value[0]}&isnyd2yzlg=${selectedRows.value[0].isnyd2yzlg}&scjdbm=${selectedRows.value[0].scjdbm}`
      );
    }
    if (type === 'th') {
      if (!chTip(selectedRows.value)) return;
      openModal(true, {
        record: selectedRows.value[0],
        showFooter: true,
      });
    }
  }
  // 点击查看流程图
  function handelStep() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openlcModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  function handleSuccess() {
    reload();
  }
</script>
<style scoped></style>
