<script name="projectApprovalInfo" setup>
import { ref, watch } from 'vue'
import { fileFormatVerification, handleFileError, handleFileSuccess, handleFilePreview, reviewFileParam, fileTypes, fileDesc } from '@/utils/uploadAction'
import store from '@/store'

const props = defineProps({
    detail: {
      type: Object
    },
    basicInfoData: {
      type: Object
    },
    dictionaryList: {
      type: Array,
      default: () => []
    },
    isSupplemented: {
      type: Boolean,
      default: false
    }
  }),
  uploadUrl = store.dictStore.uploadUrl,
  formRef = ref(),
  formData = ref({
  }),
  currentRules = ref([]),
  copyData = ref()
defineExpose({ formRef, formData })
watch(() => props.detail, val => {
  if (val) {
    if (props.isSupplemented) {
      copyData.value = val.approve || {}
    }
    formData.value = {
      ...val.approve,
      baseReviewCommentsFile: val.approve?.baseReviewCommentsFile && reviewFileParam(JSON.parse(val.approve.baseReviewCommentsFile)) || [],
      approvalFile: val.approve?.approvalFile && reviewFileParam(JSON.parse(val.approve.approvalFile)) || [],
      preliminaryDesignScheme: val.approve?.preliminaryDesignScheme && reviewFileParam(JSON.parse(val.approve.preliminaryDesignScheme)) || [],
      preliminaryDesignFile: val.approve?.preliminaryDesignFile && reviewFileParam(JSON.parse(val.approve.preliminaryDesignFile)) || []
    }
  }
})
const fileRule = [{ required: true, message: '请上传' }],
  emits = defineEmits(['getApprovalInfoData'])
watch(() => props.basicInfoData, val => {
  if (val?.baseProjIsConfidentiality === '02') {
    currentRules.value = {}
  } else {
    currentRules.value = {
      baseReviewResults: [{ required: true, message: '请选择评审结果' }],
      equalProtectionLevel: [{ required: true, message: '请选择等保定级' }],
      baseReviewOpinion: [{ required: true, message: '请填写评审意见' }],
      baseReviewCommentsFile: fileRule,
      approvalFile: fileRule,
      baseExpertTotalMoney: [{ required: true, message: '请填写建议总投资' }],
      baseExpertYearMoney: [{ required: true, message: '请填写建议年度预算' }],
      baseInitialReviewTotalMoney: [{ required: true, message: '请填写建议批复总投资' }],
      baseProjReplyAmount: [{ required: true, message: '请填写建议批复年度预算' }],
      releaseYearMoney: [{ required: true, message: '请填写年度预算下达金额' }],
      preliminaryDesignScheme: fileRule,
      preliminaryDesignFile: fileRule
    }
  }
  formRef.value?.clearValidate()
}, { deep: true, immediate: true })
watch(() => formData.value, val => {
  emits('getApprovalInfoData', val)
}, { 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 v-if="basicInfoData?.baseProjSetProg!=='01'" :span="8">
        <el-form-item label="评审结果" prop="baseReviewResults">
          <el-select v-model="formData.baseReviewResults" class="w-full" :disabled="copyData?.baseReviewResults&&isSupplemented">
            <el-option
              v-for="(item,index) in dictionaryList?.filter(i => i.type === 'REVIEW_RESULTS')"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-if="basicInfoData?.baseProjIsConfidentiality === '01'&&!['01','02','03'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item
          v-if="basicInfoData?.baseConstructionType?.includes('01')&&basicInfoData?.baseProjSetProg!=='04'"
          key="equalProtectionLevel1"
          label="等保定级"
          prop="equalProtectionLevel"
        >
          <el-select v-model="formData.equalProtectionLevel" class="w-full" :disabled="copyData?.equalProtectionLevel&&isSupplemented">
            <el-option
              v-for="(item,index) in dictionaryList?.filter(i => i.type === 'EQUAL_PROTECTION_RATING')"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          v-else
          key="equalProtectionLevel2"
          label="等保定级"
        >
          <el-select v-model="formData.equalProtectionLevel" class="w-full" :disabled="copyData?.equalProtectionLevel&&isSupplemented">
            <el-option
              v-for="(item,index) in dictionaryList?.filter(i => i.type === 'EQUAL_PROTECTION_RATING')"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-if="basicInfoData?.baseProjSetProg!=='01'" :span="24">
        <el-form-item label="评审意见" prop="baseReviewOpinion">
          <el-input
            v-model="formData.baseReviewOpinion"
            type="textarea"
            show-word-limit
            :maxlength="500"
            placeholder="请填写"
            :disabled="copyData?.baseReviewOpinion&&isSupplemented"
          />
        </el-form-item>
      </el-col>
      <el-col v-if="basicInfoData?.baseProjSetProg!=='01'" :span="8">
        <el-form-item
          label="评审意见附件"
          prop="baseReviewCommentsFile"
        >
          <el-upload
            v-model:file-list="formData.baseReviewCommentsFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.baseReviewCommentsFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :limit="10"
            :on-preview="handleFilePreview"
            :before-remove="()=>!(copyData?.baseReviewCommentsFile&&!!(JSON.parse(copyData?.baseReviewCommentsFile)?.length)&&isSupplemented)"
          >
            <el-button type="primary" class="mr-4" :disabled="copyData?.baseReviewCommentsFile&&isSupplemented">上传文件</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)" :span="8">
        <el-form-item
          label="立项批复文件"
          prop="approvalFile"
        >
          <el-upload
            v-model:file-list="formData.approvalFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.approvalFile)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :limit="10"
            :on-preview="handleFilePreview"
            :before-remove="()=>!(copyData?.approvalFile&&!!(JSON.parse(copyData?.approvalFile)?.length)&&isSupplemented)"
          >
            <el-button type="primary" class="mr-4" :disabled="copyData?.approvalFile&&isSupplemented">上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="!['01','03'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item label="建议总投资" prop="baseExpertTotalMoney">
          <el-input-number
            v-model="formData.baseExpertTotalMoney"
            class="input-amount"
            placeholder="请填写"
            :min="0.000001"
            :controls="false"
            :disabled="copyData?.baseExpertTotalMoney&&isSupplemented"
            @mousewheel.prevent
          >
            <template #suffix>万元</template>
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col v-if="!['01','03'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item label="建议年度预算" prop="baseExpertYearMoney">
          <el-input-number
            v-model="formData.baseExpertYearMoney"
            class="input-amount"
            placeholder="请填写"
            :min="0.000001"
            :controls="false"
            :disabled="copyData?.baseExpertYearMoney&&isSupplemented"
            @mousewheel.prevent
          >
            <template #suffix>万元</template>
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col v-if="!['01','02','03'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item label="建议批复总投资" prop="baseInitialReviewTotalMoney">
          <el-input-number
            v-model="formData.baseInitialReviewTotalMoney"
            class="input-amount"
            placeholder="请填写"
            :min="0.000001"
            :controls="false"
            :disabled="copyData?.baseInitialReviewTotalMoney&&isSupplemented"
            @mousewheel.prevent
          >
            <template #suffix>万元</template>
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col v-if="!['01','02','03'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item label="建议批复年度预算" prop="baseProjReplyAmount">
          <el-input-number
            v-model="formData.baseProjReplyAmount"
            class="input-amount"
            placeholder="请填写"
            :min="0.000001"
            :controls="false"
            :disabled="copyData?.baseProjReplyAmount&&isSupplemented"
            @mousewheel.prevent
          >
            <template #suffix>万元</template>
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col v-if="['05','06','07','00'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item label="年度预算下达金额" prop="releaseYearMoney">
          <el-input-number
            v-model="formData.releaseYearMoney"
            class="input-amount"
            placeholder="请填写"
            :min="0.000001"
            :controls="false"
            :disabled="copyData?.releaseYearMoney&&isSupplemented"
            @mousewheel.prevent
          >
            <template #suffix>万元</template>
          </el-input-number>
        </el-form-item>
      </el-col>
      <el-col v-if="formData.baseProjReplyAmount>=5000&&['04','05','06','07','00'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item
          v-if="basicInfoData?.baseProjSetProg!=='04'"
          key="preliminaryDesignScheme1"
          label="初步设计方案"
          prop="preliminaryDesignScheme"
        >
          <el-upload
            v-model:file-list="formData.preliminaryDesignScheme"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.preliminaryDesignScheme)"
            :on-error="handleFileError"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            multiple
            :limit="10"
            :on-preview="handleFilePreview"
            :before-remove="()=>!(copyData?.preliminaryDesignScheme&&!!(JSON.parse(copyData?.preliminaryDesignScheme)?.length)&&isSupplemented)"
          >
            <el-button type="primary" class="mr-4" :disabled="copyData?.preliminaryDesignScheme&&isSupplemented">上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item
          v-else
          key="preliminaryDesignScheme2"
          label="初步设计方案"
        >
          <el-upload
            v-model:file-list="formData.preliminaryDesignScheme"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.preliminaryDesignScheme)"
            :on-error="handleFileError"
            :before-upload="
              file =>
                fileFormatVerification(file, {
                  types: [
                    'application/pdf',
                  ]
                })
            "
            accept=".pdf"
            multiple
            :limit="10"
            :on-preview="handleFilePreview"
            :before-remove="()=>!(copyData?.preliminaryDesignScheme&&!!(JSON.parse(copyData?.preliminaryDesignScheme)?.length)&&isSupplemented)"
          >
            <el-button type="primary" class="mr-4" :disabled="copyData?.preliminaryDesignScheme&&isSupplemented">上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持格式：pdf</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="formData.baseProjReplyAmount>=5000&&['04','05','06','07','00'].includes(basicInfoData?.baseProjSetProg)" :span="8">
        <el-form-item
          v-if="basicInfoData?.baseProjSetProg!=='04'"
          key="preliminaryDesignFile1"
          label="初步设计方案批复函"
          prop="preliminaryDesignFile"
        >
          <el-upload
            v-model:file-list="formData.preliminaryDesignFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.preliminaryDesignFile)"
            :on-error="handleFileError"
            :before-upload="
              file =>
                fileFormatVerification(file, {
                  types: [
                    'application/pdf',
                  ]
                })
            "
            accept=".pdf"
            multiple
            :limit="10"
            :on-preview="handleFilePreview"
            :before-remove="()=>!(copyData?.preliminaryDesignFile&&!!(JSON.parse(copyData?.preliminaryDesignFile)?.length)&&isSupplemented)"
          >
            <el-button type="primary" class="mr-4" :disabled="copyData?.preliminaryDesignFile&&isSupplemented">上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持格式：pdf</div>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item
          v-else
          key="preliminaryDesignFile2"
          label="初步设计方案批复函"
        >
          <el-upload
            v-model:file-list="formData.preliminaryDesignFile"
            class="w-full"
            :action="uploadUrl"
            :on-success="res => handleFileSuccess(res, formData.preliminaryDesignFile)"
            :on-error="handleFileError"
            :before-upload="
              file =>
                fileFormatVerification(file, {
                  types: [
                    'application/pdf',
                  ]
                })
            "
            accept=".pdf"
            multiple
            :limit="10"
            :on-preview="handleFilePreview"
            :before-remove="()=>!(copyData?.preliminaryDesignFile&&!!(JSON.parse(copyData?.preliminaryDesignFile)?.length)&&isSupplemented)"
          >
            <el-button type="primary" class="mr-4" :disabled="copyData?.preliminaryDesignFile&&isSupplemented">上传文件</el-button>
            <template #tip>
              <div class="el-upload__tip">支持格式：pdf</div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>

</template>

