<template>
  <div class="safety-platform-container">
    <header class="safety-platform-container__header">
      <div class="breadcrumb-title"><BreadcrumbBack /> 新增危险作业申请</div>
    </header>

    <main class="safety-platform-container__main">
      <el-form ref="formRef" :model="formValue" :rules="rules" label-width="160px" label-position="right">
        <div class="section-title">基础信息</div>
        <div class="form-grid">
          <el-form-item label="审批流程" prop="templateId" class="span-full">
            <el-select v-model="formValue.templateId" placeholder="请选择审批流程" size="large" clearable>
              <el-option v-for="opt in approvalOptions" :key="opt.id" :label="opt.templateName" :value="opt.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="作业类型" prop="hazardOperationType">
            <el-select size="large" v-model="formValue.hazardOperationType" class="w-100">
              <el-option :value="1" label="有限空间作业" />
              <el-option :value="2" label="高处作业" />
              <el-option :value="3" label="临时用电" />
              <el-option :value="4" label="动火作业" />
            </el-select>
          </el-form-item>
          <el-form-item label="申请单位" prop="applicationUnitName"
            ><el-input size="large" v-model="formValue.applicationUnitName"
          /></el-form-item>
          <el-form-item label="申请人" prop="applicantName"
            ><el-input size="large" v-model="formValue.applicantName"
          /></el-form-item>
          <el-form-item label="申请部门" prop="applicationDepartment"
            ><el-cascader
              v-model="formValue.applicationDepartmentId"
              size="large"
              ref="cascaderRef"
              :options="firstLevelDepts"
              :props="cascaderProp"
              :show-all-levels="false"
              placeholder="请选择部门名称"
              filterable
              @change="handleChangeDept"
              style="width: 100%"
          /></el-form-item>
          <el-form-item label="联系电话" prop="applicantPhone"
            ><el-input size="large" v-model="formValue.applicantPhone"
          /></el-form-item>
          <el-form-item label="备注信息" prop="remark" class="span-full">
            <el-input size="large" v-model="formValue.remark" type="textarea" :rows="2" placeholder="备注信息..." />
          </el-form-item>
        </div>

        <div class="form-line-divider"></div>

        <template v-if="formValue.hazardOperationType === 1">
          <div class="section-title">有限空间作业详情</div>
          <div class="form-grid">
            <el-form-item label="所属单位" prop="space.confinedSpaceUnit"
              ><el-input size="large" v-model="formValue.space.confinedSpaceUnit"
            /></el-form-item>
            <el-form-item label="空间名称" prop="space.confinedSpaceName"
              ><el-input size="large" v-model="formValue.space.confinedSpaceName"
            /></el-form-item>
            <el-form-item label="负责人" prop="space.unitResponsible"
              ><el-input size="large" v-model="formValue.space.unitResponsible"
            /></el-form-item>
            <el-form-item label="监护人" prop="space.supervisor"
              ><el-input size="large" v-model="formValue.space.supervisor"
            /></el-form-item>
            <el-form-item label="作业人" prop="space.operator"
              ><el-input size="large" v-model="formValue.space.operator" placeholder="多人请用逗号分隔"
            /></el-form-item>
            <el-form-item label="其他作业" prop="space.otherSpecialOps"
              ><el-input size="large" v-model="formValue.space.otherSpecialOps"
            /></el-form-item>
            <el-form-item label="开始时间" prop="space.operationStartTime"
              ><el-date-picker
                size="large"
                v-model="formValue.space.operationStartTime"
                value-format="YYYY-MM-DD"
                class="w-100"
            /></el-form-item>
            <el-form-item label="结束时间" prop="space.operationEndTime"
              ><el-date-picker
                size="large"
                v-model="formValue.space.operationEndTime"
                value-format="YYYY-MM-DD"
                class="w-100"
            /></el-form-item>
          </div>

          <div class="sub-group-box">
            <div class="sub-label is-required-manual section-title-flex">环境当前浓度指标 (初始评估)</div>
            <el-table :data="[formValue.space]" border class="density-table" style="margin-bottom: 20px">
              <el-table-column label="有毒有害物质 (toxicHazardous)">
                <template #default="scope">
                  <el-form-item prop="space.toxicHazardous" :rules="r" label-width="0" class="m-0"
                    ><el-input v-model="scope.row.toxicHazardous"
                  /></el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="可燃气 (flammable)">
                <template #default="scope">
                  <el-form-item prop="space.flammable" :rules="r" label-width="0" class="m-0"
                    ><el-input v-model="scope.row.flammable"
                  /></el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="氧含量 (oxygenContent)">
                <template #default="scope">
                  <el-form-item prop="space.oxygenContent" :rules="r" label-width="0" class="m-0"
                    ><el-input v-model="scope.row.oxygenContent"
                  /></el-form-item>
                </template>
              </el-table-column>
            </el-table>
          </div>

          <div class="form-grid mt-20">
            <el-form-item label="作业内容" prop="space.operationContent" class="span-full"
              ><el-input size="large" v-model="formValue.space.operationContent" type="textarea"
            /></el-form-item>
            <el-form-item label="危害辨识" prop="space.hazardIdentification" class="span-full"
              ><el-input size="large" v-model="formValue.space.hazardIdentification" type="textarea"
            /></el-form-item>
            <el-form-item label="作业附件" class="span-full"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('space', 'attachment', l)"
                :fileList="listMap['space.attachment']"
            /></el-form-item>
          </div>
        </template>

        <template v-else-if="formValue.hazardOperationType === 2">
          <div class="section-title">高处作业详情</div>
          <div class="form-grid">
            <el-form-item label="作业地点" prop="highAltitude.operationLocation"
              ><el-input size="large" v-model="formValue.highAltitude.operationLocation"
            /></el-form-item>
            <el-form-item label="作业单位" prop="highAltitude.operationUnit"
              ><el-input size="large" v-model="formValue.highAltitude.operationUnit"
            /></el-form-item>
            <el-form-item label="作业高度(m)" prop="highAltitude.operationHeight"
              ><el-input-number
                size="large"
                v-model="formValue.highAltitude.operationHeight"
                class="w-100"
                :controls="false"
            /></el-form-item>
            <el-form-item label="作业类别" prop="highAltitude.operationType"
              ><el-input size="large" v-model="formValue.highAltitude.operationType" placeholder="如：悬挂作业"
            /></el-form-item>
            <el-form-item label="作业人姓名" prop="highAltitude.operatorName"
              ><el-input size="large" v-model="formValue.highAltitude.operatorName"
            /></el-form-item>
            <el-form-item label="监护人" prop="highAltitude.supervisor"
              ><el-input size="large" v-model="formValue.highAltitude.supervisor"
            /></el-form-item>
            <el-form-item label="开始时间" prop="highAltitude.operationStartTime"
              ><el-date-picker
                size="large"
                v-model="formValue.highAltitude.operationStartTime"
                value-format="YYYY-MM-DD"
                class="w-100"
            /></el-form-item>
            <el-form-item label="结束时间" prop="highAltitude.operationEndTime"
              ><el-date-picker
                size="large"
                v-model="formValue.highAltitude.operationEndTime"
                value-format="YYYY-MM-DD"
                class="w-100"
            /></el-form-item>
            <el-form-item label="作业内容" prop="highAltitude.operationContent" class="span-full"
              ><el-input size="large" v-model="formValue.highAltitude.operationContent" type="textarea"
            /></el-form-item>
            <el-form-item label="危害辨识" prop="highAltitude.hazardIdentification" class="span-full"
              ><el-input size="large" v-model="formValue.highAltitude.hazardIdentification" type="textarea"
            /></el-form-item>
            <el-form-item label="作业附件" class="span-full"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('highAltitude', 'attachment', l)"
                :fileList="listMap['highAltitude.attachment']"
            /></el-form-item>
          </div>
        </template>

        <template v-else-if="formValue.hazardOperationType === 3">
          <div class="section-title">临时用电详情</div>
          <div class="form-grid">
            <el-form-item label="需求部门" prop="electricityList.requestDepartment"
              ><el-input size="large" v-model="formValue.electricityList.requestDepartment"
            /></el-form-item>
            <el-form-item label="用电类型" prop="electricityList.electricityType">
              <el-select v-model="formValue.electricityList.electricityType" class="w-100">
                <el-option :value="1" label="长期" /><el-option :value="2" label="临时" />
              </el-select>
            </el-form-item>
            <el-form-item label="责任人" prop="electricityList.contactPerson"
              ><el-input size="large" v-model="formValue.electricityList.contactPerson"
            /></el-form-item>
            <el-form-item label="设备功率" prop="electricityList.equipmentPower"
              ><el-input size="large" v-model="formValue.electricityList.equipmentPower"
            /></el-form-item>
            <el-form-item label="开始时间" prop="electricityList.operationStartTime"
              ><el-date-picker
                size="large"
                v-model="formValue.electricityList.operationStartTime"
                value-format="YYYY-MM-DD"
                class="w-100"
            /></el-form-item>
            <el-form-item label="结束时间" prop="electricityList.operationEndTime"
              ><el-date-picker
                size="large"
                v-model="formValue.electricityList.operationEndTime"
                value-format="YYYY-MM-DD"
                class="w-100"
            /></el-form-item>
            <el-form-item label="保护措施" prop="electricityList.safety" class="span-full"
              ><el-input size="large" v-model="formValue.electricityList.safety"
            /></el-form-item>
            <el-form-item label="用电事由" prop="electricityList.reason" class="span-full"
              ><el-input size="large" v-model="formValue.electricityList.reason" type="textarea"
            /></el-form-item>
            <el-form-item label="申请附件" class="span-full"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('electricityList', 'attachment', l)"
                :fileList="listMap['electricityList.attachment']"
            /></el-form-item>
          </div>
        </template>

        <template v-else-if="formValue.hazardOperationType === 4">
          <div class="section-title">动火作业详情</div>
          <div class="sub-label">A. 动火任务信息</div>
          <div class="form-grid">
            <el-form-item label="动火地点" prop="hot.hotWorkLocation"
              ><el-input size="large" v-model="formValue.hot.hotWorkLocation"
            /></el-form-item>
            <el-form-item label="动火级别" prop="hot.hotWorkLevel">
              <el-select v-model="formValue.hot.hotWorkLevel" class="w-100">
                <el-option :value="1" label="一级" /><el-option :value="2" label="二级" /><el-option
                  :value="3"
                  label="三级"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="特殊时段" prop="hot.isSpecialPeriod">
              <el-radio-group v-model="formValue.hot.isSpecialPeriod">
                <el-radio :label="1">是 (节假日/重大活动)</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="动火类型" prop="hot.hotWorkType"
              ><el-input size="large" v-model="formValue.hot.hotWorkType" placeholder="气焊/电焊/打磨等"
            /></el-form-item>
            <el-form-item label="开始时间" prop="hot.hotWorkStart"
              ><el-date-picker
                size="large"
                v-model="formValue.hot.hotWorkStart"
                value-format="YYYY-MM-DD"
                class="w-100"
            /></el-form-item>
            <el-form-item label="结束时间" prop="hot.hotWorkEnd"
              ><el-date-picker size="large" v-model="formValue.hot.hotWorkEnd" value-format="YYYY-MM-DD" class="w-100"
            /></el-form-item>
            <el-form-item label="动火任务" prop="hot.hotWorkTask" class="span-full"
              ><el-input size="large" v-model="formValue.hot.hotWorkTask" type="textarea"
            /></el-form-item>
            <el-form-item label="危险性分析" prop="hot.hazardAnalysis" class="span-full"
              ><el-input size="large" v-model="formValue.hot.hazardAnalysis" type="textarea"
            /></el-form-item>
          </div>

          <div class="sub-label">B. 责任人信息</div>
          <div class="form-grid">
            <el-form-item label="动火作业人员" prop="hot.hotWorkman"
              ><el-input size="large" v-model="formValue.hot.hotWorkman"
            /></el-form-item>
            <el-form-item label="动火人电话" prop="hot.hotWorkContact"
              ><el-input size="large" v-model="formValue.hot.hotWorkContact"
            /></el-form-item>
            <el-form-item label="施工单位监护人" prop="hot.constructionSupervisor"
              ><el-input size="large" v-model="formValue.hot.constructionSupervisor"
            /></el-form-item>
            <el-form-item label="监护人电话" prop="hot.supervisorContact"
              ><el-input size="large" v-model="formValue.hot.supervisorContact"
            /></el-form-item>
            <el-form-item label="现场负责人" prop="hot.constructionSiteLeader"
              ><el-input size="large" v-model="formValue.hot.constructionSiteLeader"
            /></el-form-item>
            <el-form-item label="教育人" prop="hot.constructionEducator"
              ><el-input size="large" v-model="formValue.hot.constructionEducator"
            /></el-form-item>
            <el-form-item label="申请部门监护人" prop="hot.hotWorkSupervisor"
              ><el-input size="large" v-model="formValue.hot.hotWorkSupervisor"
            /></el-form-item>
            <el-form-item label="部门监护电话" prop="hot.supervisorNumber"
              ><el-input size="large" v-model="formValue.hot.supervisorNumber"
            /></el-form-item>
          </div>

          <div class="sub-label">C. 防护措施与附件</div>
          <div class="form-grid">
            <el-form-item label="安全防护措施" prop="hot.fireSafetyMeasures" class="span-full"
              ><el-input size="large" v-model="formValue.hot.fireSafetyMeasures" type="textarea"
            /></el-form-item>
            <el-form-item label="现场检查情况" prop="hot.supervisorMeasures" class="span-full"
              ><el-input size="large" v-model="formValue.hot.supervisorMeasures" type="textarea"
            /></el-form-item>

            <el-form-item label="动火现场照片"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('hot', 'photos', l)"
                :fileList="listMap['hot.photos']"
            /></el-form-item>
            <el-form-item label="身份证复印件"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('hot', 'idCard', l)"
                :fileList="listMap['hot.idCard']"
            /></el-form-item>
            <el-form-item label="特种作业证"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('hot', 'optionCard', l)"
                :fileList="listMap['hot.optionCard']"
            /></el-form-item>
            <el-form-item label="安全教育记录"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('hot', 'safetyEducationPlan', l)"
                :fileList="listMap['hot.safetyEducationPlan']"
            /></el-form-item>
            <el-form-item label="施工方案"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('hot', 'constructionPlan', l)"
                :fileList="listMap['hot.constructionPlan']"
            /></el-form-item>
            <el-form-item label="准备工作照片"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('hot', 'preparationPhotos', l)"
                :fileList="listMap['hot.preparationPhotos']"
            /></el-form-item>
            <el-form-item label="管理协议"
              ><UploadFiles
                label="上传附件"
                @upload-success="(l) => handleUpload('hot', 'safetyManagementAgreement', l)"
                :fileList="listMap['hot.safetyManagementAgreement']"
            /></el-form-item>
          </div>
        </template>

        <div class="form-line-divider"></div>

        <div class="section-title-flex">
          <span class="is-required-manual">安全措施清单</span>
          <el-button type="primary" @click="addRow('measure')">+ 新增措施</el-button>
        </div>
        <el-table :data="formValue.measure" border class="mb-20">
          <el-table-column label="序号" prop="serialNumber" width="100" align="center" />
          <el-table-column label="措施内容"
            ><template #default="scope"><el-input v-model="scope.row.safetyMeasure" /></template
          ></el-table-column>
          <el-table-column label="确认人" width="200"
            ><template #default="scope"><el-input v-model="scope.row.confirmer" /></template
          ></el-table-column>
          <el-table-column label="操作" width="80" align="center">
            <template #default="scope"
              ><el-button type="primary" link @click="removeRow('measure', scope.$index)">删除</el-button></template
            >
          </el-table-column>
        </el-table>

        <div class="section-title-flex">
          <span class="is-required-manual">危害分析记录</span>
          <el-button type="primary" @click="addRow('analysis')">+ 新增记录</el-button>
        </div>
        <el-table :data="formValue.analysis" border>
          <el-table-column label="检测部位"
            ><template #default="scope"><el-input v-model="scope.row.analysisPosition" /></template
          ></el-table-column>
          <el-table-column label="有毒物质及浓度"
            ><template #default="scope"><el-input v-model="scope.row.toxicSubstance" /></template
          ></el-table-column>
          <el-table-column label="可燃气及浓度"
            ><template #default="scope"><el-input v-model="scope.row.flammableGas" /></template
          ></el-table-column>
          <el-table-column label="氧含量"
            ><template #default="scope"><el-input v-model="scope.row.oxygenContent" /></template
          ></el-table-column>
          <el-table-column label="检测人" width="130"
            ><template #default="scope"><el-input v-model="scope.row.analyst" /></template
          ></el-table-column>
          <el-table-column label="检测时间" width="220"
            ><template #default="scope">
              <el-date-picker
                v-model="scope.row.analysisTime"
                value-format="YYYY-MM-DD"
                style="width: 100%"
              /> </template
          ></el-table-column>
          <el-table-column label="操作" width="80" align="center">
            <template #default="scope"
              ><el-button type="primary" link @click="removeRow('analysis', scope.$index)">删除</el-button></template
            >
          </el-table-column>
        </el-table>
      </el-form>
    </main>

    <footer class="safety-platform-container__footer">
      <el-button size="large" @click="router.back()">取 消</el-button>
      <el-button type="primary" size="large" :loading="loading" @click="handleSave">提交</el-button>
    </footer>
  </div>
  <el-dialog v-model="hotWorkDialogVisible" title="">
    <div
      v-html="
        `备注1：
    1.【重要】申请部门联络人手机号需与企业微信中登记的手机号一致，系統根据申请部门联络人手机号推送审批单及相关信息。
    2.一级动火，由动火部门申请，须由院消防安全麦任人南批;二级动火，由动火都门申清，须由消防安全分管院领导南批；三级动火，由动火部门申请，须由消防安全管理部门领导南批。
    3.申请动火时需提供动火地点具体位置的照片，否则不子以办理动火证。
    4,一级动火区域为：一级危险点、油库、燃油试验室、燃气区域等高危场所；二级动火区域为：二级危险点、35KV变电站、高处作业场所、试验场所、仓库。堆场、待建区等；三级动火区域为：三级危险点、10KV交电站、空压机房、办公室、室外区域(除待建区外）
    等。 备注2： 1.填写动火开始时间及截止时间，一级动火和二级动火有效期不应超过1天，三级动火有效期不应超过6天：
    2.动火作业需提前三天开始申请；
    3.申请部门完成申请后，将动火作业表打印纸质版本及动火申请材料放置于动火现场，消防值班人员将进行现场检查，现场若无紙质动火作业表及动火申请材料不予动火。
    备注3： 1,电煌 气割，严格道守“十不烧”规程操作： 2.操作前检查动火人员的工具、电焊机、电源开关及线路是否良好；
    3.注意安全用电，检查电线是否有乱拖乱拉现象，电源线是否架空扎车； 4.检查人员在动火期间不得离开现场，应当留下监护， 5.
    动火结束后，督促动火人员清理现场，不得留下残渣； 6.现场配备灭火器： 7.现场专人看护， 8.清理周边易燃物：
    9.现场动火人员持证上岗；`
      "
    ></div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleCancel">我已阅读并确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
  import { ref, reactive, onMounted, watch } from 'vue';
  import { useRouter } from 'vue-router';
  import { ElMessage } from 'element-plus';
  import UploadFiles from '@/components/UploadFiles/UploadFiles.vue';
  import { getAllApproval } from '@/api/approval/approval';
  import { dangerWorkSaveDangerWork } from '@/api/production-safety/responsibility-implementation';
  import { formatDeptTree } from '@/views/disaster/utils/formatDeptTree';
  import { getAllDepartments } from '@/api/auth/dept';
  import { unformatAttachment, formatAttachmentList } from '@/components/UploadFiles/utils';

  const router = useRouter();
  const formRef = ref<any>(null);
  const loading = ref(false);
  const approvalOptions = ref<any[]>([]);
  const firstLevelDepts = ref<any[]>([]);
  const hotWorkDialogVisible = ref(false);
  const cascaderRef = ref<any>(null);
  const cascaderProp = {
    expandTrigger: 'click',
    checkStrictly: true,
    // emitPath: false,
    value: 'id',
    label: 'deptName',
  };

  // 附件管理 (存储 fileList 用于回显)
  const listMap = reactive<any>({
    'space.attachment': [],
    'highAltitude.attachment': [],
    'electricityList.attachment': [],
    'hot.photos': [],
    'hot.idCard': [],
    'hot.optionCard': [],
    'hot.safetyEducationPlan': [],
    'hot.constructionPlan': [],
    'hot.preparationPhotos': [],
    'hot.safetyManagementAgreement': [],
  });

  const formValue = reactive<any>({
    templateId: '',
    applicationDepartment: '',
    applicationDepartmentId: [],
    applicationUnitName: '',
    applicantName: '',
    applicantPhone: '',
    hazardOperationType: 1,
    remark: '',
    space: {
      confinedSpaceUnit: '',
      confinedSpaceName: '',
      operationContent: '',
      operationStartTime: '',
      operationEndTime: '',
      unitResponsible: '',
      supervisor: '',
      operator: '',
      otherSpecialOps: '',
      hazardIdentification: '',
      attachment: '',
      toxicHazardous: '',
      flammable: '',
      oxygenContent: '',
    },
    highAltitude: {
      operationStartTime: '',
      operationEndTime: '',
      operationLocation: '',
      operationContent: '',
      operationHeight: undefined,
      operationType: '',
      operationUnit: '',
      supervisor: '',
      operatorName: '',
      operatorJob: '',
      hazardIdentification: '',
      attachment: '',
    },
    electricityList: {
      requestDepartment: '',
      electricityType: 2,
      operationStartTime: '',
      operationEndTime: '',
      reason: '',
      contactPerson: '',
      equipmentPower: '',
      safety: '',
      attachment: '',
    },
    hot: {
      hotWorkLocation: '',
      hotWorkLevel: 2,
      isSpecialPeriod: 0,
      hotWorkStart: '',
      hotWorkEnd: '',
      hotWorkTask: '',
      hotWorkType: '',
      hotWorkman: '',
      hotWorkContact: '',
      hazardAnalysis: '',
      fireSafetyMeasures: '',
      constructionSupervisor: '',
      supervisorContact: '',
      constructionSiteLeader: '',
      constructionEducator: '',
      hotWorkSupervisor: '',
      supervisorNumber: '',
      supervisorMeasures: '',
      photos: '',
      idCard: '',
      optionCard: '',
      safetyEducationPlan: '',
      constructionPlan: '',
      preparationPhotos: '',
      safetyManagementAgreement: '',
    },
    measure: [{ serialNumber: 1, safetyMeasure: '', confirmer: '' }],
    analysis: [
      { toxicSubstance: '', flammableGas: '', oxygenContent: '', analysisTime: '', analysisPosition: '', analyst: '' },
    ],
  });

  const r = { required: true, message: '此项必填', trigger: ['blur', 'change'] };

  // 核心校验规则
  const rules = reactive({
    templateId: [r],
    applicationUnitName: [r],
    applicantName: [r],
    applicationDepartment: [r],
    applicantPhone: [r],
    remark: [r],
    // 有限空间
    'space.confinedSpaceUnit': [r],
    'space.confinedSpaceName': [r],
    'space.unitResponsible': [r],
    'space.supervisor': [r],
    'space.operator': [r],
    'space.otherSpecialOps': [r],
    'space.operationStartTime': [r],
    'space.operationEndTime': [r],
    'space.operationContent': [r],
    'space.hazardIdentification': [r],
    // 高处作业
    'highAltitude.operationLocation': [r],
    'highAltitude.operationUnit': [r],
    'highAltitude.operationHeight': [r],
    'highAltitude.operationType': [r],
    'highAltitude.operatorName': [r],
    'highAltitude.supervisor': [r],
    'highAltitude.operationStartTime': [r],
    'highAltitude.operationEndTime': [r],
    'highAltitude.operationContent': [r],
    'highAltitude.hazardIdentification': [r],
    // 临时用电
    'electricityList.requestDepartment': [r],
    'electricityList.electricityType': [r],
    'electricityList.contactPerson': [r],
    'electricityList.equipmentPower': [r],
    'electricityList.operationStartTime': [r],
    'electricityList.operationEndTime': [r],
    'electricityList.safety': [r],
    'electricityList.reason': [r],
    // 动火作业
    'hot.hotWorkLocation': [r],
    'hot.hotWorkLevel': [r],
    'hot.hotWorkType': [r],
    'hot.hotWorkStart': [r],
    'hot.hotWorkEnd': [r],
    'hot.hotWorkman': [r],
    'hot.hotWorkContact': [r],
    'hot.constructionSupervisor': [r],
    'hot.supervisorContact': [r],
    'hot.constructionSiteLeader': [r],
    'hot.constructionEducator': [r],
    'hot.hotWorkSupervisor': [r],
    'hot.supervisorNumber': [r],
    'hot.hotWorkTask': [r],
    'hot.hazardAnalysis': [r],
    'hot.fireSafetyMeasures': [r],
    'hot.supervisorMeasures': [r],
  });
  const handleChangeDept = () => {
    const deptInfo = cascaderRef.value?.getCheckedNodes();
    if (deptInfo?.[0]) {
      formValue.applicationDepartment = deptInfo[0].label;
      formValue.applicationDepartmentId = deptInfo[0].pathValues;
    }
  };
  const handleUpload = async (obj: string, key: string, list: any[]) => {
    listMap[`${obj}.${key}`] = list;

    if (formValue[obj]) {
      const formatted = await formatAttachmentList(list);
      formValue[obj][key] = formatted.length > 0 ? JSON.stringify(formatted) : '';
    }
  };

  const handleSave = () => {
    formRef.value.validate((valid: boolean) => {
      if (!valid) return ElMessage.error('请完善表单必填信息');

      loading.value = true;

      // 1. 深拷贝表单数据
      const submitData = JSON.parse(JSON.stringify(formValue));

      // 2. 根据类型删除不相关的对象
      const type = submitData.hazardOperationType;
      if (type !== 1) delete submitData.space;
      if (type !== 2) delete submitData.highAltitude;
      if (type !== 3) delete submitData.electricityList;
      if (type !== 4) delete submitData.hot;

      // 3. 执行提交
      dangerWorkSaveDangerWork(submitData)
        .then(() => {
          ElMessage.success('提交成功');
          router.back();
        })
        .finally(() => (loading.value = false));
    });
  };

  const addRow = (type: string) => {
    if (type === 'measure') {
      formValue.measure.push({ serialNumber: formValue.measure.length + 1, safetyMeasure: '', confirmer: '' });
    } else {
      formValue.analysis.push({
        toxicSubstance: '',
        flammableGas: '',
        oxygenContent: '',
        analysisTime: '',
        analysisPosition: '',
        analyst: '',
      });
    }
  };

  const removeRow = (type: string, index: number) => {
    formValue[type].splice(index, 1);
    if (type === 'measure') formValue.measure.forEach((item: any, idx: number) => (item.serialNumber = idx + 1));
  };
  const getDeptData = () => {
    getAllDepartments().then((res) => {
      firstLevelDepts.value = formatDeptTree(res);
    });
  };
  watch(
    () => formValue.hazardOperationType,
    (newVal: number) => {
      if (newVal === 4 && sessionStorage.getItem('hazardOperationType') !== '4') {
        hotWorkDialogVisible.value = true;
      }
    },
  );

  const handleCancel = () => {
    hotWorkDialogVisible.value = false;
    sessionStorage.setItem('hazardOperationType', '4');
  };

  onMounted(async () => {
    await getDeptData();
    const res = await getAllApproval();
    approvalOptions.value = res || [];
  });
</script>

<style lang="scss" scoped>
  @use '@/styles/page-main-layout.scss' as *;
  @use '@/styles/page-details-layout.scss' as *;
  .safety-platform-container {
    .section-title {
      margin-bottom: 15px;
    }
    .section-title-flex {
      display: flex;
      justify-content: space-between;
      margin: 15px 0;
    }
    &__main {
      padding: 24px;
      background-color: #fff;
    }
    :deep(.el-form-item) {
      margin-right: 20px;
      margin-bottom: 24px;
      vertical-align: top;
      .el-form-item__label {
        font-weight: bold;
      }
    }
  }
</style>
