<template>
  <div class="app-container">
    <div class="top-btn" v-if="row.isRectify || row.isDelay">
      <el-button type="primary" size="small" @click="handleSubmit" v-preventReClick="5000">提交</el-button>
      <el-button type="primary" size="small" @click="">打印</el-button>
    </div>
    <div class="top-btn" v-if="row.isAcceptance">
      <el-button type="primary" plain size="small" @click="handleAcceptance(1)">验收通过</el-button>
      <el-button type="danger" plain size="small" @click="handleAcceptance(2)">驳回</el-button>
      <el-button plain size="small" @click="close">关闭</el-button>
    </div>
    <div class="top-btn" v-if="row.isDelayAcceptance">
      <el-button type="primary" plain size="small" @click="handleDelayAcceptance(1)">验收通过</el-button>
      <el-button type="danger" plain size="small" @click="handleDelayAcceptance(2)">驳回</el-button>
      <el-button plain size="small" @click="close">关闭</el-button>
    </div>
    <el-tabs v-model="active">
      <!-- 隐患整改单 -->
      <el-tab-pane label="隐患整改单" name="1" v-if="!row.isRecord">
        <!-- 基础信息 -->
        <el-card class="box-card">
          <div slot="header" class="card-header">
            <span>基础信息</span>
          </div>
          <el-form :model="baseInfoForm" ref="baseInfoForm" label-width="99px" size="small" disabled>
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item label="隐患问题编号" prop="pitfallCode">
                  <el-input v-model="baseInfoForm.pitfallCode" placeholder="请输入隐患问题编号" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="检查记录编号" prop="recordCode">
                  <el-input v-model="baseInfoForm.recordCode" placeholder="请输入检查记录编号" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="对应检查级别" prop="inspectLevelName">
                  <el-input
                    v-model="baseInfoForm.inspectLevelName"
                    placeholder="请输入对应检查级别"
                    class="cont-width" v-no-whitespace
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="排查类型" prop="checkTypeName">
                  <el-input v-model="baseInfoForm.checkTypeName" placeholder="请输入排查类型" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item label="创建人" prop="createUserName">
                  <el-input v-model="baseInfoForm.createUserName" placeholder="请输入创建人" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="创建时间">
                  <el-input v-model="baseInfoForm.createTime" placeholder="请输入创建时间" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="整改接收时间" prop="rectificationReceiveTime">
                  <el-input
                    v-model="baseInfoForm.rectificationReceiveTime"
                    placeholder="请输入整改接收时间" v-no-whitespace
                    class="cont-width"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="整改反馈时间" prop="rectificationFeedbackTime">
                  <el-input
                    v-model="baseInfoForm.rectificationFeedbackTime"
                    placeholder="请输入整改反馈时间" v-no-whitespace
                    class="cont-width"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item label="整改状态" prop="checkStatus">
                  <el-input v-model="baseInfoForm.checkStatus" placeholder="请输入整改状态" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>

        <!-- 检查方填写 -->
        <el-card class="box-card">
          <div slot="header" class="card-header">
            <span>检查方填写</span>
          </div>
          <el-form :model="inspectForm" ref="inspectForm" label-width="120px" size="small" disabled>
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item label="检查人" prop="checkUserName">
                  <el-input v-model="inspectForm.checkUserName" placeholder="请输入检查人" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="发现日期" prop="findTime">
                  <el-date-picker
                    v-model="inspectForm.findTime"
                    type="date"
                    placeholder="选择发现日期"
                    value-format="yyyy-MM-dd"
                    clearable
                    class="cont-width"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="隐患工程" prop="hazardProOptions">
                  <el-input v-model="inspectForm.proName" placeholder="请输入隐患工程" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="整改责任承包商" prop="subName">
                  <el-input v-model="inspectForm.subName" placeholder="请输入整改责任承包商" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item label="隐患问题类别" prop="pitfallTypeName">
                  <el-input v-model="inspectForm.pitfallTypeName" placeholder="请输入隐患问题类别" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>

              <el-col :span="6">
                <el-form-item label="隐患问题级别" prop="pitfallLevelName">
                  <el-input
                    v-model="inspectForm.pitfallLevelName"
                    placeholder="请输入隐患问题级别" v-no-whitespace
                    class="cont-width"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="期限整改时间" prop="correctionTime">
                  <el-date-picker
                    v-model="inspectForm.correctionTime"
                    type="date"
                    placeholder="选择期限整改时间"
                    value-format="yyyy-MM-dd"
                    clearable
                    class="cont-width"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="承包商责任人" prop="subPersonName">
                  <el-input v-model="inspectForm.subPersonName" placeholder="请输入承包商责任人" class="cont-width" v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="上传图片" prop="imgList">
                  <div style="display: flex">
                    <div v-for="(item, index) in inspectForm.imgList" :key="index">
                      <el-image
                        style="width: 100px; height: 100px; margin-right: 10px"
                        fit="fit"
                        :src="item.url"
                        :preview-src-list="[item.url]"
                      ></el-image>
                    </div>
                  </div>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="隐患问题描述" prop="pitfallDescription">
                  <el-input
                    class="cont-width-textarea"
                    type="textarea"
                    v-model="inspectForm.pitfallDescription"
                    placeholder="请输入隐患问题描述"
                    clearable v-no-whitespace
                    :autosize="{ minRows: 4, maxRows: 6 }"
                    maxlength="2000"
                    show-word-limit
                  />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="整改措施建议" prop="suggestion">
                  <el-input
                    class="cont-width-textarea"
                    type="textarea"
                    v-model="inspectForm.suggestion"
                    placeholder="请输入整改措施建议"
                    clearable v-no-whitespace
                    :autosize="{ minRows: 4, maxRows: 6 }"
                    maxlength="2000"
                    show-word-limit
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>

        <!-- 整改方填写 -->
        <el-card class="box-card">
          <div slot="header" class="card-header">
            <span>整改方填写</span>
          </div>
          <el-form :model="rectificationForm" ref="rectificationForm" label-width="120px" size="small" :rules="rules1">
            <el-form-item label="责任人" prop="dutyUserId">
              <el-select
                v-model="rectificationForm.dutyUserId"
                placeholder="请选择责任人"
                class="cont-width"
                @change="changeDutyUserId"
                :disabled="!row.isRectify"
              >
                <el-option
                  v-for="item in rectificationContractorOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="产生原因描述" prop="generateReason">
              <el-input
                class="cont-width-textarea"
                type="textarea"
                v-model="rectificationForm.generateReason"
                placeholder="请输入产生原因描述"
                clearable v-no-whitespace
                :autosize="{ minRows: 4, maxRows: 6 }"
                maxlength="2000"
                show-word-limit
                :disabled="!row.isRectify"
              />
            </el-form-item>
            <el-form-item label="整改情况" prop="correctionStatus">
              <el-input
                class="cont-width-textarea"
                type="textarea"
                v-model="rectificationForm.correctionStatus"
                placeholder="请输入整改情况"
                clearable v-no-whitespace
                :autosize="{ minRows: 4, maxRows: 6 }"
                maxlength="2000"
                show-word-limit
                :disabled="!row.isRectify"
              />
            </el-form-item>
            <el-form-item label="上传图片" prop="photoList">
              <el-upload
                v-if="row.isRectify"
                action="#"
                accept="image/*"
                :auto-upload="false"
                list-type="picture-card"
                :on-change="handleUpImg"
                :on-remove="handleRemoveImg"
                :on-exceed="handleExceedImg"
                :file-list="rectificationForm.photoList"
                :limit="9"
                :multiple="true"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <div v-else style="display: flex">
                <div v-for="(item, index) in rectificationForm.photoList" :key="index">
                  <el-image
                    style="width: 100px; height: 100px; margin-right: 10px"
                    fit="fit"
                    :src="item.url"
                    :preview-src-list="[item.url]"
                  ></el-image>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="上传文件" prop="fileList">
              <el-upload
                v-if="row.isRectify"
                action="#"
                accept=".doc,.docx,.xls,.xlsx,.ppt,.pptx,.pdf,.txt,.DOC,.DOCX,.XLS,.XLSX,.PPT,.PPTX,.PDF,.TXT"
                :auto-upload="false"
                :on-change="handleUpFile"
                :on-remove="handleRemoveFile"
                :on-exceed="handleExceedFile"
                :file-list="rectificationForm.fileList"
                :limit="9"
                :multiple="true"
              >
                <el-button size="small" type="primary">选择上传文件</el-button>
              </el-upload>
              <el-button v-else size="small" type="primary" @click="document = true">查看文件</el-button>
            </el-form-item>
          </el-form>
        </el-card>

        <!-- 验收方填写 -->
        <el-card class="box-card">
          <div slot="header" class="card-header">
            <span>验收方填写</span>
          </div>
          <el-form :model="acceptForm" ref="acceptForm" size="small" :rules="rules3">
            <el-row :gutter="10">
              <el-col :span="7" :offset="0">
                <el-form-item label="整改验收部门" prop="deptName" label-width="110px">
                  <el-input
                    v-model="acceptForm.deptName"
                    placeholder="请输入整改验收部门"
                    class="cont-width" v-no-whitespace
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="7" :offset="0">
                <el-form-item label="整改验收人" prop="userName">
                  <el-input v-model="acceptForm.userName" placeholder="请输入整改验收人" class="cont-width" disabled v-no-whitespace />
                </el-form-item>
              </el-col>
              <el-col :span="7" :offset="0">
                <el-form-item label="是否进行流程审批" prop="isProcess">
                  <el-input
                    v-model="acceptForm.isProcess"
                    placeholder="请输入是否进行流程审批"
                    class="cont-width"
                    disabled v-no-whitespace
                  />
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item label="整改意见" prop="opinion" label-width="110px">
              <el-input
                class="cont-width-textarea"
                type="textarea"
                v-model="acceptForm.opinion"
                placeholder="请输入整改意见"
                clearable
                :autosize="{ minRows: 4, maxRows: 6 }"
                maxlength="2000"
                show-word-limit v-no-whitespace
                :disabled="!row.isAcceptance"
              />
            </el-form-item>
            <el-form-item label="备注" prop="remarks" label-width="110px">
              <el-input
                class="cont-width-textarea"
                type="textarea"
                v-model="acceptForm.remarks"
                placeholder="请输入备注"
                clearable
                :autosize="{ minRows: 4, maxRows: 6 }"
                maxlength="2000"
                show-word-limit v-no-whitespace
                :disabled="!row.isAcceptance"
              />
            </el-form-item>
          </el-form>
        </el-card>

        <!-- 延期信息 -->
        <el-card class="box-card">
          <div slot="header" class="card-header">
            <span>延期信息</span>
          </div>
          <el-form
            :model="delayInfoForm"
            ref="delayInfoForm"
            size="small"
            label-width="110px"
            :disabled="!row.isDelay"
            :rules="rules2"
          >
            <el-form-item label="延期整改时间" prop="delayTime">
              <el-date-picker
                v-model="delayInfoForm.delayTime"
                type="date"
                placeholder="选择延期整改时间"
                value-format="yyyy-MM-dd"
                clearable
                class="cont-width"
              />
            </el-form-item>
            <el-form-item label="延期原因" prop="delayReason">
              <el-input
                class="cont-width-textarea"
                type="textarea"
                v-model="delayInfoForm.delayReason"
                placeholder="请输入延期原因"
                clearable v-no-whitespace
                :autosize="{ minRows: 4, maxRows: 6 }"
                maxlength="1000"
                show-word-limit
              />
            </el-form-item>
          </el-form>
        </el-card>
      </el-tab-pane>
      <!-- 审批信息 -->
      <el-tab-pane label="审批信息" name="2" v-if="row.isApprove || row.isRecord || row.isDetail">
        <div class="container" style="max-height: 750px; min-height: 500px; overflow-y: auto; padding: 10px">
          <img
            src="@/assets/file/no_data.png"
            alt="暂无数据"
            v-if="approvalItems.length === 0"
            style="position: absolute; top: 30%; left: 43%"
          />
          <bns-timeline-tabs
            :is-examine="true"
            :btn-show="row.isApprove"
            :show-type="row.isApprove ? 2 : 1"
            :show-position="2"
            :items="approvalItems"
            :final-check="row.finalCheck"
            v-on:pass="approvalPass"
            v-on:turnDown="approvalTurnDown"
            v-on:finalInstance="approvalFinalInstance"
            v-else
          ></bns-timeline-tabs>
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-dialog title="文件查看" :visible.sync="document" width="80%" top="5vh" append-to-body>
      <div style="width: 100%; height: 720px">
        <bns-file-list-tabs :items="rectificationForm.fileList" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getFileData, getMyToDoNum, getParams, lookFaceFile } from '@/utils/bonus'
import BnsTimelineTabs from '@/components/pro-tabs/bns-timeline-tabs.vue'
import { approvalHistory } from '@/api/terminalEquipment/deviceInformation'
import { decryptCBC } from '@/utils/aescbc'
import {
  getHiddenDangerRectificationForm,
  addHiddenDangerRectification,
  getPersonByConsIdSelect,
  updateHiddenDangerRectification,
  updateSuggestionsForRectification,
  updateApplicationForExtension,
  updateDeferredAcceptancePassed,
  updateReasonForDelay,
} from '@/api/hiddenDangerManagement'
import { submitApproval } from '@/api/pro/admissionRequest'
import { submitPersonApproval } from '@/api/pro/subManagement/subList'
import bnsFileListTabs from '@/components/pro-tabs/bns-file-list-tabs.vue'

export default {
  components: {
    BnsTimelineTabs,
    bnsFileListTabs,
  },
  data() {
    return {
      userId: this.$store.state.user.id, // 用户id
      proId: '', // 工程id
      userType: this.$store.state.user.userType, // 用户类型
      userName: this.$store.state.user.userData.nickName, // 用户名
      row: {}, // 行数据
      active: '1', // 当前激活的标签页
      document: false, // 文件查看弹窗
      // 基础信息
      baseInfoForm: {
        pitfallCode: '', // 隐患问题编号
        recordCode: '', // 检查记录编号
        inspectLevelName: '', // 对应检查级别
        checkTypeName: '', // 排查类型
        createUserName: '', // 创建人
        createTime: '', // 创建时间
        rectificationReceiveTime: '', // 整改接收时间
        rectificationFeedbackTime: '', // 整改反馈时间
        checkStatus: '', // 整改状态
      },
      // 检查方填写
      inspectForm: {
        checkUserName: '', // 检查人
        findTime: '', // 发现日期
        proName: '', // 隐患工程
        subName: '', // 整改责任承包商
        pitfallTypeName: '', // 隐患问题类别
        pitfallLevelName: '', // 隐患问题级别
        correctionTime: '', // 期限整改时间
        subPersonName: '', // 承包商责任人
        imgList: [], // 图片列表
        pitfallDescription: '', // 隐患问题描述
        suggestion: '', // 整改措施建议
      },
      // 整改方填写
      rectificationForm: {
        dutyUserId: '', // 责任人id
        dutyUserName: '', // 责任人
        generateReason: '', // 产生原因
        correctionStatus: '', // 整改情况
        photoList: [], // 图片列表
        fileList: [], // 文件列表
      },
      // 验收方填写
      acceptForm: {
        deptId: undefined, // 验收部门id
        deptName: '', // 验收部门
        userId: '', // 验收人id
        userName: '', // 验收人
        isProcess: '', // 是否进行流程审批
        opinion: '', // 验收描述
        remarks: '', // 备注
      },
      // 延期信息
      delayInfoForm: {
        delayTime: '', // 延期整改时间
        delayReason: '', // 延期原因
      },
      fileId: [], // 删除ids
      // 整改责任人-下拉
      rectificationContractorOptions: [],
      rules1: {
        dutyUserId: [{ required: true, message: '请选择责任人', trigger: 'change' }],
        generateReason: [{ required: true, message: '请输入产生原因描述', trigger: 'blur' }],
        correctionStatus: [{ required: true, message: '请输入整改描述', trigger: 'blur' }],
      },
      rules2: {
        delayTime: [{ required: true, message: '请选择延期整改时间', trigger: 'change' }],
        delayReason: [{ required: true, message: '请输入延期原因', trigger: 'blur' }],
      },
      rules3: {
        opinion: [{ required: true, message: '请输入整改意见', trigger: 'blur' }],
      },
      // 审批记录
      approvalItems: [],
    }
  },
  created() {
    console.log('🚀 ~ created-query:', this.$route.query)
    if (this.$route.query) {
      this.row = this.$route.query
      for (const key in this.row) {
        if (this.row[key] === 'true' || this.row[key] === 'false') {
          this.row[key] = JSON.parse(this.row[key])
        }
      }
      this.active = this.row.active || '1'
      console.log('🚀 ~ created ~ this.row:', this.row)
      // 修改面包屑名称
      const obj = Object.assign({}, this.$route, { title: this.row.title })
      this.$tab.updatePage(obj)
      this.getDetails()
      this.approvalHistory()
    }
  },
  methods: {
    getTodayDate() {
      const today = new Date()
      const year = today.getFullYear()
      const month = String(today.getMonth() + 1).padStart(2, '0')
      const day = String(today.getDate()).padStart(2, '0')
      return `${year}-${month}-${day}`
    },
    approvalHistory() {
      const params = {
        taskId: this.row.taskId,
        checkType: this.row.isRecord ? 2 : 1,
        auditRecordType: this.row.isRecord ? '全部' : '',
        auditType: '',
      }
      approvalHistory(params).then(res => {
        console.log('🚀 ~ approvalHistory ~ res', res)
        this.approvalItems = res.data
      })
    },
    async getDetails() {
      const params = {
        id: this.row.id,
        // proId: this.proId,
      }
      console.log('🚀 ~ 详情 ~ params:', params)
      const res = await getHiddenDangerRectificationForm(params)
      this.proId = res.data.proId
      await this.getPersonByConsIdSelect(res.data.subId)
      console.log('🚀 ~ getDetails ~ res:', res)
      this.baseInfoForm = { ...res.data }
      this.baseInfoForm.inspectLevelName = res.data.checkLevelName
      this.inspectForm = { ...res.data }
      if (this.inspectForm.checkPhotoList) {
        this.inspectForm.imgList = this.inspectForm.checkPhotoList.map(item => {
          return {
            name: item.fileName,
            url: lookFaceFile() + item.filePath,
          }
        })
      }
      this.rectificationForm = { ...res.data }
      if (this.rectificationForm.rectificationPhotoList) {
        this.rectificationForm.photoList = this.rectificationForm.rectificationPhotoList.map(item => {
          return {
            ...item,
            name: item.fileName,
            url: lookFaceFile() + item.filePath,
          }
        })
      }
      if (this.rectificationForm.attachmentList) {
        this.rectificationForm.fileList = this.rectificationForm.attachmentList.map(item => {
          return {
            ...item,
            name: item.fileName,
            url: lookFaceFile() + item.filePath,
          }
        })
      }
      this.acceptForm = { ...res.data }
      this.acceptForm.isProcess = res.data && res.data.isProcess == '1' ? '是' : '否'
      if (this.row.isAcceptance) {
        this.acceptForm.opinion = ''
        this.acceptForm.remarks = ''
      }
      this.delayInfoForm = { ...res.data }
      this.$nextTick(() => {
        this.$refs.rectificationForm.clearValidate()
        this.$refs.delayInfoForm.clearValidate()
      })
    },
    // 整改责任人-下拉
    async getPersonByConsIdSelect(supUuid) {
      const params = {
        supUuid,
        proId: this.proId,
      }
      console.log('🚀 ~ 下拉 ~ params:', params)
      const res = await getPersonByConsIdSelect(params)
      console.log('🚀 ~ 责任人 ~ res:', res)
      this.rectificationContractorOptions = res.data
    },
    changeDutyUserId(val) {
      console.log('责任人', val)
      const item = this.rectificationContractorOptions.find(item => item.value === val)
      this.rectificationForm.dutyUserName = item.label
    },
    // 上传图片
    handleUpImg(file, fileList) {
      console.log('上传图片', file, fileList)
      if (file.size > 10 * 1024 * 1024) {
        setTimeout(() => {
          this.$message.error('上传图片大小不能超过10M')
        }, fileList.length + 100)
        // 删除超过10M的图片
        fileList.pop()
        return
      }
      // 超过9张图片 提示
      if (fileList.length > 9) {
        this.$message.error('最多上传9张图片')
        return
      }
      this.rectificationForm.photoList = fileList
    },
    // 移除图片
    handleRemoveImg(file, fileList) {
      console.log('移除图片', file, fileList)
      this.rectificationForm.photoList = fileList
      this.fileId.push(file.fileId)
      console.log('🚀 ~ handleRemoveImg ~ this.fileId:', this.fileId)
    },
    // 超过9张图片 提示
    handleExceedImg() {
      this.$message.error('最多上传9张图片')
    },
    // 上传文件
    handleUpFile(file, fileList) {
      if (file.size > 10 * 1024 * 1024) {
        setTimeout(() => {
          this.$message.error('上传文件大小不能超过10M')
        }, fileList.length + 100)
        // 删除超过10M的图片
        fileList.pop()
        return
      }
      console.log('上传文件', file, fileList)
      this.rectificationForm.fileList = fileList
    },
    // 移除文件
    handleRemoveFile(file, fileList) {
      console.log('移除文件', file, fileList)
      this.rectificationForm.fileList = fileList
      this.fileId.push(file.fileId)
      console.log('🚀 ~ handleRemoveFile ~ this.fileId:', this.fileId)
    },
    // 超过9个文件 提示
    handleExceedFile() {
      this.$message.error('最多上传9个文件')
    },
    // 验收-通过/驳回
    async handleDelayAcceptance(type) {
      console.log('🚀 ~ 验收-通过/驳回 ~ type:', type)
      console.log('验收', this.row)
      const loading = this.$loading({
        lock: true,
        background: 'rgba(0, 0, 0, 0.7)',
      })
      if (type === 1) {
        // 通过
        const params = { id: this.row.id }
        console.log('🚀 ~ handleAcceptance ~ params:', params)
        try {
          const res = await updateDeferredAcceptancePassed(params)
          console.log('🚀 ~ handleAcceptance ~ res:', res)
          this.submitApply(1)
          loading.close()
        } catch (error) {
          console.log('验收', error)
          loading.close()
        }
      } else {
        // 驳回-弹框输入驳回原因
        this.$prompt('请输入驳回原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /\S/,
          inputErrorMessage: '请输入驳回原因',
        })
          .then(async ({ value }) => {
            console.log('驳回原因', value)
            const loading = this.$loading({
              lock: true,
              background: 'rgba(0, 0, 0, 0.7)',
            })
            try {
              const params = {
                id: this.row.id,
                rejectionDelay: value,
              }
              console.log('🚀 ~ handleAcceptance ~ params:', params)
              const res = await updateReasonForDelay(params)
              console.log('🚀 ~ handleAcceptance ~ res:', res)
              loading.close()
              this.$message.success('操作成功')
              this.$tab.closePage()
            } catch (error) {
              console.log('验收', error)
              loading.close()
            }
          })
          .catch(() => {
            console.log('取消驳回')
          })
      }
    },
    // 验收
    async handleAcceptance(type) {
      console.log('验收', type)
      this.$refs.acceptForm.validate(async valid => {
        if (valid) {
          console.log('验收表单验证通过', this.row.proId)
          const loading = this.$loading({
            lock: true,
            background: 'rgba(0, 0, 0, 0.7)',
          })
          try {
            const params = {
              id: this.row.id,
              proId: this.row.proId,
              isProcess: this.rectificationForm.isProcess,
              delayTime: this.delayInfoForm.delayTime,
              correctionTime: this.row.correctionTime,
              opinion: this.acceptForm.opinion,
              remarks: this.acceptForm.remarks,
            }
            console.log('🚀 ~ handleAcceptance ~ params:', params)
            if (type === 1) {
              const res = await updateHiddenDangerRectification(params)
              loading.close()
              console.log('🚀 ~ handleAcceptance ~ res:', res)
              if (Number(this.baseInfoForm.isProcess) === 1) {
                //有流程审批
                this.submitApply(2)
              } else {
                this.$message.success(res.msg)
                this.$tab.closePage()
              }
            } else {
              const res = await updateSuggestionsForRectification(params)
              loading.close()
              console.log('🚀 ~ handleAcceptance ~ res:', res)
              this.$message.success(res.msg)
              this.$tab.closePage()
            }
          } catch (error) {
            console.log('验收', error)
            loading.close()
          }
        } else {
          console.log('验收表单验证失败')
          return false
        }
      })
    },
    getParams,
    submitApply(type) {
      console.log(this.getParams())
      let taskType
      let param
      let code = '验收通过'
      if (type === 2) {
        if (Number(this.baseInfoForm.pitfallLevelId) === 1) {
          taskType = 14
        } else if (Number(this.baseInfoForm.pitfallLevelId) === 2) {
          taskType = 15
        } else if (Number(this.baseInfoForm.pitfallLevelId) === 3) {
          taskType = 16
        }
        param = {
          ...this.getParams(),
          proId: this.row.proId,
          taskType: taskType,
          userType: '01',
          taskId: '',
          uuid: this.row.id,
        }
      } else {
        param = {
          ...this.getParams(),
          proId: this.row.proId,
          taskType: '17',
          userType: '01',
          taskId: this.row.delayTaskId,
          uuid: this.row.id,
        }
        code = '延期申请成功'
      }
      console.log('param=' + JSON.stringify(param))
      submitApproval(param).then(response => {
        if (response.code === 200) {
          if (response.msg === '此审核类型未配置相对应的审核流，请联系管理员配置后再次提交') {
            this.$modal.msgError(response.msg)
          } else {
            this.$message.success(code + '，进入审核流程！')
            this.$tab.closePage()
          }
        } else {
          this.$modal.msgError(
            (type === 1 ? '验收' : '延期申请') + '失败，请联系管理员查询具体情况，再次进行验收操作！'
          )
        }
      })
    },
    // 关闭
    close() {
      this.$tab.closePage()
    },
    // 提交
    handleSubmit() {
      console.log('提交')
      if (this.row.isRectify) {
        this.$refs.rectificationForm.validate(async valid => {
          if (valid) {
            console.log('整改方填写表单验证通过')
            const formData = new FormData()
            if (this.rectificationForm.photoList && this.rectificationForm.photoList.length > 0) {
              this.rectificationForm.photoList.forEach(item => {
                if (item.raw) {
                  formData.append('photoList', item.raw)
                } else {
                  formData.append('photoList', new Blob())
                }
              })
            } else {
              formData.append('photoList', new Blob())
            }
            if (this.rectificationForm.fileList && this.rectificationForm.fileList.length > 0) {
              this.rectificationForm.fileList.forEach(item => {
                if (item.raw) {
                  formData.append('attachmentList', item.raw)
                } else {
                  formData.append('attachmentList', new Blob())
                }
              })
            } else {
              formData.append('attachmentList', new Blob())
            }
            const params = {
              ...this.rectificationForm,
              fileId: this.fileId,
              rectificationFeedbackTime: this.getTodayDate(),
            }
            delete params.photoList
            delete params.fileList
            console.log('🚀 ~ handleSubmit ~ params:', params)
            formData.append('params', JSON.stringify(params))
            // 打印(formData)
            for (let [key, value] of formData.entries()) {
              console.log('🚀 ~ formData--参数-->', key, value)
            }
            const res = await addHiddenDangerRectification(formData)
            console.log('🚀 ~ handleSubmit ~ res:', res)
            this.$message.success(res.msg)
            this.$tab.closePage()
          } else {
            console.log('整改方填写表单验证失败')
            return false
          }
        })
      } else if (this.row.isDelay) {
        console.log('延期信息表单验证', this.delayInfoForm)
        this.$refs.delayInfoForm.validate(valid => {
          if (valid) {
            updateApplicationForExtension(this.delayInfoForm).then(res => {
              if (res.code === 200) {
                // this.submitApply(1)
                this.$message.success(res.msg)
                this.$tab.closePage()
              } else {
                this.$message.error(res.msg)
              }
            })
            console.log('延期信息表单验证通过')
          } else {
            console.log('延期信息表单验证失败')
            return false
          }
        })
      }
    },
    /** 审核通过*/
    approvalPass(e) {
      const param = {
        reason: e,
        agree: '1',
      }
      this.approval(param, '审批通过')
    },
    /**审核驳回*/
    approvalTurnDown(e) {
      const param = {
        ...e,
        agree: '2',
      }
      this.approval(param, '驳回成功')
    },
    /**终审*/
    approvalFinalInstance(e) {
      const param = {
        reason: e,
        agree: '3',
      }
      this.approval(param, '终审成功')
    },
    /**
     * 审批方法+
     * @param param
     * @param text
     */
    getMyToDoNum,
    approval(param, text) {
      const params = {
        ...param,
        proId: this.row.proId,
        taskId: this.row.taskId,
        procInstId: this.row.procInsId,
        taskType: this.row.checkLevelId == '1' ? 14 : this.row.checkLevelId == '2' ? 15 : 16,
        uuid: this.row.id,
      }
      submitPersonApproval(params).then(response => {
        this.getMyToDoNum()
        if (response.code === 200) {
          this.$message({
            message: text,
            type: 'success',
          })
          this.$tab.closePage()
        } else {
          this.$message({
            message: response.msg,
            type: 'error',
          })
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.top-btn {
  display: flex;
  justify-content: flex-end;
}
.box-card {
  margin: 20px 0;
}
.card-header {
  font-size: 16px;
  font-weight: 700;
}
.cont-width {
  width: 210px;
}
.cont-width-textarea {
  width: 450px;
}
.footer-btn {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>
