<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" v-auth="'yztsjsj:add'">添加</a-button>
        <a-button type="primary" @click="onExportXls" v-auth="'yztsjsj:export'">导出</a-button>
        <a-button type="primary" @click="getMdb" v-auth="'yztsjsj:mdbgdb'">下载文件(mdb、gdb)</a-button>
        <a-button type="primary" @click="handleShls" v-auth="'yztsjsj:shls'">审核历史</a-button>
        <a-button type="primary" @click="handleZgSh(true)" v-auth="'yztsjsj:zgbmsh'" v-if="userStore.userInfo.userType ==2">自规部门审核</a-button>
        <a-button type="primary" @click="handleZgSh(false)" v-auth="'yztsjsj:zgbmshls'">自规部门审核</a-button>
        <a-button type="primary" @click="handleStUp" v-auth="'yztsjsj:stbmtj'">生态部门提交</a-button>
        <a-button type="primary" @click="handleStUp" v-auth="'yztsjsj:wjjc'">文件检查</a-button>
        <a-button type="primary" @click="handleStUp" v-auth="'yztsjsj: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>

    <!-- 表单区域 -->
    <AddModal @register="registerModal" @success="handleSuccess"></AddModal>
    <!-- 审核历史区域 -->
    <ShlsModal @register="registerModalShls"></ShlsModal>
    <!-- 自规部门审核区域 -->
    <ZgShModal @register="registerModalZgSh" @success="handleSuccess"></ZgShModal>
    <!-- 生态部门提交区域 -->
    <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-yztsj-shiji" setup>
  import { ref, h } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './data';
  import { list, getDownloadMdb, getDownloadGdb, deleteOne, getExportUrl, saveCityAuditCheck, cityZgbmCheck } from './api';
  import { existDclx, updateDclx, downloadFile, getDkDetail } from '/@/api/common/api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import AddModal from './modules/AddModal.vue'
  import ShlsModal from './modules/ShlsModal.vue'
  import StUpdateModal from './modules/StUpdateModal.vue'
  import ZgShModal from './modules/ZgShModal.vue'
  import { useRouter } from 'vue-router';
  import { Modal } from 'ant-design-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();
  //注册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 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, 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) {
      cityZgbmCheck(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.cityStatus == 2) {
      createMessage.info('数据已提交，无需再次提交');
      return;
    }
    const res = await saveCityAuditCheck({ jd: record.jd });
    if (res) {
      if (res && res.indexOf("暂不可提交") > -1) {
         Modal.warning({
          title: '提交提示',
          content: res,
        })
      } else {
        Modal.confirm({
          title: '提交提示',
          content: res,
          okText:'提交',
          onOk() {
            openModalStUp(true, {
              isUpdate: false,
              showFooter: true,
              record: record
            });
          },
        })
      }
    }
  }
  /**
   * 新增事件
   */
  function handleAdd() {
    let arr = getDataSource();
    const record: any = arr[0];
    if (record.cityStatus == 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}`);
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    if (record.cityStatus == 2) {
      createMessage.info('数据已提交，不能删除');
      return;
    }
    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: 'yztsjsj:delete',
        ifShow: record.cityStatus !=2
      },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
        auth: 'yztsjsj: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('文件下载失败');
      }
    });
  };

  const getGdb = () => {
    getDownloadGdb({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>
