<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="showLookModal" v-auth="'dkdc-tbz-jbxx'">查看基本信息</a-button>
        <a-button type="primary" @click="showDetail" v-auth="'dkdc-tbz-zltb'">资料填报</a-button>
        <a-button type="primary" @click="showModal(2)" v-auth="'dkdc-tbz-tbfsbg'">填报方式变更</a-button>
        <a-button type="primary" @click="submit" v-auth="'dkdc-tbz-tj'">提交</a-button>
        <a-button type="primary" @click="onExportXls" v-auth="'dkdc-tbz-dc'">导出</a-button>
        <a-button type="primary" @click="handelStep" v-auth="'dkdc-tbz-lct'">流程图</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" style="line-height: 22px">初步调查（没有超标）</a-radio>
        <a-radio :style="radioStyle" value="2" style="line-height: 22px; margin: 15px 0">详细调查</a-radio>
        <a-radio :style="radioStyle" value="3" style="line-height: 22px">初步调查+详细调查</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>
      <dksumitTip />
    </a-modal>
    <!-- 流程图 -->
    <Step @register="registerModal" />
  </div>
</template>

<script lang="ts" name="dkgl-investigate-Filling-tb" 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, getExportUrl } from './api';
  import { existDclx, updateDclx, submitWrdkdc } from '/@/api/common/api';
  import Step from '/@/components/customComponent/steps/step.vue';
  import { useModal } from '/@/components/Modal';
  import { useRouter } from 'vue-router';
  import dksumitTip from '../components/dksumitTip.vue';
  import { chTip } from '@/utils/checkTip';

  const { createMessage } = useMessage();
  let router = useRouter();
  const [registerModal, { openModal }] = useModal();
  // 单选框组
  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 (!chTip(selectedRows.value)) 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;
    }
  };
  // 点击查看流程图
  function handelStep() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  // 点击填报资料
  async function showDetail() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (selectedRows.value[0].tbjdbm != 'TH') {
      if (!chTip(selectedRows.value)) 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;
    }
    // if (!chTip(selectedRows.value)) return;
    router.push(`/dkgl/investigate/modal/ViewData?wrdkid=${selectedRowKeys.value[0]}`);
  }

  //注册table数据
  const { tableContext, onExportXls } = 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,
          {
            label: '阶段内状态',
            field: 'jdnzt',
            component: 'Input',
            defaultValue: 'T1',
            show: false,
          },
        ],
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '地块管理-填报中',
      url: getExportUrl,
    },
  });
  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>
