<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleShls" v-auth="'yztsjxzls:shls'">审核历史</a-button>
        <a-button type="primary" @click="onExportXls" v-auth="'yztsjxzls:export'">导出</a-button>
        <a-button type="primary" @click="getMdb" v-auth="'yztsjxzls:mdbgdb'">下载文件(mdb、gdb)</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>

    <!-- 表单区域 -->
    <ShlsModal @register="registerModal" @success="handleSuccess"></ShlsModal>
  </div>
  
  <!--查看详情前置 -->
  <a-modal v-model:visible="open" :centered="true" title="信息">
    <template #footer>
      <a-button key="submit" type="primary" @click="open = false">确定</a-button>
    </template>
    <div class="py-10 ml-6">该地块还未进行填报，没有详情可以查看</div>
  </a-modal>
</template>

<script lang="ts" name="directory-gzml-gzml" setup>
  import { ref, h } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './data';
  import { list, getDownloadMdb, getDownloadGdb, getExportUrl } from './api';
  import { existDclx, updateDclx, downloadFile, getDkDetail } from '/@/api/common/api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import ShlsModal from './modules/ShlsModal.vue'
  import { useRouter } from 'vue-router';
  import { Modal } from 'ant-design-vue';
  const { createMessage } = useMessage();
  let router = useRouter();
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '历史数据下载',
      api: list,
      columns,
      canResize: false,
      clickToRowSelect: true,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'dkdm',
      rowSelection: { type: 'radio' },
      // beforeFetch(params) {
      //   params.jd = params.nd + params.jd;
      // },  
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      showIndexColumn: true,
      actionColumn: {
        width: 80,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: "一张图数据管理——历史数据下载",
      url: getExportUrl,
    },
  });
  const noChange = ref<boolean>(false);
  const selectdclx = ref<number>(1);
  const flg = ref(1);
  const visible = ref<boolean>(false);
  const bgvisible = ref<boolean>(false);
  const [registerTable, { reload, getForm }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;

  //成功回调
  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
  /**
   * 审核历史
   */
  function handleShls(record: Recordable) {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    getDkDetail({wrdkbm: record.dkdm}).then(res =>{
      handleView(res.dkxx)
    })
  }
  
  const open = ref(false);
  //查看基本信息
  function handleView(record) {
    if (!record.dclx) {
      open.value = true;
      return;
    }
    router.push(`/directory/mlzk/viewSelct?wrdkid=${record.wrdkid}&dksdzt=${record.dksdzt}`);
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      }
    ];
  }
  
  const getMdb = () => {
    const { nd, jd } = getForm().getFieldsValue();
    getDownloadMdb({ nd, jd }).then((res) => {
      // console.log(res);
      if (res) {
        downloadFile('/sys/common/static' + res, res.split('/')[res.split('/').length - 1]);
      } else {
        createMessage.error('文件下载失败');
      }
    });
  };

  const getGdb = () => {
    getDownloadGdb(null).then((res) => {
      // console.log(res);
      if (res) {
        downloadFile('/sys/common/static' + res, res.split('/')[res.split('/').length - 1]);
      } else {
        createMessage.error('文件下载失败');
      }
    });
  };
</script>
<style scoped></style>
