<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>
    <!-- <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="资料填报" width="80%" @ok="handleSubmit" destroyOnClose> -->
    <a-collapse v-model:activeKey="activeKey" :bordered="false" class="!bg-[#fff]">
      <a-collapse-panel key="1" header="地块信息" :forceRender="true">
        <template #extra><a-button type="primary" @click.prevent="DkFormAddEdit($event)">保存</a-button></template>
        <DkForm :data="dsInfor" ref="childrenOne" @getMj="getMj" />
        <!-- <DkForm :data="dsInfor" ref="childrenOne" @getMj="getMj" @getDyjddcqk="getDyjddcqk" /> -->
      </a-collapse-panel>

      <a-collapse-panel key="2" v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 1" header="初步调查报告基本信息" :forceRender="true">
        <suBainForm
          ref="chilrenReport"
          @getjxsmdc="getjxsmdc"
          @getcbdcfs="getcbdcfs"
          :data="dsInfor.cbdc"
          :dclx="dsInfor.wrdkjbxxb.dclx"
          :dyjddcqk="dyjddcqk"
        />
        <template #extra><a-button type="primary" @click.prevent="saveReport($event)">保存</a-button></template>
        <div
          >初步调查污染物信息<span class="text-red">
            [提示: 检测项编号请参考字典，点击
            <a-button type="primary" @click.prevent="down('/upload/static/检测项编号字典表.xlsx', '检测项编号字典表')">下载</a-button>
            查看]</span
          ></div
        >
        <CbInfor :wrdkid="dsInfor.wrdkjbxxb.wrdkid" />
      </a-collapse-panel>

      <a-collapse-panel
        header="初步调查报告编制单位基本信息"
        v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 1"
        :forceRender="true"
        v-for="(item, index) in cbdcBzdw"
        :key="index + 5"
      >
        <unitMessageForm :isRead="!bzdw" :ref="(el) => childRef(el, index)" upload=" " :data="item" />
        <template #extra>
          <a-button type="primary" @click.prevent="addUnitMessageFormList($event)" :disabled="!bzdw">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="delMessageFormList($event, index)" :disabled="!bzdw">删除</a-button>
          <a-button type="primary" @click.prevent="suBainFormAddEdit($event, index)" :disabled="!bzdw" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>

      <a-collapse-panel
        header="初步调查检测单位信息"
        v-if="(dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 1) && jcdw"
        :forceRender="true"
        v-for="(item, index) in cbjcdw"
        :key="index + 8"
      >
        <unitMessageJcForm
          :isRead="!jcdw"
          upload="检测单位"
          :dkxx="{ wrdkid: dsInfor.wrdkjbxxb.wrdkid, scjdbm: 'S1' }"
          :ref="(el) => childRefjc(el, index)"
          :data="item"
        />
        <template #extra>
          <a-button type="primary" @click.prevent="addDetectionMessageFormList($event)" :disabled="!jcdw">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="deleJcdw($event, index)" :disabled="!jcdw">删除</a-button>
          <a-button type="primary" @click.prevent="saveJcdw($event, index)" :disabled="!jcdw" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>
      <a-collapse-panel
        header="详细调查报告基本信息"
        :forceRender="true"
        v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 2"
        key="100000"
      >
        <suBainForm ref="chilrenxxReport" :data="dsInfor.xxdc" upload=" " :dclx="dsInfor.wrdkjbxxb.dclx" :labelFirst="'详细'" />
        <template #extra><a-button type="primary" @click.prevent="saveXx($event)">保存</a-button></template>
      </a-collapse-panel>
      <a-collapse-panel
        header="详细调查报告编制单位基本信息"
        v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 2"
        :forceRender="true"
        v-for="(item, index) in xxdcBzdw"
        :key="index + 6"
      >
        <unitMessageForm :ref="(el) => childRefxxbzdw(el, index)" :data="item" />
        <template #extra>
          <a-button type="primary" @click.prevent="addXxbzdw($event)">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="delXxbzdw($event, index)">删除</a-button>
          <a-button type="primary" @click.prevent="saveXxbzdw($event, index)" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>

      <a-collapse-panel
        header="详细调查检测单位信息"
        v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 2"
        :forceRender="true"
        v-for="(item, index) in xxjcdw"
        :key="index + 7"
      >
        <unitMessageJcForm
          :ref="(el) => childRefxxjc(el, index)"
          upload="检测单位"
          :dkxx="{ wrdkid: dsInfor.wrdkjbxxb.wrdkid, scjdbm: 'S2' }"
          :data="item"
        />
        <template #extra>
          <a-button type="primary" @click.prevent="addxxjcdw($event)" :disabled="isRead">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="dexxxjcdw($event, index)">删除</a-button>
          <a-button type="primary" @click.prevent="savexxJcdw($event, index)" :disabled="isRead" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>

      <a-collapse-panel header="土壤和地下水环境质量信息" :forceRender="true" key="120000">
        <EditTrwr
          ref="chilrenTr"
          @gettrMj="gettrMj"
          :dclx="dsInfor.wrdkjbxxb.dclx"
          :isRead="trxx"
          :data="dsInfor?.trhjzlxx"
          :sfwrdk="dsInfor?.xxdc?.sfwrdk"
        />
        <template #extra><a-button type="primary" @click.prevent="saveTr($event)">保存</a-button></template>
        <div
          >土壤环境检测数据<span class="text-red ml-3">
            [提示: 检测项编号请参考字典，点击
            <a-button type="primary" @click.prevent="down('/upload/static/检测项编号字典表.xlsx', '检测项编号字典表')">下载</a-button>
            查看]</span
          ></div
        >
        <PointInfor :data="{ wrdkid: dsInfor.wrdkjbxxb.wrdkid, pointType: 'solid', scjdbm: 'S2', faId: '' }" />
        <div
          >地下水环境检测数据<span class="text-red">
            [提示: 检测项编号请参考字典，点击
            <a-button type="primary" @click.prevent="down('/upload/static/检测项编号字典表.xlsx', '检测项编号字典表')">下载</a-button>
            查看]</span
          ></div
        >
        <PointInfor :data="{ wrdkid: dsInfor.wrdkjbxxb.wrdkid, pointType: 'water', scjdbm: 'S2', faId: '' }" />
      </a-collapse-panel>
      <a-collapse-panel header="敏感受体信息" :forceRender="true" key="130000">
        <EditMgst ref="chilrenMgst" :data="dsInfor?.mgst" :isRead="mgxx" />
        <template #extra><a-button type="primary" @click.prevent="saveMgst($event)">保存</a-button></template>
      </a-collapse-panel>

      <a-collapse-panel header="初步调查文件上传" :forceRender="true" v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 1" key="140000">
        <CdUpload :data="{ ...dsInfor.cbdc, scjdbm: 'S1' }" :objec="[{ name: '公示照片' }, { name: '初步调查报告' }]" />
      </a-collapse-panel>
      <a-collapse-panel header="详细调查文件上传" :forceRender="true" v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 2" key="150000">
        <CdUpload labelFirst="详细调查" :objec="[{ name: '公示照片' }, { name: '详细调查报告' }]" :data="{ ...dsInfor.xxdc, scjdbm: 'S2' }" />
      </a-collapse-panel>
      <a-collapse-panel header="填报进度" v-if="dsInfor.lzjd" :forceRender="true" key="150001">
        <fillProgress :List="dsInfor.lzjd"></fillProgress>
      </a-collapse-panel>
    </a-collapse>
  </div>
  <!-- 点击初步调查报告弹框提示 -->
  <a-modal
    v-model:visible="cbdcShow"
    title="提示"
    @ok="cbdcShow = false"
    @cancel="
      () => {
        cbdcShow = false;
      }
    "
    :footer="null"
  >
    <p class="p-8 text-base">土壤污染状况调查超标地块需进行详细调查，请变更调查方式为“详细调查”或“初步调查+详细调查”</p>
  </a-modal>
  <!-- </BasicDrawer> -->
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import DkForm from '/@/components/customComponent/dkForm.vue';
  import suBainForm from '/@/components/customComponent/suBainForm.vue';
  import unitMessageForm from '/@/components/customComponent/unitMessageForm.vue';
  import unitMessageJcForm from '@/components/customComponent/unitMessageJcForm.vue';
  import EditMgst from '/@/components/customComponent/EditMgst.vue';
  import EditTrwr from '/@/components/customComponent/EditTrwr.vue';
  import CdUpload from '/@/components/customComponent/CdUpload.vue';
  import fillProgress from '/@/components/customComponent/fillProgress.vue';
  import CbInfor from '/@/components/customComponent/CbInfor.vue';

  import PointInfor from '/@/components/customComponent/PointInfor.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadFile } from '/@/api/common/api';

  import {
    plotDetails,
    updateJbxx,
    updateBzdw,
    updateJcdw,
    delBzdw,
    delJcdw,
    updateCbdc,
    updateTrhjzlxx,
    updateXxdc,
    updateMgst,
  } from '/@/api/common/api';
  import { useRouter } from 'vue-router';
  import { nextTick } from 'vue';

  const { createMessage } = useMessage();
  let router = useRouter();
  const wrdkid = ref(router.currentRoute.value.query.wrdkid);
  const saveFlg = ref(true);
  let cbdcBzdw = ref([{ id: null }]);
  let xxdcBzdw = ref([{ id: null }]);
  let cbjcdw = ref([{ id: null }]);
  let xxjcdw = ref([{ id: null }]);
  let cbdcShow = ref(false);
  const bzdw = ref(true);
  const jcdw = ref(true);
  const trxx = ref(true);
  const mgxx = ref(true);
  const cbdcfs = ref(1);
  const dyjdIs = ref(false);
  const sfDc = ref(false);

  const zdmj = ref(''); //占地面积
  const trmj = ref(''); //获取土壤面积

  // 获取地块信息
  const dsInfor = ref<any>({
    jcdw: [],
    bzdw: [],
    cbdc: {},
    xxdc: {},
    wrdkjbxxb: {},
  });
  async function getdkInfor() {
    dsInfor.value = await plotDetails({
      wrdkid: wrdkid.value,
      scjdbm: 'S2',
    });
    saveFlg.value = true;
    // 判断初步调查是否进行书面调查选是情况
    if (dsInfor.value.wrdkjbxxb.dclx == 3 || (dsInfor.value.wrdkjbxxb.dclx == 1 && dsInfor.value?.cbdc?.jxsmdc != 1)) {
      jcdw.value = true;
    } else {
      jcdw.value = false;
    }
    // jcdw
    // 判断是详细调查的还是初步调查的
    cbdcBzdw.value = [];
    xxdcBzdw.value = [];
    if (dsInfor.value.bzdw) {
      dsInfor.value.bzdw.forEach((item) => {
        if (item.scjdbm == 'S1') {
          cbdcBzdw.value.push(item);
        } else if (item.scjdbm == 'S2') {
          xxdcBzdw.value.push(item);
        }
      });
    }
    cbjcdw.value = [];
    xxjcdw.value = [];
    if (dsInfor.value.jcdw) {
      dsInfor.value.jcdw.forEach((item) => {
        if (item.scjdbm == 'S1') {
          cbjcdw.value.push(item);
        } else if (item.scjdbm == 'S2') {
          xxjcdw.value.push(item);
        }
      });
    }

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

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

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

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

  // 校验的处理
  function jyCl() {
    // 调查方式只能是自行调查 调查形势是初步或者详细调查
    // 需要添加一个调查方式的限制以及  是自行调查还是第三方调查
    cbdcfs.value = dsInfor.value?.cbdc?.cbdcfs;
    if (dsInfor.value?.wrdkjbxxb?.dclx == 1) {
      dyjddcqk.value = dsInfor.value?.wrdkjbxxb?.dyjddcqk;
      const arr = typeof dyjddcqk.value === 'string' ? dyjddcqk.value.split(',') : dyjddcqk.value;
      if (Array.isArray(arr)) {
        let result = arr.slice(0, 6).some((item) => item === '1') || (arr[6] && arr[6] !== '0');
        if (result) {
          dyjdIs.value = true; //说明里面有选是或者文本框有值
        } else {
          dyjdIs.value = false; //说明七个值里面都是否
        }
        if (cbdcfs.value == 1) {
          sfDc.value = true; //代表选中是
        } else {
          sfDc.value = false; //代表选中否
        }
        getcbdcfs(dyjddcqk.value);
      }
    } else if (dsInfor.value?.wrdkjbxxb?.dclx == 3) {
      if (cbdcfs.value === 0) {
        // 【初步调查报告基本信息】-必填
        // 【初步调查报告编制单位基本信息】-不能填写，但不隐掉
        // 【初步调查检测单位信息】-必填
        // 【详细调查报告基本信息】-必填
        // 【详细调查报告编制单位基本信息】-必填
        // 【详细调查检测单位信息】-必填
        // 【土壤环境质量信息】-必填
        // 【敏感受体信息】-必填
        // 【初步调查文件上传】-必填
        // 【详细调查文件上传】-必填
        bzdw.value = true;
      } else {
        bzdw.value = false;
      }
    }
  }
  // 获取占地面积
  function getMj(v) {
    zdmj.value = v;
  }
  // 获取土壤面积
  function gettrMj(v) {
    trmj.value = v;
  }
  // 获取第一阶段调查情况
  const isRead = ref(false);
  const dyjddcqk = ref();

  function getDyjddcqk(val) {
    // 自行组织调查：0 委托第三方调查：1
    dyjddcqk.value = val;
    if (!val) return;
    let arr = typeof val === 'string' ? val.split(',') : val;

    if (dsInfor.value?.wrdkjbxxb?.dclx == 1) {
      // 前6个任意一个选择是、最后一个输入框有值(输入0或者其他，进入if，输入框输入的数字是字符串)
      let result = arr.slice(0, 6).some((item) => item === '1') || (arr[6] && arr[6] !== '0');
      if (result) {
        dyjdIs.value = true; //说明里面有选是或者文本框有值
      } else {
        dyjdIs.value = false; //说明七个值里面都是否
      }
      modiyData(dyjdIs.value, sfDc.value);
    }
  }
  //是否只进行调查第一阶段
  function getjxsmdc(v) {
    console.log('*********是否只进行调查第一阶段', v, '*********调查方式', cbdcfs.value);
    // 不是自行调查的 就不需要判断
    if (cbdcfs.value === 0) {
      if (v == 1) {
        sfDc.value = true; //代表选中是
      } else {
        sfDc.value = false; //代表选中否
      }
      modiyData(dyjdIs.value, sfDc.value);
    } else {
      if (v == 1) {
        sfDc.value = true; //代表选中是
        jcdw.value = false;
      } else {
        sfDc.value = false; //代表选中否
        jcdw.value = true;
      }
      bzdw.value = true;

      trxx.value = true;
      mgxx.value = true;
    }
  }
  // 调查方式-点击是自行调查 还是第三方调查
  function getcbdcfs(v) {
    console.log('>>>>>>调查方式>>>>>>');
    cbdcfs.value = v;
    if (v === 0) {
      modiyData(dyjdIs.value, sfDc.value);
    } else {
      bzdw.value = true;
      jcdw.value = true;
      trxx.value = true;
      mgxx.value = true;
    }
  }
  // 监听第一节点调查以及是否选中第一节点调查的值
  function modiyData(val, val1) {
    console.log('--val--第一调查阶段情况-有选是或者文本框有值', val, '--val1--是否只进行调查第一阶段', val1);
    // 自行组织调查
    if (!val && !val1) {
      // “第一阶段调查情况7项都为【否】”且“是否只进行调查第一阶段选【否】”
      // 【初步调查报告基本信息】-必填
      // 【初步调查报告编制单位基本信息】-不能填写，但不隐掉
      // 【初步调查检测单位信息】-必填
      // 【土壤环境质量信息】-必填
      // 【敏感受体信息】-必填
      // 【初步调查文件上传】-必填
      bzdw.value = false;
      jcdw.value = true;
      trxx.value = true;
      mgxx.value = true;
    } else if (!val && val1) {
      // “第一阶段调查情况7项都为【否】”且“是否只进行调查第一阶段选【是】”
      // 【初步调查报告基本信息】-必填
      // 【初步调查报告编制单位基本信息】-不能填写，但不隐掉
      // 【初步调查检测单位信息】-不能填写，但不隐掉(系统原有逻辑）
      // 【土壤环境质量信息】-选填
      // 【敏感受体信息】-选填
      // 【初步调查文件上传】-必填
      bzdw.value = false;
      jcdw.value = false;
      trxx.value = false;
      mgxx.value = false;
    } else if (val) {
      // “第一阶段调查情况7项但凡有一项为【是】”且“是否只进行调查第一阶段必须为【否】”
      // 【初步调查报告基本信息】-必填
      // 【初步调查报告编制单位基本信息】-不能填写，但不隐掉
      // 【初步调查检测单位信息】-必填
      // 【土壤环境质量信息】-必填
      // 【敏感受体信息】-必填
      // 【初步调查文件上传】-必填
      bzdw.value = false;
      jcdw.value = true;
      trxx.value = true;
      mgxx.value = true;
    }
  }
  const childrenOne = ref<any>(null);

  const activeKey = ref(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '100000', '120000', '130000', '140000', '150000']);

  // 土壤环境检测
  const chilrenTr = ref<any>(null);
  async function saveTr(event) {
    event.stopPropagation();
    const res = await chilrenTr?.value.getFormValueTr();
    res.wrdkid = wrdkid.value;
    // 如同选的其他 不用校验
    if (!res.wcc && !res.cbSxz && !res.cbGzz) {
      createMessage.warn('请选择土壤受污染程度');
      return;
    }
    if (!res.wcc) {
      if (!dsInfor.value?.wrdkjbxxb.zdmj) {
        createMessage.warn('请先填写地块占地面积并保存地块信息');
        return;
      } else if (dsInfor.value?.wrdkjbxxb.zdmj < res.cbqymj) {
        createMessage.warn('土壤污染物超标区域面积不能超过地块占地面积');
        return;
      }
    }
    if (res.wcc == 1 || res.wcc) {
      res.wrwZjsGzz = '';
      res.wrwZjsSxz = '';
      res.wrwYjwGzz = '';
      res.wrwYjwSxz = '';
    }
    console.log(res, 'res');

    try {
      if (saveFlg.value) {
        saveFlg.value = false;
        if (dsInfor?.value?.trhjzlxx?.id) res.id = dsInfor.value.trhjzlxx.id;
        const id = await updateTrhjzlxx(res);
        if (!dsInfor.value.trhjzlxx) {
          dsInfor.value.trhjzlxx = res;
          dsInfor.value.trhjzlxx.id = id;
        }
        saveFlg.value = true;
        // getdkInfor()
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 修改基本信息
  async function DkFormAddEdit(event) {
    event.stopPropagation();
    const res = await childrenOne?.value.getFormValue();
    if (!res) return;
    res.wrdkid = res.wrdkid || wrdkid.value;
    if (res?.hylb) delete res.hylb;
    // if (trmj.value) {
    //   if (res.zdmj < trmj.value) {
    //     createMessage.warn('土壤污染物超标区域面积不能超过地块占地面积');
    //     return;
    //   }
    // }
    await updateJbxx(res);
    // 防止下面的数据没有保存给更新了
    dsInfor.value = await plotDetails({
      wrdkid: wrdkid.value,
      scjdbm: 'S2',
    });
    // getdkInfor();
  }
  // 初步调查报告
  const chilrenReport = ref<any>(null);
  async function saveReport(event) {
    event.stopPropagation();
    const res = await chilrenReport?.value.getFormValueReport();
    console.log(res, 'res');
    const reg = /^(http|https):\/\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|localhost|[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,})(:\d{2,5})?(\/\S*)?$/;
    if (res.sfwrdk == 1 && dsInfor?.value?.dklx == 1) {
      cbdcShow.value = true;
      return;
    }

    if (res.gsfs == 1) {
      if (!res.gswz) {
        createMessage.warn('请填写网址');
        return;
      }
      if (!reg.test(res.gswz)) {
        createMessage.warn('请输入正确的网址');
        return;
      }
    }
    if (new Date(res.fbsj) > new Date(res.wcsj)) {
      createMessage.warn('调查开始日期不能大于调查结束日期');
      return;
    }
    if (dsInfor.value.wrdkjbxxb.dclx === '3') {
      res.jxsmdc = '1'
    }

    await updateCbdc(res);
  }
  // 详细调查
  const chilrenxxReport = ref<any>(null);
  async function saveXx(event) {
    event.stopPropagation();
    const res = await chilrenxxReport?.value.getFormValueReport();
    const reg = /^(http|https):\/\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|localhost|[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z]{2,})(:\d{2,5})?(\/\S*)?$/;

    if (res.gsfs == 1) {
      if (!res.gswz) {
        createMessage.warn('请输入网址');
        return;
      }
      if (!reg.test(res.gswz)) {
        createMessage.warn('请输入正确的网址');
        return;
      }
    }
    if (new Date(res.fbsj) > new Date(res.wcsj)) {
      createMessage.warn('调查开始日期不能大于调查结束日期');
      return;
    }
    await updateXxdc(res);
  }
  // 敏感受体
  const chilrenMgst = ref<any>(null);
  async function saveMgst(event) {
    event.stopPropagation();
    try {
      if (saveFlg.value) {
        saveFlg.value = false;
        const res = await chilrenMgst?.value.getFormValueMgst();
        res.wrdkid = wrdkid.value;
        if (dsInfor?.value?.mgst?.id) res.id = dsInfor.value.mgst.id;
        const id = await updateMgst(res);
        if (!dsInfor.value.mgst) {
          dsInfor.value.mgst = {};
          dsInfor.value.mgst.id = id;
        }
        saveFlg.value = true;
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }

  // 初步编制单位开始
  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();
    cbdcBzdw.value.push({ id: null });
    nextTick(() => {
      callChildMethod(cbdcBzdw.value.length - 1);
    });
  }
  async function delMessageFormList(event, index) {
    event.stopPropagation();
    if (cbdcBzdw.value[index].id) {
      await delBzdw({ id: cbdcBzdw.value[index].id });
    }
    cbdcBzdw.value.splice(index, 1);
    childrenBzd.value.splice(index, 1);
  }
  async function suBainFormAddEdit(event, index) {
    event.stopPropagation();
    try {
      const res = await childrenBzd.value[index].getFormValueBz();
      res.wthtje = String(res.wthtje);
      // 详细编制传S2初步编制单位S1
      res.scjdbm = 'S1';
      // 新增的时候没有这个值
      res.jgtype = 'bzdw';
      res.wrdkid = wrdkid.value;
      if (saveFlg.value) {
        saveFlg.value = false;
        const id = await updateBzdw(res);
        if (id != '成功') {
          cbdcBzdw.value[index] = res;
          cbdcBzdw.value[index].id = id;
        }
        saveFlg.value = true;
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 初步编制单位结束
  // 详细编制单位开始
  const childrenCbBzd = ref<any>([]);
  const childRefxxbzdw = (el, index) => {
    if (el) childrenCbBzd.value[index] = el;
  };
  const callChildxx = (index) => {
    childrenCbBzd.value[index].getFormValueBz();
  };
  async function delXxbzdw(event, index) {
    event.stopPropagation();
    if (xxdcBzdw.value[index].id) {
      await delBzdw({ id: xxdcBzdw.value[index].id });
    }
    xxdcBzdw.value.splice(index, 1);
    childrenCbBzd.value.splice(index, 1);
    // getdkInfor();
  }
  function addXxbzdw(event) {
    event.stopPropagation();
    xxdcBzdw.value.push({ id: null });
    nextTick(() => {
      callChildxx(xxdcBzdw.value.length - 1);
    });
  }
  async function saveXxbzdw(event, index) {
    event.stopPropagation();
    try {
      const res = await childrenCbBzd.value[index].getFormValueBz();
      res.wthtje = String(res.wthtje);
      // 详细编制传S2初步编制单位S1
      res.scjdbm = 'S2';
      res.jgtype = 'bzdw';
      // 新增的时候没有这个值
      res.wrdkid = wrdkid.value;
      if (saveFlg.value) {
        saveFlg.value = false;
        const id = await updateBzdw(res);
        if (id != '成功') {
          xxdcBzdw.value[index] = res;
          xxdcBzdw.value[index].id = id;
        }
        saveFlg.value = true;
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 初步检测单位
  const childrenjc = ref<any>([]);
  const childRefjc = (el, index) => {
    if (el) childrenjc.value[index] = el;
  };
  const callChildMethodjc = (index) => {
    childrenjc.value[index].getFormValueBz();
  };
  // 新增
  function addDetectionMessageFormList(event) {
    event.stopPropagation();
    cbjcdw.value.push({ id: null });
    nextTick(() => {
      callChildMethodjc(cbjcdw.value.length - 1);
    });
  }
  // 删除
  async function deleJcdw(event, index) {
    event.stopPropagation();
    if (cbjcdw.value[index].id) await delJcdw({ id: cbjcdw.value[index].id });
    cbjcdw.value.splice(index, 1);
    childrenjc.value.splice(index, 1);
  }
  // 保存
  async function saveJcdw(event, index) {
    event.stopPropagation();
    try {
      const res = await childrenjc.value[index].getFormValueBz();
      res.scjdbm = 'S1';
      res.jgtype = 'jcdw';
      res.wthtje = String(res.wthtje);
      // 新增的时候没有这个值
      res.wrdkid = wrdkid.value;
      if (saveFlg.value) {
        saveFlg.value = false;
        const id = await updateJcdw(res);
        if (id != '成功') {
          cbjcdw.value[index] = res;
          cbjcdw.value[index].id = id;
        }
        saveFlg.value = true;
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 详细检测单位
  const childrenxxjc = ref<any>([]);
  const childRefxxjc = (el, index) => {
    if (el) childrenxxjc.value[index] = el;
  };
  const callChildMethodxxjc = (index) => {
    childrenxxjc.value[index].getFormValueBz();
  };
  function addxxjcdw(event) {
    event.stopPropagation();
    xxjcdw.value.push({ id: null });
    nextTick(() => {
      callChildMethodxxjc(xxjcdw.value.length - 1);
    });
  }
  // 删除
  async function dexxxjcdw(event, index) {
    event.stopPropagation();
    if (xxjcdw.value[index].id) await delJcdw({ id: xxjcdw.value[index].id });
    xxjcdw.value.splice(index, 1);
    childrenxxjc.value.splice(index, 1);
  }
  // 保存
  async function savexxJcdw(event, index) {
    event.stopPropagation();
    try {
      const res = await childrenxxjc.value[index].getFormValueBz();
      res.scjdbm = 'S2';
      res.jgtype = 'jcdw';
      res.wthtje = String(res.wthtje);
      // 新增的时候没有这个值
      res.wrdkid = wrdkid.value;
      if (saveFlg.value) {
        saveFlg.value = false;
        const id = await updateJcdw(res);
        if (id != '成功') {
          xxjcdw.value[index] = res;
          xxjcdw.value[index].id = id;
        }
        saveFlg.value = true;
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  function down(url, name) {
    let fileExtension = url.split('.').pop();
    downloadFile('/sys/common/static' + url, `${name}.${fileExtension}`);
  }
</script>

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