<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" @click="submit('fxpg-psytg-jbxx')" v-auth="'fxpg-psytg-jbxx'">查看基本信息</a-button>
        <a-button type="primary" @click="submit('th')" v-auth="'fxpg-dps-thbps'">退回无需评审</a-button>
        <a-button type="primary" @click="onExportXls" v-auth="'fxpg-psytg-dc'">导出</a-button>
        <a-button type="primary" @click="submit('cc')" v-auth="'fxpg-psytg-cc'">地块抽查</a-button>
        <a-button type="primary" @click="submit('cclh')" v-auth="'fxpg-psytg-cclh'">地块抽查历史</a-button>
        <a-button type="primary" @click="handelStep" v-auth="'fxpg-psytg-lct'">流程图</a-button>
        <a-button type="primary" @click="showDetail" v-auth="'fxpg:bcxx'">补充信息</a-button>
      </template>
    </BasicTable>
    <SendBack @register="registerModal" @success="handleSuccess" />
    <CheckHistory @register="registerCcModal" @success="handleSuccess" />
    <!-- 流程图 -->
    <Step @register="registerlcModal" />
  </div>
</template>

<script lang="ts" name="dkgl-fxpg-Approved" setup>
  import { useRouter } from 'vue-router';
  import { BasicTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import SendBack from '/@/components/customComponent/SendBack.vue';
  import CheckHistory from '/@/components/customComponent/CheckHistory.vue'; // 新组件
  import { columns, searchFormSchema, useSuspectsData, useCategoryData } from './data';
  import { approvedList, getImportUrl, getExportUrl } from './api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { tip, chTip, spotCheck, oneTip } from '@/utils/checkTip';
  import Step from '/@/components/customComponent/steps/step.vue';

  const [registerModal, { openModal }] = useModal();
  const [registerCcModal, { openModal: openCcModal }] = useModal();
  const [registerlcModal, { openModal: openlcModal }] = useModal();
  const router = useRouter();
  const { createMessage } = useMessage();
  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '风险评估-评审通过',
      api: approvedList,
      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: 'sfcb',
          resizable: true,
          customRender: ({ record }: any) => {
            return record?.sfcb==='1'?'是':record?.sfcb==='0'?'否':'未调查';
          },
        });
        res.splice(8, 0, {
          title: '地块锁定状态',
          dataIndex: 'dksdztText',
          resizable: true,
        });
        setColumns(res);
      },
      formConfig: {
        labelWidth: 120,
        schemas: [
          ...searchFormSchema,
          {
            label: '地块锁定状态',
            field: 'dksdzt',
            component: 'Select',
            componentProps: {
              options: [{ value: '1', label: '锁定中' }],
            },
          },
          {
            label: '阶段内状态',
            field: 'jdnzt',
            component: 'Input',
            defaultValue: 'SH',
            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;
  // 查看基本信息
  function submit(v) {
    if (v == 'fxpg-psytg-jbxx') {
      if (oneTip(selectedRowKeys.value)) return;
      router.push(`/dkgl/fxpg/ViewDetail?wrdkid=${selectedRowKeys.value[0]}&dksdzt=${selectedRows.value[0].dksdzt}`);
    } else if (v == 'th') {
      if (oneTip(selectedRowKeys.value)) return;
      if (tip(selectedRows.value[0])) return;
      openModal(true, {
        record: selectedRows.value[0],
        showFooter: true,
      });
    } else if (v == 'cc') {
      if (!chTip(selectedRows.value)) return;
      const params = {
        wrdkid: selectedRowKeys.value.toString(),
        sccdmkbm: 'S2',
        scjdbm: 'S3',
      };
      spotCheck(params, handleSuccess);
    } else if (v == 'cclh') {
      if (oneTip(selectedRowKeys.value)) return;

      const params = {
        wrdkid: selectedRowKeys.value[0],
        scjdbm: 'S3',
      };
      openCcModal(true, {
        record: params,
        showFooter: true,
      });
    }
  }
  // 点击查看流程图
  function handelStep() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1 ) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openlcModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  // 点击补充信息
  async function showDetail() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    
    router.push(`/dkgl/investigate/modal/DataFilling?wrdkid=${selectedRowKeys.value[0]}`);
  }

  function handleSuccess() {
    reload();
  }
</script>
<style scoped></style>
