<script name="empMaterials" setup>
import { reactive, ref, watch } from 'vue'
import { fileFormatVerification, handleFileError, handleFileSuccess, handleFilePreview, reviewFileParam, fileTypes, fileDesc } from '@/utils/uploadAction'
import store from '@/store'
import ActualPerformanceIndicatorsDialog
  from '@/pages/declareManage/initialInspectionRecord/uploadInitMaterials/components/actualPerformanceIndicatorsDialog.vue'

const props = defineProps({
    detail: {
      type: Object
    },
    data: {
      type: Object
    },
    basicInfoData: {
      type: Object
    },
    approvalInfoData: {
      type: Object
    },
    baseProjSetYear: {
      type: String,
      default: ''
    }
  }),
  uploadUrl = store.dictStore.uploadUrl,
  formRef = ref(),
  formData = ref({
    feasibilityStudyReport: [],
    approvedFile: [],
    purchaseFile: [],
    acceptanceLetter: [],
    purchaseContract: [],
    acceptanceReport: [],
    changeApprovalDoc: [],
    baseBusinessMetrics: []
  }),
  fileRule = [{ required: true, message: '请选择文件' }],
  currentRules = ref({
    baseInitialOpinionFile: fileRule,
    baseInforLevelFile: fileRule,
    basePasswAssessFile: fileRule,
    baseThirdAcceptFile: fileRule,
    baseCheckFile: fileRule,
    baseFinanlAuditFile: fileRule,
    baseUserConsFile: fileRule,
    baseEstaSummFile: fileRule,
    baseOperatMaintenSummFile: fileRule,
    baseFinalExpertOpinionFile: fileRule,
    baseEngineerPostpoFile: fileRule,
    baseEngineerAlterFile: fileRule,
    baseChanFile: fileRule,
    baseBusinessMetrics: [{ required: true, message: '请添加' }],
    baseLogAggregation: [{ required: true, message: '请选择' }]
  }),
  // 实际成效指标
  column1 = [
    {
      label: '核心业务',
      prop: 'businessName',
      key: 'businessName'
    },
    {
      label: '实际成效指标',
      prop: 'name',
      key: 'name'
    },
    {
      label: '数值',
      prop: 'nums',
      key: 'nums'
    },
    {
      label: '单位',
      prop: 'unit',
      key: 'unit'
    },
    {
      label: '操作',
      slot: 'action',
      key: 'action'
    }
  ],
  projectContentDialogData = reactive({
    visible: false,
    data: undefined
  }),
  projectContentIndex = ref(),
  showProjectContentDialog = (data, index) => {
    projectContentDialogData.data = data
    projectContentDialogData.visible = true
    projectContentIndex.value = index
  },
  setContent = (data) => {
    if (projectContentIndex.value === undefined) {
      formData.value.baseBusinessMetrics = [...formData.value.baseBusinessMetrics, ...data]
    } else {
      formData.value.baseBusinessMetrics[projectContentIndex.value] = data[0]
    }
  },
  delProjectContent = (index) => {
    formData.value.baseBusinessMetrics.splice(index, 1)
  }

defineExpose({ formRef, formData })
watch(() => props.basicInfoData, val => {
  if (val.baseProjIsConfidentiality === '02' || val?.baseProjSetProg === '00') {
    currentRules.value = {}
  } else {
    currentRules.value = {
      baseInitialOpinionFile: fileRule,
      baseInforLevelFile: fileRule,
      basePasswAssessFile: fileRule,
      baseThirdAcceptFile: fileRule,
      baseCheckFile: fileRule,
      baseFinanlAuditFile: fileRule,
      baseUserConsFile: fileRule,
      baseEstaSummFile: fileRule,
      baseOperatMaintenSummFile: fileRule,
      baseFinalExpertOpinionFile: fileRule,
      baseEngineerPostpoFile: fileRule,
      baseEngineerAlterFile: fileRule,
      baseChanFile: fileRule,
      baseBusinessMetrics: [{ required: true, message: '请添加' }],
      baseLogAggregation: [{ required: true, message: '请选择' }]
    }
  }
  formRef.value.clearValidate()
}, { deep: true })
watch(() => props.detail, val => {
  if (val) {
    formData.value = {
      ...val.mimplement,
      baseInitialOpinionFile: val.mimplement?.baseInitialOpinionFile ? reviewFileParam(JSON.parse(val.mimplement.baseInitialOpinionFile)) : [],
      baseInforLevelFile: val.mimplement?.baseInforLevelFile ? reviewFileParam(JSON.parse(val.mimplement.baseInforLevelFile)) : [],
      basePasswAssessFile: val.mimplement?.basePasswAssessFile ? reviewFileParam(JSON.parse(val.mimplement.basePasswAssessFile)) : [],
      baseThirdAcceptFile: val.mimplement?.baseThirdAcceptFile ? reviewFileParam(JSON.parse(val.mimplement.baseThirdAcceptFile)) : [],
      baseCheckFile: val.mimplement?.baseCheckFile ? reviewFileParam(JSON.parse(val.mimplement.baseCheckFile)) : [],
      baseFinanlAuditFile: val.mimplement?.baseFinanlAuditFile ? reviewFileParam(JSON.parse(val.mimplement.baseFinanlAuditFile)) : [],
      baseUserConsFile: val.mimplement?.baseUserConsFile ? reviewFileParam(JSON.parse(val.mimplement.baseUserConsFile)) : [],
      baseEstaSummFile: val.mimplement?.baseEstaSummFile ? reviewFileParam(JSON.parse(val.mimplement.baseEstaSummFile)) : [],
      baseOperatMaintenSummFile: val.mimplement?.baseOperatMaintenSummFile ? reviewFileParam(JSON.parse(val.mimplement.baseOperatMaintenSummFile)) : [],
      baseFinalExpertOpinionFile: val.mimplement?.baseFinalExpertOpinionFile ? reviewFileParam(JSON.parse(val.mimplement.baseFinalExpertOpinionFile)) : [],
      baseEngineerPostpoFile: val.mimplement?.baseEngineerPostpoFile ? reviewFileParam(JSON.parse(val.mimplement.baseEngineerPostpoFile)) : [],
      baseEngineerAlterFile: val.mimplement?.baseEngineerAlterFile ? reviewFileParam(JSON.parse(val.mimplement.baseEngineerAlterFile)) : [],
      baseChanFile: val.mimplement?.baseChanFile ? reviewFileParam(JSON.parse(val.mimplement.baseChanFile)) : [],
      baseBusinessMetrics: val.mimplement?.baseBusinessMetrics && JSON.parse(val.mimplement?.baseBusinessMetrics) || []
    }
  }
})
watch(() => props.data, val => {
},
{ immediate: true, deep: true })
</script>

<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="currentRules"
    label-position="top"
    label-suffix=":"
    scroll-to-error
    :validate-on-rule-change="false"
  >
    <el-row :gutter="40">
      <el-col :span="24">
        <el-form-item
          v-if="baseProjSetYear*1>=2024"
          label="是否完成日志数据归集"
          label-width="170"
          prop="baseLogAggregation"
        >
          <el-radio-group v-model="formData.baseLogAggregation">
            <el-radio label="1">是</el-radio>
            <el-radio label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-else
          label="是否完成日志数据归集"
          label-width="170"
        >
          <el-radio-group v-model="formData.baseLogAggregation">
            <el-radio label="1">是</el-radio>
            <el-radio label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item
          v-if="baseProjSetYear*1>=2024"
          label="实际成效指标"
          label-width="170"
          prop="baseBusinessMetrics"
        >
          <table-list
            :pagination="false"
            style="width: 100%"
            :column="column1"
            :data="formData.baseBusinessMetrics"
            :empty-temp="false"
          >
            <template #action="{scope}">
              <a @click="showProjectContentDialog(scope.row,scope.$index)">编辑</a>
              <a class="text-danger" @click="delProjectContent(scope.$index)">删除</a>
            </template>
          </table-list>
          <p class="text-right w-full mt-8">
            <el-button
              type="primary"
              class="w-full"
              plain
              icon="Plus"
              @click="()=>showProjectContentDialog()"
            >添加</el-button>
          </p>
        </el-form-item>
        <el-form-item
          v-else
          label="实际成效指标"
          label-width="170"
        >
          <table-list
            :pagination="false"
            style="width: 100%"
            :column="column1"
            :data="formData.baseBusinessMetrics"
            :empty-temp="false"
          >
            <template #action="{scope}">
              <a @click="showProjectContentDialog(scope.row,scope.$index)">编辑</a>
              <a class="text-danger" @click="delProjectContent(scope.$index)">删除</a>
            </template>
          </table-list>
          <p class="text-right w-full mt-8">
            <el-button
              type="primary"
              class="w-full"
              plain
              icon="Plus"
              @click="()=>showProjectContentDialog()"
            >添加</el-button>
          </p>
        </el-form-item>
      </el-col>
      <el-col v-if="(['05','06','07','00'].includes(basicInfoData?.baseProjSetProg)&&basicInfoData?.baseConstructionType?.includes('01'))||!basicInfoData?.baseConstructionType?.includes('01')" :span="8">
        <el-form-item
          v-if="basicInfoData?.baseProjSetProg==='07'&&basicInfoData?.baseConstructionType?.includes('01')&&['03','04','05'].includes(approvalInfoData?.equalProtectionLevel)"
          label="信息安全等级保护测评报告"
          prop="baseInforLevelFile"
        >
          <el-upload
            v-model:file-list="formData.baseInforLevelFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseInforLevelFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item
          v-else
          label="信息安全等级保护测评报告"
        >
          <el-upload
            v-model:file-list="formData.baseInforLevelFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseInforLevelFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="!['01','02','03'].includes(basicInfoData?.baseProjSetProg)||baseProjSetYear*1<2023" :span="8">
        <el-form-item
          v-if="['01','02'].includes(approvalInfoData?.equalProtectionLevel)||baseProjSetYear*1<2023"
          label="商业密码应用评估报告"
        >
          <el-upload
            v-model:file-list="formData.basePasswAssessFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.basePasswAssessFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item
          v-else
          label="商业密码应用评估报告"
          prop="basePasswAssessFile"
        >
          <el-upload
            v-model:file-list="formData.basePasswAssessFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.basePasswAssessFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item
          label="第三方验收测试报告"
        >
          <el-upload
            v-model:file-list="formData.baseThirdAcceptFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseThirdAcceptFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="!['01','02','03','04'].includes(basicInfoData?.baseProjSetProg)||baseProjSetYear*1<2023" :span="8">
        <el-form-item
          v-if="baseProjSetYear*1<2023||['05','06','07'].includes(basicInfoData?.baseProjSetProg)"
          label="用户使用报告"
        >
          <el-upload
            v-model:file-list="formData.baseUserConsFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseUserConsFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item
          v-else
          label="用户使用报告"
          prop="baseUserConsFile"
        >
          <el-upload
            v-model:file-list="formData.baseUserConsFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseUserConsFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item
          label="监理总结报告"
        >
          <el-upload
            v-model:file-list="formData.baseEstaSummFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseEstaSummFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="!['01','02','03','04'].includes(basicInfoData?.baseProjSetProg)||baseProjSetYear*1<2023" :span="8">
        <el-form-item
          v-if="basicInfoData?.baseProjSetProg==='07'&&baseProjSetYear*1>=2023"
          label="运维总结报告"
          prop="baseOperatMaintenSummFile"
        >
          <el-upload
            v-model:file-list="formData.baseOperatMaintenSummFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseOperatMaintenSummFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item
          v-else
          label="运维总结报告"
        >
          <el-upload
            v-model:file-list="formData.baseOperatMaintenSummFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseOperatMaintenSummFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="basicInfoData?.baseProjSetProg==='07'" :span="8">
        <el-form-item
          label="终验意见"
          prop="baseFinalExpertOpinionFile"
        >
          <el-upload
            v-model:file-list="formData.baseFinalExpertOpinionFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseFinalExpertOpinionFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item
          label="项目延期申请表"
        >
          <el-upload
            v-model:file-list="formData.baseEngineerPostpoFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseEngineerPostpoFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item
          label="变更报告"
        >
          <el-upload
            v-model:file-list="formData.baseEngineerAlterFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseEngineerAlterFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item
          label="变更批复文件"
        >
          <el-upload
            v-model:file-list="formData.baseChanFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseChanFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
          >
            <el-button type="primary" class="mr-4" plain>上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <actual-performance-indicators-dialog
    :visible="projectContentDialogData.visible"
    :data="projectContentDialogData.data"
    @set-content="setContent"
    @close="projectContentDialogData.visible=false"
  />
</template>
