<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" v-if="dsInfor.wrdkjbxxb">
        <DkTable :data="dsInfor.wrdkjbxxb" />
      </a-collapse-panel>

      <a-collapse-panel key="2" header="修复效果评估报告基本信息" :forceRender="true">
        <suBainForm
          ref="chilrenReport"
          labelFirst="效果评估"
          :obj="[{ name: '修复效果评估报告' }, { name: '修复效果评估报告编制' }]"
          :data="dsInfor.xgpg"
        />
        <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" v-for="(item, index) in jcpgunitList" :key="index + 5">
        <unitMessageForm
          :ref="(el) => childjcRef(el, index)"
          upload="检测单位"
          :dkxx="{ wrdkid: dsInfor.wrdkjbxxb.wrdkid, scjdbm: 'S6' }"
          :data="item"
        />
        <template #extra>
          <a-button type="primary" @click.prevent="addjcpgunitList($event)">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="deljcpgunitList($event, index)">删除</a-button>
          <a-button type="primary" @click.prevent="sujcpgunitListt($event, index)" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>
      <a-collapse-panel header="修复效果评估实施单位基本信息" :forceRender="true" v-for="(item, index) in ssdw" :key="index + 9">
        <unitMessageForm :ref="(el) => childRefss(el, index)" :data="item" />
        <template #extra>
          <a-button type="primary" @click.prevent="addSs($event)">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="deless($event, index)">删除</a-button>
          <a-button type="primary" @click.prevent="savess($event, index)" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>
      <a-collapse-panel
        header="修复效果评估监理单位基本信息"
        :forceRender="true"
        v-for="(item, index) in jldw"
        :key="index + 12"
        :dkxx="{ wrdkid: dsInfor.wrdkjbxxb.wrdkid, scjdbm: 'S5' }"
      >
        <unitMessageForm :ref="(el) => childRefjl(el, index)" :data="item" upload="检测单位" title="监理" />
        <template #extra>
          <a-button type="primary" @click.prevent="addjl($event)">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="deljl($event, index)">删除</a-button>
          <a-button type="primary" @click.prevent="savejl($event, index)" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>
      <a-collapse-panel
        header="修复效果评估施工总结报告基本信息"
        :forceRender="true"
        v-for="(item, index) in sgzj"
        :key="index + 14"
        :dkxx="{ wrdkid: dsInfor.wrdkjbxxb.wrdkid, scjdbm: 'S6' }"
      >
        <unitMessageForm :ref="(el) => childRefsg(el, index)" :data="item" upload="检测单位" title="施工" />
        <template #extra>
          <a-button type="primary" @click.prevent="addsg($event)">添加</a-button>
          <a-button type="primary" class="ml-5" v-if="index != 0" @click.prevent="delsg($event, index)">删除</a-button>
          <a-button type="primary" @click.prevent="savesg($event, index)" style="margin-left: 10px">保存</a-button>
        </template>
      </a-collapse-panel>
      <a-collapse-panel header="修复措施信息" :forceRender="true" key="140000">
        <ControlInformation
          labelFirst="修复效果评估"
          ref="chilrenGkcs"
          :obj="[{ name: '实际修复范围(m²)' }, { name: '实际修复土壤方量(m³)' }]"
          :data="dsInfor.xgpg"
        />
        <template #extra><a-button type="primary" @click.prevent="addGKcs($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: 'S6', faId: param.id }" />
        <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: 'S6', faId: param.id }" />
      </a-collapse-panel>
      <a-collapse-panel header="修复效果评估文件上传" :forceRender="true" key="150000">
        <CdUpload
          labelFirst="修复效果评估"
          :dkxx="dsInfor.wrdkjbxxb"
          :objec="[{ name: '公示截图' }, { name: '评估报告' }]"
          :data="{ ...dsInfor.xgpg, scjdbm: 'S6', pgtype: param.pgtype }"
          @upsuccess="handleUpSuccess"
        />
      </a-collapse-panel>
      <a-collapse-panel header="填报进度" :forceRender="true" key="160000">
        <fillProgress :List="dsInfor?.lzjd"></fillProgress>
      </a-collapse-panel>
    </a-collapse>
  </div>
</template>
<script lang="ts" setup>
  import { ref, nextTick } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import DkTable from '/@/components/customComponent/DkTable.vue';
  import suBainForm from '/@/components/customComponent/gkpg/pgReport.vue';
  import ControlInformation from '/@/components/customComponent/gkpg/csInfor.vue';
  import unitMessageForm from '/@/components/customComponent/unitMessageForm.vue';
  import fillProgress from '/@/components/customComponent/fillProgress.vue';
  import CdUpload from '/@/components/customComponent/CdUpload.vue';
  import { plotDetails, updateBzdw, updateJcdw, delBzdw, delJcdw, saveXgpg } from '/@/api/common/api';
  import { useRouter } from 'vue-router';
  import PointInfor from '/@/components/customComponent/PointInfor.vue';
  import { downloadFile } from '/@/api/common/api';

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

  async function getdkInfor() {
    dsInfor.value = await plotDetails({
      wrdkid: param.value.wrdkid,
      id: param.value.id,
      scjdbm: 'S6',
      pgtype: param.value.pgtype,
    });
    saveFlg.value = true;
    unitMessageFormList.value = dsInfor.value.bzdw || [{ id: null }];
    jcpgunitList.value = dsInfor.value.jcdw || [{ id: null }];
    ssdw.value = dsInfor.value.ssdw || [{ id: null }];
    jldw.value = dsInfor.value.jldw || [{ id: null }];
    sgzj.value = dsInfor.value.sgzj || [{ id: null }];
    if (unitMessageFormList.value.length == 0) unitMessageFormList.value.push({ id: null });

    if (jcpgunitList.value.length == 0) jcpgunitList.value.push({ id: null });
    if (ssdw.value.length == 0) ssdw.value.push({ id: null });
    if (jldw.value.length == 0) jldw.value.push({ id: null });
    if (sgzj.value.length == 0) sgzj.value.push({ id: null });
  }
  getdkInfor();

  const handleUpSuccess = ()=>{
    // getdkInfor();
  }
  const activeKey = ref(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '140000', '150000', '160000']);

  // 风险管控基本方案
  const chilrenReport = ref<any>(null);
  async function saveReport(event) {
    event.stopPropagation();
    const res = await chilrenReport?.value.getFormValueFxpg();
    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 false;
      }
    }
    await saveXgpg(res);
  }
  // 修复措施
  const chilrenGkcs = ref<any>(null);
  async function addGKcs(event) {
    event.stopPropagation();
    const res = await chilrenGkcs?.value.getFormValueFxpg();
    if (res.sjxffw > dsInfor.value.wrdkjbxxb.zdmj) {
      createMessage.warn('开展修复土壤面积不能超过地块占地面积');
      return false;
    }
    await saveXgpg(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.push({ 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.scjdbm = 'S6';
        res.pgtype = param.value.pgtype;
        res.jgtype = 'bzdw';
        // 新增的时候没有这个值
        res.wrdkid = param.value.wrdkid;
        const id = await updateBzdw(res);
        if (id != '成功') {
          unitMessageFormList.value[index] = res;
          unitMessageFormList.value[index].id = id;
        }
        saveFlg.value = true;
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 编制单位结束
  // 修复效果评估样品分析检测单位基本信息
  const childrenfxBzd = ref<any>([]);
  const childjcRef = (el, index) => {
    if (el) childrenfxBzd.value[index] = el;
  };
  const callChildFxMethod = (index) => {
    childrenfxBzd.value[index].getFormValueBz();
  };
  function addjcpgunitList(event) {
    event.stopPropagation();
    jcpgunitList.value.push({ id: null });
    nextTick(() => {
      callChildFxMethod(jcpgunitList.value.length - 1);
    });
  }
  async function deljcpgunitList(event, index) {
    event.stopPropagation();
    if (jcpgunitList.value[index].id) {
      await delJcdw({ id: jcpgunitList.value[index].id });
    }
    jcpgunitList.value.splice(index, 1);
    childrenfxBzd.value.splice(index, 1);
    // getdkInfor();
  }
  async function sujcpgunitListt(event, index) {
    event.stopPropagation();
    try {
      if (saveFlg.value) {
        saveFlg.value = false;
        const res = await childrenfxBzd.value[index].getFormValueBz();
        res.wthtje = String(res.wthtje);
        res.scjdbm = 'S6';
        res.pgtype = param.value.pgtype;
        res.wrdkid = param.value.wrdkid;
        res.jgtype = 'jcdw';
        const id = await updateJcdw(res);
        if (id != '成功') {
          jcpgunitList.value[index] = res;
          jcpgunitList.value[index].id = id;
        }
        saveFlg.value = true;
        // getdkInfor();
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  // 修复效果评估样品分析检测单位基本信息
  // 实施单位
  const childrenss = ref<any>([]);
  const childRefss = (el, index) => {
    if (el) childrenss.value[index] = el;
  };
  const callChildMethodss = (index) => {
    childrenss.value[index].getFormValueBz();
  };
  // 新增
  function addSs(event) {
    event.stopPropagation();
    ssdw.value.push({ id: null });
    nextTick(() => {
      callChildMethodss(ssdw.value.length - 1);
    });
  }
  // 删除
  async function deless(event, index) {
    event.stopPropagation();
    if (ssdw.value[index].id) await delBzdw({ id: ssdw.value[index].id });
    ssdw.value.splice(index, 1);
    childrenss.value.splice(index, 1);
  }
  // 保存
  async function savess(event, index) {
    event.stopPropagation();
    try {
      if (saveFlg.value) {
        saveFlg.value = false;
        const res = await childrenss.value[index].getFormValueBz();
        res.wthtje = String(res.wthtje);
        res.scjdbm = 'S6';
        res.pgtype = param.value.pgtype;
        res.jgtype = 'ssdw';
        res.wrdkid = param.value.wrdkid;
        const id = await updateBzdw(res);
        if (id != '成功') {
          ssdw.value[index] = res;
          ssdw.value[index].id = id;
        }
        saveFlg.value = true;
        // getdkInfor();
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  //监理单位
  const childrenjl = ref<any>([]);
  const childRefjl = (el, index) => {
    if (el) childrenjl.value[index] = el;
  };
  const callChildMethodjl = (index) => {
    childrenss.value[index].getFormValueBz();
  };
  // 新增
  function addjl(event) {
    event.stopPropagation();
    jldw.value.push({ id: null });
    nextTick(() => {
      callChildMethodjl(jldw.value.length - 1);
    });
  }
  // 删除
  async function deljl(event, index) {
    event.stopPropagation();
    if (jldw.value[index].id) await delJcdw({ id: jldw.value[index].id });
    jldw.value.splice(index, 1);
    childrenjl.value.splice(index, 1);
  }
  // 保存
  async function savejl(event, index) {
    event.stopPropagation();
    try {
      if (saveFlg.value) {
        saveFlg.value = false;
        const res = await childrenjl.value[index].getFormValueBz();
        res.wthtje = String(res.wthtje);
        res.scjdbm = 'S6';
        res.pgtype = param.value.pgtype;
        res.jgtype = 'jldw';
        res.wrdkid = param.value.wrdkid;
        const id = await updateJcdw(res);
        if (id != '成功') {
          jldw.value[index] = res;
          jldw.value[index].id = id;
        }
        saveFlg.value = true;
        // getdkInfor();
      } else {
        createMessage.warn('数据更新中请稍后点击');
      }
    } catch {
      saveFlg.value = true;
    }
  }
  //施工总结
  const childrensg = ref<any>([]);
  const childRefsg = (el, index) => {
    if (el) childrensg.value[index] = el;
  };
  const callChildMethodsg = (index) => {
    childrensg.value[index].getFormValueBz();
  };
  // 新增
  function addsg(event) {
    event.stopPropagation();
    sgzj.value.push({ id: null });
    nextTick(() => {
      callChildMethodsg(sgzj.value.length - 1);
    });
  }
  // 删除
  async function delsg(event, index) {
    event.stopPropagation();
    if (sgzj.value[index].id) await delJcdw({ id: sgzj.value[index].id });
    sgzj.value.splice(index, 1);
    childrensg.value.splice(index, 1);
  }
  // 保存
  async function savesg(event, index) {
    event.stopPropagation();
    try {
      if (saveFlg.value) {
        saveFlg.value = false;
        const res = await childrensg.value[index].getFormValueBz();
        res.wthtje = String(res.wthtje);
        res.scjdbm = 'S6';
        res.pgtype = param.value.pgtype;
        res.jgtype = 'sgzj';
        res.wrdkid = param.value.wrdkid;
        const id = await updateJcdw(res);
        if (id != '成功') {
          sgzj.value[index] = res;
          sgzj.value[index].id = id;
        }
        saveFlg.value = true;
        // getdkInfor();
      } 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>
