<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <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>
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:export-outlined" v-auth="'tbgj-dc'" @click="onExportXls"> 导出</a-button>
      <a-button type="primary" preIcon="ant-design:export-outlined" v-auth="'tbgj-tsgjk'" @click="handleTsgjk"> 推送国家库</a-button>
    </template>
  </BasicTable>
</template>
<script setup lang="ts" name="directory-tbgj-Investigate">
  import { BasicTable } from '@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';

  import { Columns, searchFormSchema, useCategoryData } from './data';
  import { sbdcml, sbdcmlExcel, pushWrdk } from './api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Modal } from 'ant-design-vue';

  const { createMessage } = useMessage();

  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '',
      api: sbdcml,
      columns: Columns,
      canResize: false,
      rowSelection: { type: 'radio' },
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      showActionColumn: false,
      beforeFetch(params: any) {
        if (params.data) {
          const arry = params.data.split(',');
          params.startTime = arry[0];
          params.endTime = arry[1];
        }
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '上报调查名录',
      url: sbdcmlExcel,
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据

  //推送国家库
  const handleTsgjk = () => {
    if (selectedRows.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    Modal.confirm({
      title: '系统提示',
      content: '请确认是否推送国家库？',
      centered: true,
      okText: '确认',
      cancelText: '取消',
      onOk: async () => {
        pushWrdk({ wrdkbm: selectedRows.value[0].wrdkbm, pushNode: '2' }).then(() => {
          reload();
        });
      },
    });
  };
</script>
