<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" ref="tableRef" :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-baml-wsxx'">完善信息</a-button>
        <a-button type="primary" v-auth="'qdml-baml-del'" @click="del">删除</a-button>
      </template>
    </BasicTable>
    <DelModal @register="registerDelModal" @success="handleSuccess"></DelModal>
    <GWrdkjbxxbModal @register="registerModal" @success="handleSuccess"></GWrdkjbxxbModal>
    <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-baml-baml" setup>
  import { ref } from 'vue';
  import { Modal } from 'ant-design-vue';
  import { BasicTable, TableActionType } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema, useSuspectsData, useCategoryData } from './data';
  import { BahygzList, getImportUrl, getExportUrl } from './api';
  import GWrdkjbxxbModal from '../components/GWrdkjbxxbModal.vue';
  import DelModal from '../components/DelModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import { submitWrdkdc } from '/@/api/common/api';
  const { createMessage } = useMessage();
  //注册model
  const [registerModal, { openModal }] = useModal();
  const [registerDelModal, { openModal: openDelModal }] = useModal();
  const tableRef = ref<Nullable<TableActionType>>(null);
  const open = ref(false);
  let router = useRouter();
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '清单名录-备案地块清单',
      api: BahygzList,
      columns,
      canResize: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'wrdkid',
      rowSelection: { type: 'radio' },
      clickToRowSelect: true,
      showActionColumn: false,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      showIndexColumn: true,
      actionColumn: {
        width: 120,
      },
    },
    exportConfig: {
      name: '清单名录-备案地块清单',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });
  const isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法
  const [registerTable, { reload }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;
  //编辑事件
  function handleEdit() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择要修改的地块');
      return;
    }
    openModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  // 提交事件
  async function tiji() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (selectedRows.value[0].jxsmdc && selectedRows.value[0].sfthxg) {
      createMessage.info('数据还未进行填报');
      return;
    }
    try {
      await submitWrdkdc({
        wrdkid: selectedRows.value[0].wrdkid,
        jxsmdc: selectedRows.value[0].jxsmdc,
        sfthxg: selectedRows.value[0].sfthxg,
      });
    } catch (error) {
      handleSuccess();
    }
  }
  //  删除
  async function del() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openDelModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  //成功回调
  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
  function handleView() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    const record: any = selectedRows.value[0];
    if (!record.dclx) {
      open.value = true;
      return;
    }
    router.push(`/dkgl/investigate/modal/lookDataFilling?wrdkid=${selectedRowKeys.value[0]}`);
  }
  // // 点击填报资料
  // async function showDetail() {
  //   if (selectedRowKeys.value.length == 0) {
  //     createMessage.info('请选择需要处理的内容，每次只能选中一条');
  //     return;
  //   }
  //   const res = await existDclx({ wrdkid: selectedRowKeys.value[0] });
  //   if (res) {
  //     router.push(`/dkgl/investigate/modal/DataFilling?wrdkid=${selectedRowKeys.value[0]}`);
  //   } else {
  //     visible.value = true;
  //   }
  // }
</script>
<style scoped></style>
