<template>
  <div>
    <a-tabs v-model:activeKey="activeKey" type="card">
      <a-tab-pane key="1" tab="数据信息">
        <!--引用表格-->
        <BasicTable @register="registerTable" :rowSelection="rowSelection">
          <!--插槽:table标题-->
          <template #tableTitle>
            <a-button type="primary" @click="handleAdd" v-auth="'yztsjshj:add'">添加</a-button>
            <a-button type="primary" @click="onExportXls" v-auth="'yztsjshj:export'">导出</a-button>
            <a-button type="primary" @click="getMdb" v-auth="'yztsjshj:mdbgdb'">下载文件(mdb、gdb)</a-button>
            <!-- <a-button type="primary" @click="handleShls" v-auth="'yztsjshj:shls'">审核历史</a-button> -->
            <a-button type="primary" @click="handleZgSh(true)" v-auth="'yztsjshj:zgbmsh'" v-if="userStore.userInfo.userType == 2"
              >自规部门审核</a-button
            >
            <a-button type="primary" @click="handleZgSh(false)" v-auth="'yztsjshj:zgbmshls'">自规部门审核</a-button>
            <a-button type="primary" @click="handleStUp" v-auth="'yztsjshj:stbmtj'">生态部门提交</a-button>
            <a-button type="primary" @click="handleFileCheck" v-auth="'yztsjshj:wjjc'">文件检查</a-button>
            <a-button type="primary" @click="handleCheckResultDownload" v-auth="'yztsjshj:jcjgxz'">检查结果下载</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>
      </a-tab-pane>
      <a-tab-pane key="2" tab="审核信息" v-if="userStore.userInfo.userType != 2">
        <ShList @success="handleSuccess" />
      </a-tab-pane>
    </a-tabs>
    <!-- 表单区域 -->
    <AddModal @register="registerModal" @success="handleSuccess"></AddModal>
    <!-- 审核历史区域 -->
    <ShlsModal @register="registerModalShls"></ShlsModal>
    <!-- 自规部门审核区域 -->
    <ZgShModal @register="registerModalZgSh" @success="handleSuccess"></ZgShModal>
    <!-- 生态部门退回区域 -->
    <StThModal @register="registerModalStTh" @success="handleSuccess"></StThModal>
    <!-- 生态部门提交区域 -->
    <StUpdateModal @register="registerModalStUp" @success="handleSuccess"></StUpdateModal>
  </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 } 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, deleteOne, getExportUrl, provinceCheckSubmit, proviceZgbmCheck, fileCheck, checkSuccessDownLoad } from './api';
  import { downloadFile, getDkDetail } from '/@/api/common/api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import AddModal from './modules/AddModal.vue';
  import ShlsModal from './modules/ShlsModal.vue';
  import StUpdateModal from './modules/StUpdateModal.vue';
  import ShList from './ShList.vue';
  import StThModal from './modules/StThModal.vue';
  import ZgShModal from './modules/ZgShModal.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { getNd, getJd } from '/@/utils/ndjd';
  const userStore: any = useUserStore();
  const { createMessage } = useMessage();
  let router = useRouter();
  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerModalShls, { openModal: openModalShls }] = useModal();
  const [registerModalStUp, { openModal: openModalStUp }] = useModal();
  const [registerModalZgSh, { openModal: openModalZgSh }] = useModal();
  const [registerModalStTh, { openModal: openModalStTh }] = useModal();
  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '省级数据',
      api: list,
      columns,
      canResize: false,
      clickToRowSelect: true,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'id',
      rowSelection: { type: 'radio' },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      showIndexColumn: true,
      actionColumn: {
        width: 160,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '一张图数据管理——省级审核',
      url: getExportUrl,
    },
  });
  const open = ref(false);

  const [registerTable, { reload, getDataSource }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;

  //成功回调
  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
  /**
   * 审核历史
   */
  /**
   * 审核历史
   */
  function handleShls() {
    let arr = getDataSource();
    if (arr.length == 0) {
      createMessage.info('暂无审核数据，请稍后再试');
      return;
    }
    const record: any = arr[0];
    openModalShls(true, {
      isUpdate: false,
      showFooter: true,
      record: record,
    });
  }
  /**
   * 自规部门审核
   */
  function handleZgSh(type: boolean) {
    let arr = getDataSource();
    if (arr.length == 0) {
      createMessage.info('暂无需审核数据，请稍后再试');
      return;
    }
    const record: any = arr[0];
    if (type) {
      proviceZgbmCheck(null).then((res) => {
        if (res) {
          openModalZgSh(true, {
            isUpdate: true,
            showFooter: true,
            record: record,
          });
        } else {
          openModalZgSh(true, {
            isUpdate: false,
            showFooter: true,
            record: record,
          });
        }
      });
    } else {
      openModalZgSh(true, {
        isUpdate: type,
        showFooter: true,
        record: record,
      });
    }
  }
  /**
   * 生态部门提交
   */
  async function handleStUp() {
    let arr = getDataSource();
    if (arr.length == 0) {
      createMessage.info('暂无提交数据，请稍后再试');
      return;
    }
    const record: any = arr[0];
    if (record.provinceStatus == 2) {
      createMessage.info('数据已提交，无需再次提交');
      return;
    }
    const checkRes = await provinceCheckSubmit({ jd: record.jd });
    if (!checkRes || (checkRes && checkRes.indexOf('暂不可提交') > -1) || checkRes == 'false') {
      createMessage.warning(checkRes);
      return;
    }
    openModalStUp(true, {
      isUpdate: false,
      showFooter: true,
      record: record,
    });
  }

  async function handleFileCheck() {
    const res = await fileCheck({ nd: getNd(), jd: getJd() });
    if (res) {
      createMessage.success('文件检查任务已发起');
    }
  }

  async function handleCheckResultDownload() {
    const res = await checkSuccessDownLoad({ nd: getNd(), jd: getJd() });
    if (typeof res === 'string' && res.indexOf('/') > -1) {
      downloadFile('/sys/common/static' + res, res.split('/')[res.split('/').length - 1]);
      return;
    }
    createMessage.info((res as string) || '文件校验中');
  }
  /**
   * 新增事件
   */
  function handleAdd() {
    let arr = getDataSource();
    const record: any = arr[0];
    if (record && record.provinceStatus == 2) {
      createMessage.info('数据已提交，不能添加新数据');
      return;
    }
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */

  function handleDetail(record: Recordable) {
    getDkDetail({ wrdkbm: record.dkdm }).then((res) => {
      handleView(res.dkxx);
    });
  }

  //查看基本信息
  function handleView(record) {
    if (!record.dclx) {
      open.value = true;
      return;
    }
    router.push(`/directory/mlzk/viewSelct?wrdkid=${record.wrdkid}&dksdzt=${record.dksdzt}`);
  }
  /**
   * 退回
   */
  function handleBack(record: Recordable) {
    openModalStTh(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, reload);
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      // {
      //   label: '编辑',
      //   onClick: handleEdit.bind(null, record),
      // },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
        auth: 'yztsjshj:delete',
        ifShow: record.provinceStatus != 2,
      },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
        auth: 'yztsjshj:detail',
      },
    ];
  }

  const getMdb = () => {
    getDownloadMdb({ nd: getNd(), jd: getJd() }).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>
