<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="Tj = true" v-auth="'dkdc-tbz-tj'">提交</a-button>
        <a-button type="primary" @click="showDetail" v-auth="'dkdc-tbz-zltb'">资料填报</a-button>
        <a-button type="primary" @click="showLookModal" v-auth="'dkdc-tbz-jbxx'">查看基本信息</a-button>
        <a-button type="primary" @click="showModal(2)" v-auth="'dkdc-tbz-tbfsbg'">填报方式变更</a-button>
      </template>
    </BasicTable>
    <!-- 资料填报前置 -->
    <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="radioStyle" value="1">初步调查（没有超标）</a-radio>
        <a-radio :style="radioStyle" value="2">详细调查</a-radio>
        <a-radio :style="radioStyle" 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>
    <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="Tj" title="信息" centered @ok="submit" :bodyStyle="{ 'padding-left': '30px' }" :width="700">
      <template #footer>
        <div>
          <a-button type="primary" @click="submit">我已了解，继续提交</a-button>
        </div>
      </template>
      <div class="py-10 pl-5">
        按照《建设用地土壤污染风险管控和修复从业单位和个人执业情况信用记录管理办法(试行)》第十五条，
        土壤污染状况调查、土壤污染风险评估、风险管控方案编制、修复方案编制、风险管控效果评估、修复效果评估、
        风险管控施工、修复施工、工程监理、土壤和地下水监测的从业单位，应当在项目实施所涉及的土壤污染状况调查报告、
        风险评估报告、效果评估报告，以及风险管控与修复方案依据《中华人民共和国土壤污染防治法》提交评审或备案前，
        在信用记录系统记录单位和个人业绩情况信息。
      </div>
    </a-modal>
  </div>
</template>

<script lang="ts" name="dkgl-investigate-Filling" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { columns, searchFormSchema, useSuspectsData, useCategoryData } from './data';
  import { fillingList, getImportUrl, getExportUrl } from './api';
  import { existDclx, updateDclx, submitWrdkdc } from '/@/api/common/api';
  import { useRouter } from 'vue-router';

  const { createMessage } = useMessage();
  let router = useRouter();

  // 单选框组
  const flg = ref(1);
  const visible = ref<boolean>(false);
  const bgvisible = ref<boolean>(false);
  const noChange = ref<boolean>(false);
  let Tj = ref(false);

  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);
  }
  const selectdclx = ref<number>(1);
  const radioStyle = reactive({
    display: 'block',
    height: '30px',
    lineHeight: '30px',
  });
  function showLookModal() {
    // openLookRoleDesc(true);
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    router.push(`/dkgl/investigate/modal/lookDataFilling?wrdkid=${selectedRowKeys.value[0]}`);
  }

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '地块管理-填报中',
      api: fillingList,
      columns,
      canResize: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      showActionColumn: false,
      rowSelection: { type: 'radio' },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    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;

  // 点击提交
  async function submit() {
    await submitWrdkdc({
      wrdkid: selectedRows.value[0].wrdkid,
      jxsmdc: selectedRows.value[0].jxsmdc,
      sfthxg: selectedRows.value[0].sfthxg,
    });
    Tj.value = false;
    reload();
  }
</script>
<style scoped></style>
