<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!-- 行业分类 -->
      <template #form-suspect="{ model, field }">
        <a-select
          v-model:value="model[field]"
          placeholder="请选择行业分类"
          :fieldNames="{ label: 'text', value: 'value' }"
          :options="isSuspectOptions"
          @change="changeSuspect"
        ></a-select>
      </template>
      <template #form-category="{ model, field }">
        <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>
      </template>
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleEdit" v-auth="'qdml-fxpgyc-wsxx'">完善信息</a-button>
        <a-button type="primary" @click="handleview" v-auth="'qdml-fxpgyc-ckjbxx'">查看基本信息</a-button>
        <a-button type="primary" @click="edidDK" v-auth="'qdml-fxpgyc-xgmc'">修改地块名称</a-button>
        <a-button type="primary" @click="allocat" v-auth="'qdml-fxpgyc-zhfp'">账号分配</a-button>

        <a-button type="primary" @click="midify" v-auth="'qdml-fxpgyc-xgzb'">修改坐标</a-button>
      </template>
    </BasicTable>

    <!-- 表单区域 -->
    <GWrdkjbxxbModal @register="registerModal" @success="handleSuccess"></GWrdkjbxxbModal>

    <EditDKModal @register="registerEditdkModal" @success="handleSuccess" />
    <!-- 修改坐标 -->
    <ModifyModal @register="registerModifyModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="directory-ycmlqd" setup>
  import { BasicTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './data';
  import { list, getImportUrl, getExportUrl } from './api';
  import { useCategoryData, useSuspectsData } from '/@/utils/domUtils';
  import GWrdkjbxxbModal from '../components/GWrdkjbxxbModal.vue';
  import EditDKModal from '../components/EditDKModal.vue';
  import ModifyModal from '../components/ModifyModal.vue';
  import { allocation } from '../components/GWrdkjbxxb.api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import { tip } from '@/utils/checkTip';
  const { createMessage } = useMessage();

  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerEditdkModal, { openModal: openEditdkModal }] = useModal();
  const [registerModifyModal, { openModal: openModifyModal }] = useModal();

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '移除调查名录清单',
      api: list,
      columns,
      canResize: false,
      clickToRowSelect: true,
      rowSelection: { type: 'radio' },
      rowKey: 'wrdkid',
      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
      },
      showActionColumn: false,
      // actionColumn: {
      //     width: 120,
      // },
    },
    exportConfig: {
      name: '污染地块基本信息',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });

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

  const isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法

  // 修改地块名称
  function edidDK() {
    if (selectedRowKeys.value.length == 0) {
      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) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    await allocation({ wrdkid: selectedRowKeys.value[0] });
  }
  // 修改坐标
  function midify() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    openModifyModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  // 查看基本信息
  let router = useRouter();
  function handleview() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    router.push(`/directory/ycmlqd/viewSelct?wrdkid=${selectedRowKeys.value[0]}&dksdzt=${selectedRows.value[0].dksdzt}`);
  }
  /**
   * 编辑事件
   */
  function handleEdit() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择要修改的地块');
      return;
    }
    if (tip(selectedRows.value[0])) return;
    openModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }

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