<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection" :searchInfo="{type:2}">
      <!-- 行业分类 -->
      <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" @click="handleView" v-auth="'fxpg-fh-jbxx'">查看基本信息</a-button>
        <a-button type="primary" v-auth="'fxpg-fh-clps'" @click="showModal('psclsc')">抽查评审</a-button>
        <a-button type="primary" v-auth="'fxpg-fh-clsc'" @click="showModal('clsc')">材料上传</a-button>
      </template>
    </BasicTable>
    <Upcheckcl @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="dkgl-investigate-Improved" setup>
  import { BasicTable } from '/@/components/Table';
  import { useRouter } from 'vue-router';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useModal } from '/@/components/Modal';
  import Upcheckcl from '/@/components/customComponent/Upcheckcl.vue';
  import { columns, searchFormSchema, useSuspectsData, useCategoryData } from './data';
  import { getImportUrl, getExportUrl } from './api';
  import { checkList } from '/@/api/common/api';

  let router = useRouter();
  //注册model
  const [registerModal, { openModal }] = useModal();
  const { createMessage } = useMessage();
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '风险评估-抽查中',
      api: checkList,
      columns,
      canResize: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      rowSelection: { type: 'radio' },
      beforeFetch() {
        let res = [...columns];
        res.splice(7, 0, {
          title: '地块是否超标',
          dataIndex: 'sfcb',
          resizable: true,
          customRender: ({ record }: any) => {
            return record?.sfcb==='1'?'是':record?.sfcb==='0'?'否':'未调查';
          },
        });
        res.splice(8, 0, {
          title: '抽查状态',
          dataIndex: 'tgjg',
          resizable: true,
        });
        setColumns(res);
      },
      formConfig: {
        labelWidth: 120,
        schemas: [
          ...searchFormSchema,
          {
            label: '阶段内状态',
            field: 'sccdmkbm',
            component: 'Input',
            defaultValue: 'S2',
            show: false,
          },
          {
            label: '抽查状态',
            field: 'tgjg',
            component: 'Select',
            componentProps: {
              options: [
                { value: '0', label: '待抽查' },
                { value: '1', label: '不通过' },
                { value: '2', label: '待确认' },
              ],
            },
          },
        ],
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      showActionColumn: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '风险评估-抽查中',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });
  const isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法
  const [registerTable, { setColumns, reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;

  // 查看基本信息
  function handleView() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    router.push(`/dkgl/fxpg/ViewDetail?wrdkid=${selectedRowKeys.value[0]}`);
  }

  function showModal(type) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (type === 'psclsc') {
      // if (selectedRows.value[0].tgjg != '待确认') {
      //   createMessage.info('请选择待确认的数据');
      //   return;
      // }
      // 评审材料
      router.push(
        `/dkgl/fxpg/modal/ReviewMaterialUpload?wrdkid=${selectedRowKeys.value[0]}&isnyd2yzlg=${selectedRows.value[0].isnyd2yzlg}&scjdbm=S3&dclx=${selectedRows.value[0].dclx}&dksdzt=${selectedRows.value[0].dksdzt}`
      );
    } else if (type == 'clsc') {
      if (selectedRows.value[0].tgjg != '不通过') {
        createMessage.info('请选择不通过的数据');
        return;
      }
      openModal(true, {
        record: selectedRows.value[0],
        showFooter: true,
      });
    }
  }
  function handleSuccess() {
    reload();
  }
</script>
<style scoped></style>
