<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="suspects"
            @change="Changetype"
          ></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="handleEdit" v-auth="'qdml-fxpg-wsxx'">完善信息</a-button>

        <a-button type="primary" @click="edidDK" v-auth="'qdml-fxpg-xgdk'">修改地块名称</a-button>
        <a-button type="primary" @click="allocat" v-auth="'qdml-fxpg-zhfp'">账号分配</a-button>

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

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

    <EditDKModal @register="registerEditdkModal" @success="handleSuccess" />
    <!-- 修改坐标 -->
    <ModifyModal @register="registerModifyModal" @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="dkgl-gWrdkjbxxb" setup>
  import { ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  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 { categoryList, dict } from '/@/api/common/api';
  import { columns, searchFormSchema } from './data';
  import { list, getImportUrl, getExportUrl } from './api';
  import { allocation } from '../components/GWrdkjbxxb.api';

  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,
      rowSelection: { type: 'radio' },
      canResize: false,
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      showActionColumn: false,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
        autoAdvancedCol: 20, //超过几行显示折叠默认是3
      },
      actionColumn: {
        width: 120,
      },
    },
    exportConfig: {
      name: '污染地块基本信息',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });

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

  const open = ref(false);

  // 获取行业类别
  let allCategory = [];
  const treeData = ref<any>([]);
  async function getcategory() {
    allCategory = await categoryList();
    treeData.value = allCategory;
  }
  getcategory();
  // 获取行业分类
  const suspects = ref([]);
  async function gethylb() {
    suspects.value = await dict({ key: 'dk_hyfl' });
  }
  gethylb();
  // 点击行业类别事件
  function Changetype(v) {
    if (v == 1) {
      // 只显示42号类
      treeData.value = allCategory.filter((item) => item['is42']);
    } else {
      treeData.value = allCategory;
    }
  }

  // 修改地块名称
  function edidDK() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openEditdkModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  // 点击账号分配
  async function allocat() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    await allocation({ wrdkid: selectedRowKeys.value[0] });
  }
  // 修改坐标
  function midify() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openModifyModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }

  /**
   * 编辑事件
   */
  function handleEdit() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择要修改的地块');
      return;
    }
    openModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }

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