<template>
  <div class="bg-[#fff]">
    <a-page-header style="border: 1px solid rgb(235, 237, 240)" @back="router.go(-1)">
      <template #backIcon>
        <div><a-button type="primary">返回</a-button></div>
      </template>
      <template #title>
        <div></div>
      </template>
    </a-page-header>
    <a-collapse v-model:activeKey="activeKey" :bordered="false">
      <a-collapse-panel key="1" header="地块信息" :forceRender="true">
        <DkTable :data="dsInfor.wrdkjbxxb" />
      </a-collapse-panel>

      <a-collapse-panel key="2" header="管控及修复方案基本信息" :forceRender="true">
        <suBainForm ref="chilrenReport" labelFirst="管控及修复" :jslx="dsInfor?.gkxf?.jslx || ''" :data="dsInfor?.gkxf" />
        <template #extra><a-button type="primary" @click.prevent="saveReport($event)">保存</a-button></template>
      </a-collapse-panel>

      <a-collapse-panel header="管控及修复方案编制单位基本信息" :forceRender="true" v-for="(item, index) in unitMessageFormList" :key="index + 4">
        <unitMessageForm :ref="(el) => childRef(el, index)" :data="item" />
        <template #extra>
          <a-button type="primary" @click.prevent="addUnitMessageFormList($event)">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="delMessageFormList($event, index)">删除</a-button>
          <a-button type="primary" @click.prevent="suBainFormAddEdit($event, index)" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>

      <a-collapse-panel header="管控及修复措施信息" :forceRender="true" key="140000">
        <ControlInformationgkxf
          labelFirst="修复"
          ref="chilrenXfcs"
          :obj="[{ name: '开展修复土壤面积(m2)' }, { name: '修复土壤方量(m3)' }]"
          :data="dsInfor.gkxf"
        />
        <template #extra><a-button type="primary" @click.prevent="saveXfcs($event)">保存</a-button></template>
      </a-collapse-panel>
      <a-collapse-panel header="管控及修复文件上传" :forceRender="true" key="150000">
        <CdUpload labelFirst="管控与修复" :objec="[{ name: '公示截图' }, { name: '管控及修复方案' }]" :data="dsInfor.gkxf" />
      </a-collapse-panel>
      <a-collapse-panel header="填报进度" :forceRender="true" key="160000">
        <Progress />
      </a-collapse-panel>
    </a-collapse>
  </div>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import DkTable from '/@/components/customComponent/DkTable.vue';
  import suBainForm from '/@/components/customComponent/fxgk/ControlPlan.vue';
  // import ControlInformation from '/@/components/customComponent/gkpg/csInfor.vue';
  import ControlInformationgkxf from '/@/components/customComponent/gkpg/ControlInformationgkxf.vue';
  import unitMessageForm from '/@/components/customComponent/unitMessageForm.vue';
  import Progress from '/@/components/customComponent/fillProgress.vue';
  import CdUpload from '/@/components/customComponent/CdUpload.vue';
  import { plotDetails, updateBzdw, delBzdw, saveGkxf } from '/@/api/common/api';
  import { useRouter } from 'vue-router';
  import { nextTick } from 'vue';
  import { useMessage } from '@/hooks/web/useMessage';

  let router = useRouter();
  const saveFlg = ref(true);
  const { createMessage } = useMessage();
  const params = ref(router.currentRoute.value.query);
  let unitMessageFormList = ref([{ id: null }]);
  // 获取地块信息
  const dsInfor = ref<any>({
    jcdw: [],
    bzdw: [],
    cbdc: {},
    xxdc: {},
    wrdkjbxxb: {},
  });

  async function getdkInfor() {
    dsInfor.value = await plotDetails({
      wrdkid: params.value.wrdkid,
      id: params.value.id,
      pgtype: params.value.pgtype,
      scjdbm: 'S10',
    });
    saveFlg.value = true;
    unitMessageFormList.value = dsInfor.value.bzdw;

    if (unitMessageFormList.value.length == 0) unitMessageFormList.value.push({ id: null });
  }
  getdkInfor();

  const activeKey = ref(['1', '2', '3', '4', '5', '6', '7', '140000', '150000', '160000']);

  // 风险管控基本方案
  const chilrenReport = ref<any>(null);
  async function saveReport(event) {
    event.stopPropagation();
    const res = await chilrenReport?.value.getFormValueReport();
    res.pgtype = params.value.pgtype;
    if (res.gkfs == 1) {
      if (!res.gswz) {
        createMessage.warn('请填写网址');
        return;
      }
    }
    await saveGkxf(res);
  }
  //风险管控措施
  const chilrenXfcs = ref<any>(null);
  async function saveXfcs(event) {
    event.stopPropagation();
    const res = await chilrenXfcs?.value.getFormValueFxpg();
    res.pgtype = params.value.pgtype;
    if(res.kzfxgktrmj > dsInfor.value.wrdkjbxxb.zdmj){
        createMessage.warn('开展修复土壤面积不能超过地块占地面积');
        return false
    }
    if (res.gkcs == 2) {
      if (res.checkboxInputValue) {
        res.gkcsQt = res.checkboxValue + ',' + res.checkboxInputValue;
      } else {
        res.gkcsQt = res.checkboxValue;
      }
    }
    await saveGkxf(res);
  }
  // 编制单位开始
  const childrenBzd = ref<any>([]);
  const childRef = (el, index) => {
    if (el) childrenBzd.value[index] = el;
  };
  const callChildMethod = (index) => {
    childrenBzd.value[index].getFormValueBz();
  };
  function addUnitMessageFormList(event) {
    event.stopPropagation();
    unitMessageFormList.value.unshift({ id: null });
    nextTick(() => {
      callChildMethod(unitMessageFormList.value.length - 1);
    });
  }
  async function delMessageFormList(event, index) {
    event.stopPropagation();
    if (unitMessageFormList.value[index].id) {
      await delBzdw({ id: unitMessageFormList.value[index].id });
    }
    unitMessageFormList.value.splice(index, 1);
    childrenBzd.value.splice(index, 1);
    // getdkInfor();
  }
  async function suBainFormAddEdit(event, index) {
    event.stopPropagation();
    try {
      if (saveFlg.value) {
        saveFlg.value = false;
        const res = await childrenBzd.value[index].getFormValueBz();
        res.wthtje = String(res.wthtje);
        res.pgtype = params.value.pgtype;
        res.scjdbm = 'S10';
        res.jgtype = 'bzdw';
        // 新增的时候没有这个值
        res.wrdkid = params.value.wrdkid;
        const id = await updateBzdw(res);
        if (id != '成功') {
          unitMessageFormList.value[index] = res;
          unitMessageFormList.value[index].id = id;
        }
        saveFlg.value = true;
        // getdkInfor();
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 编制单位结束
</script>

<style lang="less" scoped>
  :deep(.ant-collapse-borderless) {
    width: 100%;
  }
</style>
