<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="handleView" v-auth="'fxpg-tbz-jbxx'">查看基本信息</a-button>
        <a-button type="primary" @click="filling" v-auth="'fxpg-tbz-zltb'">资料填报</a-button>
        <a-button type="primary" @click="submit" v-auth="'fxpg-tbz-tj'">提交</a-button>
        <a-button type="primary" @click="onExportXls" v-auth="'fxpg-tbz-dc'">导出</a-button>
        <a-button type="primary" @click="handelStep" v-auth="'fxpg-tbz-lct'">流程图</a-button>
        <a-button type="primary" @click="handelPgjs" v-auth="'fxpg-tbz-fxpgjs'">风险评估计算</a-button>
        <a-button type="primary" @click="handelPgjg" v-auth="'fxpg-tbz-fxpgjg'">风险评估结果查看</a-button>
      </template>
    </BasicTable>
    <!-- 点击提交出现的提示弹框 暂时不用-->
    <a-modal v-model:visible="Tj" title="信息" centered @ok="submit" :bodyStyle="{ 'padding-left': '30px' }" :width="700">
      <template #footer>
        <div>
          <a-button type="primary" @click="submit">我已了解，继续提交</a-button>
        </div>
      </template>
      <dksumitTip />
    </a-modal>
    <Step @register="registerlcModal" />
    <!-- 计算的弹框 -->
    <Jsresults @register="registerjsresultModal" :wrdkid="selectedRowKeys && selectedRowKeys[0]" />
  </div>
</template>

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

  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '风险评估-填报中',
      api: fillingList,
      columns,
      canResize: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'wrdkid',
      rowSelection: { type: 'radio' },
      clickToRowSelect: true,
      showActionColumn: false,
      formConfig: {
        labelWidth: 120,
        schemas: [
          ...searchFormSchema,
          {
            label: '阶段内状态',
            field: 'jdnzt',
            component: 'Input',
            defaultValue: 'T1',
            show: false,
          },
        ],
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
      },
    },
    exportConfig: {
      name: '风险评估-填报中',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });
  const [registerlcModal, { openModal: openlcModal }] = useModal();
  const [registerjsresultModal, { openModal: openjsresultModal }] = useModal();
  const isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法
  const [registerTable, { reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;

  let router = useRouter();
  const { createMessage } = useMessage();
  let Tj = ref(false);
  function filling() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
   
    if (selectedRows.value[0].tbjdbm != 'TH') {
      if (!chTip(selectedRows.value)) return;
    } 
    router.push(`/dkgl/fxpg/UpFilling?wrdkid=${selectedRowKeys.value[0]}`);
  }
  // 查看基本信息
  function handleView() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    // if (!chTip(selectedRows.value)) return;
    router.push(`/dkgl/fxpg/ViewDetail?wrdkid=${selectedRowKeys.value[0]}`);
  }
  // 点击提交
  async function submit() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!chTip(selectedRows.value) && selectedRows.value[0].tbjdbm !='TH') return;
    submitFxpg({ wrdkid: selectedRowKeys.value[0] }, handleSuccess);
    Tj.value = false;
  }
  // 点击查看流程图
  function handelStep() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openlcModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  function handleSuccess() {
    reload();
  }
  function handelPgjs() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    router.push(`/zcgl/wrcdfxpg/index?wrdkid=${selectedRowKeys.value[0]}`);
  }
  function handelPgjg() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openjsresultModal(true, { record: {}, isUpdate: true, showFooter: false });
  }
</script>
<style scoped></style>
