<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="handleView" v-auth="'qdml-gzml-ckxx'">查看基本信息</a-button>
        <a-button type="primary" @click="showDetail" v-auth="'qdml-gzml-zltb'">资料填报</a-button>
        <a-button type="primary" @click="showModal(2)" v-auth="'qdml-gzml-tbfsbg'">填报方式变更</a-button>
        <a-button type="primary" @click="move" v-auth="'qdml-gzml-ycygz'">移出预关注</a-button>
        <a-button type="primary" v-auth="'qdml-gzml-export'" @click="onExportXls">导出</a-button>
      </template>
    </BasicTable>
  </div>
  <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>
  <!-- 填报方式变更前置 -->
  <a-modal
    v-model:visible="noChange"
    title="信息"
    centered
    @ok="noChange = false"
    :bodyStyle="{ 'padding-left': '30px' }"
    :width="400"
    :okText="'确定'"
    :cancelText="'取消'"
  >
    <template #footer>
      <a-button type="primary" @click="noChange = false">确定</a-button>
    </template>
    <div class="py-10 pl-5"> 还没有选定调查类型，不能变更 </div>
  </a-modal>
  <!-- 资料填报前置 -->
  <a-modal
    v-model:visible="visible"
    title="请选择土壤污染状态调查填报方式："
    centered
    @ok="handleOk"
    :bodyStyle="{ 'padding-left': '30px' }"
    :width="500"
    :height="400"
    :okText="'确定'"
    :cancelText="'取消'"
  >
    <a-radio-group v-model:value="selectdclx">
      <a-radio :style="{ display: 'block', height: '30px', lineHeight: '30px' }" value="1">初步调查（没有超标）</a-radio>
      <a-radio :style="{ display: 'block', height: '30px', lineHeight: '30px' }" value="2">详细调查</a-radio>
      <a-radio :style="{ display: 'block', height: '30px', lineHeight: '30px' }" value="3">初步调查+详细调查</a-radio>
    </a-radio-group>
  </a-modal>
  <!-- 点击确定出现的弹框变更的时候 -->
  <a-modal
    v-model:visible="bgvisible"
    title="信息"
    centered
    @ok="handlebg"
    :bodyStyle="{ 'padding-left': '30px' }"
    :width="400"
    :okText="'确定'"
    :cancelText="'取消'"
  >
    <div class="py-10 pl-5"> 本次变更，将会丢失地块调查填报的信息。请确认 </div>
  </a-modal>
</template>

<script lang="ts" name="directory-gzml-gzml" setup>
  import { ref } from 'vue';
  import { BasicTable } 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 { existDclx, updateDclx } from '/@/api/common/api';
  import MoveinModal from '../components/MoveinModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  const { createMessage } = useMessage();
  let router = useRouter();
  //注册model
  const [registerMoveModal, { openModal: openMoveModal }] = useModal();
  //注册table数据
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '清单名录-预关注地块清单',
      api: BahygzList,
      columns,
      canResize: false,
      clickToRowSelect: true,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'wrdkid',
      rowSelection: { type: 'radio' },
      showActionColumn: false,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      showIndexColumn: true,
      actionColumn: {
        width: 120,
      },
    },
    exportConfig: {
      name: '清单名录-预关注地块清单',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
    },
  });
  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 isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法
  const [registerTable, { reload }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;

  // 移入关注
  function move() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择要移入关注的数据');
      return;
    }
    openMoveModal(true, {
      record: {
        ...selectedRows.value[0],
        type: 'mouse',
      },
      isUpdate: true,
      showFooter: true,
    });
  }
  //查看基本信息
  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/ViewData?wrdkid=${selectedRowKeys.value[0]}`);
  }
  //成功回调
  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
  //填报方式变更
  const showModal = async (v) => {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!selectedRows.value[0].dclx) {
      noChange.value = true;
    } else {
      selectdclx.value = selectedRows.value[0].dclx;
      flg.value = v;
      visible.value = true;
    }
  };
  const handleOk = async () => {
    if (flg.value != 2) {
      visible.value = false;
      await updateDclx({ wrdkid: selectedRowKeys.value[0], dclx: selectdclx.value });
      router.push(`/dkgl/investigate/modal/DataFilling?wrdkid=${selectedRowKeys.value[0]}`);
    } else {
      bgvisible.value = true;
    }
  };
  // 点击填报资料
  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;
    }
  }
  // 点击变更确认提示弹框
  async function handlebg() {
    const res = await updateDclx({ wrdkid: selectedRowKeys.value[0], dclx: selectdclx.value });
    visible.value = false;
    bgvisible.value = false;
    createMessage.info(res);
  }
</script>
<style scoped></style>
