<script name="accessory" setup>
import { ref } from 'vue'
import { fileFormatVerification, handleFileSuccess, reviewFileParam, handleFilePreview, fileTypes, fileDesc } from '@/utils/uploadAction.js'
import store from '@/store'
defineProps({
  detailData: {
    type: Object,
    default: () => {
      return {}
    }
  },
  projectType: {
    type: String,
    default: ''
  },
  declareAmount: {
    type: Number
  }
})
const uploadUrl = store.dictStore.uploadUrl,
  formData = ref({
    constructionPlanFile: [],
    preliminaryPlanFile: [],
    mainResponsibilitiesApplicantFile: [],
    supportingMaterialsFile: [],
    calculationTotalInvestmentFile: [],
    projectApplicationForm: [],
    mainAccusationDoc: []
  }),
  formRef = ref(),
  rules = {
    preliminaryPlanFile: [
      { required: true, message: '请上传初步方案', trigger: 'blur' }
    ],
    supportingMaterialsFile: [
      { required: true, message: '请上传佐证材料', trigger: 'blur' }
    ],
    calculationTotalInvestmentFile: [
      { required: true, message: '请上传项目总投资测算明细', trigger: 'blur' }
    ],
    mainResponsibilitiesApplicantFile: [
      { required: true, message: '请上传申报单位主要职责(单位三定方案)', trigger: 'blur' }
    ],
    projectApplicationForm: [{ required: true, message: '请上传项目申报书' }],
    baseResearchReportFile: [{ required: true, message: '请上传可行性研究报告' }],
    constructionPlanFile: [{ required: true, message: '请上传建设方案' }],
    mainAccusationDoc: [{ required: true, message: '请上传申报单位主要职责(单位三定方案)', trigger: 'blur' }]
  },
  // 校验
  validForm = (callback) => {
    formRef.value.validate(valid => {
      callback(valid)
    })
  },
  // 回显
  setFormData = (data) => {
    formData.value = {
      constructionPlanFile: data.constructionPlanFile ? reviewFileParam(JSON.parse(data.constructionPlanFile)) : [],
      preliminaryPlanFile: data.preliminaryPlanFile ? reviewFileParam(JSON.parse(data.preliminaryPlanFile)) : [],
      mainResponsibilitiesApplicantFile: data.mainResponsibilitiesApplicantFile ? reviewFileParam(JSON.parse(data.mainResponsibilitiesApplicantFile)) : [],
      supportingMaterialsFile: data.supportingMaterialsFile ? reviewFileParam(JSON.parse(data.supportingMaterialsFile)) : [],
      baseResearchReportFile: data.baseResearchReportFile ? reviewFileParam(JSON.parse(data.baseResearchReportFile)) : [],
      calculationTotalInvestmentFile: data.calculationTotalInvestmentFile ? reviewFileParam(JSON.parse(data.calculationTotalInvestmentFile)) : [],
      projectApplicationForm: data.projectApplicationForm ? reviewFileParam(JSON.parse(data.projectApplicationForm)) : [],
      baseProjOtherFile: data.baseProjOtherFile ? reviewFileParam(JSON.parse(data.baseProjOtherFile)) : [],
      mainAccusationDoc: data.mainAccusationDoc ? reviewFileParam(JSON.parse(data.mainAccusationDoc)) : []
    }
  }
defineExpose({ validForm, formData, setFormData })
</script>

<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="rules"
    label-position="right"
    label-width="180px"
    label-suffix=":"
    scroll-to-error
  >
    <el-row :gutter="40">
      <el-col :span="12">
        <el-form-item label="初步设计方案">
          <el-upload
            v-model:file-list="formData.preliminaryPlanFile"
            class="w-full"
            :action="uploadUrl"
            :limit="1"
            :on-success="res => handleFileSuccess(res, formData.preliminaryPlanFile, true)"
            :before-upload="file=>fileFormatVerification(file, {
              types: fileTypes
            })"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" plain>选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="false" :span="12">
        <el-form-item
          label="佐证材料"
          prop="supportingMaterialsFile"
        >
          <el-upload
            v-model:file-list="formData.supportingMaterialsFile"
            class="upload-demo"
            :action="uploadUrl"
            multiple
            :on-success="res => handleFileSuccess(res, formData.supportingMaterialsFile)"
            :before-upload="file=>fileFormatVerification(file, {
              types: fileTypes
            })"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :limit="10"
            :on-preview="handleFilePreview"
          >
            <el-button plain type="primary">选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="false" :span="12">
        <el-form-item label="项目总投资测算明细" prop="calculationTotalInvestmentFile">
          <el-upload
            ref="investmentCalculationDetailsRef"
            v-model:file-list="formData.calculationTotalInvestmentFile"
            class="upload-demo"
            :action="uploadUrl"
            multiple
            :on-success="res => handleFileSuccess(res, formData.calculationTotalInvestmentFile)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :limit="10"
            :on-preview="handleFilePreview"
          >
            <el-button plain type="primary">选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="false" :span="12">
        <el-form-item
          label="申报单位主要职责(单位三定方案)"
          prop="mainResponsibilitiesApplicantFile"
        >
          <el-upload
            v-model:file-list="formData.mainResponsibilitiesApplicantFile"
            class="upload-demo"
            :action="uploadUrl"
            multiple
            :on-success="res => handleFileSuccess(res, formData.mainResponsibilitiesApplicantFile)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :limit="10"
            :on-preview="handleFilePreview"
          >
            <el-button plain type="primary">选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="projectType!=='03'&&false" :span="12">
        <el-form-item label="项目申报书" prop="projectApplicationForm">
          <el-upload
            v-model:file-list="formData.projectApplicationForm"
            class="w-full"
            :action="uploadUrl"
            :limit="1"
            :on-success="res => handleFileSuccess(res, formData.projectApplicationForm, true)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" plain>选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="projectType!=='03'" :span="12">
        <el-form-item label="可行性研究报告" prop="baseResearchReportFile">
          <el-upload
            v-model:file-list="formData.baseResearchReportFile"
            class="w-full"
            :action="uploadUrl"
            :limit="1"
            :on-success="res => handleFileSuccess(res, formData.baseResearchReportFile, true)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" plain>选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col v-if="$route.name==='declarePlan'||[10012,10013,10016].includes(detailData?.status)" :span="12">
        <el-form-item label="建设方案" prop="constructionPlanFile">
          <el-upload
            v-model:file-list="formData.constructionPlanFile"
            class="w-full"
            :action="uploadUrl"
            :limit="1"
            :on-success="res => handleFileSuccess(res, formData.constructionPlanFile, true)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :on-preview="handleFilePreview"
          >
            <el-button type="primary" plain>选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="其他附件">
          <el-upload
            ref="baseProjOtherFileRef"
            v-model:file-list="formData.baseProjOtherFile"
            class="upload-demo"
            :action="uploadUrl"
            multiple
            :on-success="res => handleFileSuccess(res, formData.baseProjOtherFile)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :limit="10"
            :on-preview="handleFilePreview"
          >
            <el-button plain type="primary">选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item v-if="declareAmount>=1000" label="申报单位主要职责文件（单位三定职责）" prop="mainAccusationDoc">
          <el-upload
            ref="mainAccusationDocRef"
            v-model:file-list="formData.mainAccusationDoc"
            class="upload-demo"
            :action="uploadUrl"
            multiple
            :on-success="res => handleFileSuccess(res, formData.mainAccusationDoc)"
            :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
            :accept="fileTypes.map(i=>`.${i}`).join(',')"
            :limit="10"
            :on-preview="handleFilePreview"
          >
            <el-button plain type="primary">选择文件</el-button>
            <template #tip>
              <div class="el-upload__tip">
                支持{{ fileDesc }}文件
              </div>
            </template>
          </el-upload>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<style lang="less">
.el-upload-list{
  width: 100%;
}
</style>
