<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="onExportXls" v-auth="'yztsjxz:export'">导出</a-button>
        <a-button type="primary" @click="getMdb" v-auth="'yztsjxz:mdb'">下载mdb文件</a-button>
        <a-button type="primary" @click="getGdb" v-auth="'yztsjxz:gdb'">下载gdb文件</a-button>
        <!-- <a-button type="primary" @click="postUpdate">数据更新</a-button> -->
        <div style="color: red; text-align: right; flex: 1; line-height: 32px">注：本次数据为全量数据，生成的结果为前一天数据结果！</div>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </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 { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './data';
  import { list, getDownloadMdb, getDownloadGdb, getExportUrl, update } from './api';
  import { existDclx, updateDclx, downloadFile, getDkDetail } from '/@/api/common/api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import { Modal } from 'ant-design-vue';
  const { createMessage } = useMessage();
  let router = useRouter();
  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '数据下载',
      api: list,
      columns,
      canResize: false,
      clickToRowSelect: true,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'dkdm',
      rowSelection: { type: 'radio' },
      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 }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;

  //成功回调
  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
  const postUpdate = ()=>{
    update(null).then((res)=>{
      if(res){
        createMessage.success('数据更新成功');
        reload();
      }else{
        createMessage.error('数据更新失败');
      }
    });
  }
  /**
   * 详情
   */
  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 = () => {
    getDownloadMdb(null).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>
