<template>
  <div class="bg-[#fff]">
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!-- 行业分类 -->
      <template #form-suspect="{ model, field }">
        <div>
          <a-select
            v-model:value="model[field]"
            placeholder="请选择"
            :fieldNames="{ label: 'text', value: 'value' }"
            :options="isSuspectOptions"
            @change="changeSuspect"
          ></a-select>
        </div>
      </template>
      <template #form-category="{ model, field }">
        <div>
          <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>
        </div>
      </template>

      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" v-auth="'qdml-add'">添加</a-button>
        <a-button type="primary" @click="handleEdit" v-auth="'qdml-info'">完善信息</a-button>
        <a-button type="primary" @click="handleView" v-auth="'qdml-view'">查看基本信息</a-button>
        <a-button type="primary" @click="edidDK" v-auth="'qdml-dkmc'">修改地块名称</a-button>
        <a-button type="primary" @click="allocat" v-auth="'qdml-zhfp'">账号分配</a-button>
        <a-button type="primary" v-auth="'qdml-del'" @click="del">删除</a-button>
        <a-button type="primary" @click="move" v-auth="'qdml-gz'">移入预关注</a-button>
        <a-button type="primary" @click="midify" v-auth="'qdml-zb'">修改坐标</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" v-auth="'qdml-dc'" @click="onExportXls"> 导出</a-button>
        <a-button type="primary" v-auth="'qdml-download'" @click="onExportZip(selectedRows.map((item) => item.wrdkbm))">矢量文件批量下载</a-button>
      </template>
    </BasicTable>

    <!-- 表单区域 -->
    <GWrdkjbxxbModal @register="registerModal" @success="handleSuccess"></GWrdkjbxxbModal>
    <!-- 点击删除的弹框 -->
    <DelModal @register="registerDelModal" @success="handleSuccess"></DelModal>

    <EditDKModal @register="registerEditdkModal" @success="handleSuccess" />
    <!-- 修改坐标 -->
    <ModifyModal @register="registerModifyModal" @success="handleSuccess" />
    <!-- 移入关注 -->
    <MoveinModal @register="registerMoveModal" @success="handleSuccess" />
    <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>
  </div>
</template>

<script lang="ts" name="directory-mlzk-land" setup>
  import { ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import { BasicTable } from '/@/components/Table';

  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';

  import GWrdkjbxxbModal from '../components/GWrdkjbxxbModal.vue';
  import EditDKModal from '../components/EditDKModal.vue';
  import ModifyModal from '../components/ModifyModal.vue';
  import MoveinModal from '../components/MoveinModal.vue';
  import DelModal from '../components/DelModal.vue';
  import { columns, searchFormSchema, useSuspectsData, useCategoryData } from './data';
  import { list, getImportUrl, getExportUrl, AllFileDown } from './api';
  import { allocation } from '../components/GWrdkjbxxb.api';
  import { tip } from '@/utils/checkTip';
  import {getDayOfMonth, getDayOfYear, getYearDay} from "xe-utils";

  const { createMessage } = useMessage();
  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerDelModal, { openModal: openDelModal }] = useModal();
  const [registerEditdkModal, { openModal: openEditdkModal }] = useModal();
  const [registerModifyModal, { openModal: openModifyModal }] = useModal();
  const [registerMoveModal, { openModal: openMoveModal }] = useModal();
  //获取年月日
  const currentDate = () => {
    const date = new Date();
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的，所以+1
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`;
  };
  //注册table数据
  const { tableContext, onExportXls, onExportZip } = useListPage({
    tableProps: {
      title: '地块基本信息',
      api: list,
      columns,
      rowSelection: { type: 'checkbox' },
      canResize: false,
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      showActionColumn: false,
      beforeFetch() {
        let res = [...columns];
        res.splice(8, 0, {
          title: '地块锁定状态',
          dataIndex: 'dksdztText',
          resizable: true,
        });
        setColumns(res);
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
        autoAdvancedCol: 20, //超过几行显示折叠默认是3
      },
      handleSearchInfoFn: () => {
        selectedRows.value = [];
        selectedRowKeys.value = [];
      },
      actionColumn: {
        width: 120,
      },
    },
    exportConfig: {
      name: '清单名录基本信息'+ currentDate(),
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
    exportZip: {
      name: '矢量文件批量下载',
      url: AllFileDown,
    },
  });

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

  const open = ref(false);
  const isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法
  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  // 修改地块名称
  function edidDK() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    openEditdkModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  // 点击账号分配
  async function allocat() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    await allocation({ wrdkid: selectedRowKeys.value[0] });
  }
  // 修改坐标
  function midify() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    openModifyModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  //  删除
  async function del() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    openDelModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1) {
      createMessage.info('请选择要修改的地块，每次只能选中一条');
      return;
    }
    if (tip(selectedRows.value[0])) return;

    openModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  // 移入关注
  function move() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1) {
      createMessage.info('请选择要移入关注的数据');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    openMoveModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  let router = useRouter();
  function handleView() {
    if (selectedRowKeys.value.length == 0 || selectedRowKeys.value.length > 1) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    const record: any = selectedRows.value[0];
    if (!record.dclx) {
      open.value = true;
      return;
    }
    router.push(`/directory/mlzk/viewSelct?wrdkid=${selectedRowKeys.value[0]}&dksdzt=${selectedRows.value[0].dksdzt}`);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
</script>
<style scoped></style>
