<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" @getDyjddcqk="getDyjddcqk" />
      </a-collapse-panel>

      <a-collapse-panel key="2" v-if="dsInfor.wrdkjbxxb.dclx == 3 || dsInfor.wrdkjbxxb.dclx == 1" header="初步调查报告基本信息" :forceRender="true">
        {{ dyjddcqk }}
        <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>
      </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)" :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"
        :forceRender="true"
        v-for="(item, index) in cbjcdw"
        :key="index + 8"
      >
        <unitMessageForm
          :isRead="!jcdw"
          :upload="index + 1"
          :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" :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"
      >
        <unitMessageForm
          :ref="(el) => childRefxxjc(el, index)"
          :upload="index + 1"
          :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" :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>
      </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" :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" />
      </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>
  <!-- </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 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 { useMessage } from '/@/hooks/web/useMessage';
  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 }]);

  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 dsInfor = ref<any>({
    jcdw: [],
    bzdw: [],
    cbdc: {},
    xxdc: {},
    wrdkjbxxb: {},
  });

  async function getdkInfor() {
    dsInfor.value = await plotDetails({
      wrdkid: wrdkid.value,
      scjdbm: 'S2',
    });
    saveFlg.value = true;
    // 判断是详细调查的还是初步调查的
    cbdcBzdw.value = [];
    xxdcBzdw.value = [];
    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 = [];
    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 });

    // 调查方式只能是自行调查 调查形势是初步或者详细调查
    // 需要添加一个调查方式的限制以及  是自行调查还是第三方调查
    cbdcfs.value = dsInfor.value?.cbdc?.cbdcfs;
    if (dsInfor.value?.wrdkjbxxb?.dclx == 1) {
      dyjddcqk.value = dsInfor.value?.wrdkjbxxb?.dyjddcqk;
      const arr = dsInfor.value?.wrdkjbxxb?.dyjddcqk.split(',');
      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;
      }
    }
  }
  getdkInfor();

  // 获取第一阶段调查情况
  const isRead = ref(false);
  const dyjddcqk = ref();

  function getDyjddcqk(val) {
    // 自行组织调查：0 委托第三方调查：1
    dyjddcqk.value = val;
    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; //代表选中是
      } else {
        sfDc.value = false; //代表选中否
      }
      bzdw.value = true;
      jcdw.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;
    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 = {};
          dsInfor.value.trhjzlxx.id = id;
        }
        saveFlg.value = true;
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 修改基本信息
  async function DkFormAddEdit(event) {
    event.stopPropagation();
    const res = await childrenOne?.value.getFormValue();
    if (res?.hylb) delete res.hylb;
    await updateJbxx(res);
  }
  // 初步调查报告
  const chilrenReport = ref<any>(null);
  async function saveReport(event) {
    event.stopPropagation();
    const res = await chilrenReport?.value.getFormValueReport();
    await updateCbdc(res);
  }
  // 详细调查
  const chilrenxxReport = ref<any>(null);
  async function saveXx(event) {
    event.stopPropagation();
    const res = await chilrenxxReport?.value.getFormValueReport();

    if (res.projectType == 1) {
      if (!res.gswz) createMessage.warn('请输入网址');
    }
    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;
    }
  }
</script>

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