<script setup name="upLoadReplayDialog">
import { watch, ref, getCurrentInstance } from 'vue'
import store from '@/store'
import {
  changFilesParam,
  fileFormatVerification,
  handleFileSuccess,
  handleFilePreview,
  fileTypes,
  fileDesc,
  reviewFileParam
} from '@/utils/uploadAction.js'
import { detail, fillResult } from '@/http/apis/performanceEvaluation/projectReform'
import Accessory from '@/components/accessory/index.vue'

const uploadUrl = store.dictStore.uploadUrl,
  { proxy } = getCurrentInstance(),
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: Object,
    isEdit: {
      type: Boolean,
      default: true
    }
  }),
  emits = defineEmits(['close']),
  formData = ref({
  }),
  loading = ref(false),
  rules = {
    rectifyResult: [{ required: true, message: '请输入' }]
  },
  formRef = ref(),
  submit = async (formEl) => {
    if (!formEl) return
    await formEl.validate(async valid => {
      if (valid) {
        loading.value = true
        const postData = {
          appraisalId: props.data.appraisalId,
          appraisalProjectId: formData.value.appraisalProjectId,
          appraisalProjectCode: formData.value.appraisalProjectCode,
          rectifyResult: formData.value.rectifyResult,
          rectifyResultAppendix: formData.value.rectifyResultAppendix?.length && JSON.stringify(changFilesParam(formData.value.rectifyResultAppendix))
        }
        try {
          await fillResult(postData)
          proxy.$message.success('提交成功')
          formData.value = {}
          loading.value = false
          emits('close', true)
        } catch (e) {
          loading.value = false
        }
      }
    })
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      const res = await detail(props.data.appraisalId, props.data.projectCode)
      formData.value = {
        ...res.data,
        appraisalProjectId: props.data.projectId,
        appraisalProjectCode: props.data.projectCode,
        rectifyResultAppendix: res.data.rectifyResultAppendix && reviewFileParam(JSON.parse(res.data.rectifyResultAppendix)) || []
      }
    }
  }
)

</script>
<template>
  <el-dialog
    :model-value="visible"
    :title="props.isEdit?'填写整改意见':'整改详情'"
    destroy-on-close
    @close="emits('close')"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-suffix=":"
      label-width="130"
    >
      <el-form-item label="整改意见">
        {{ formData.rectifyOpinion }}
      </el-form-item>
      <div class="mb-16">
        <el-alert
          v-if="formData.rectifyAuditResult===2||formData.rectifyAuditResult===1"
          :title="formData.rectifyAuditResult===2?'整改核查不通过':'整改核查通过'"
          :type="formData.rectifyAuditResult===2?'error':'success'"
          effect="dark"
          :closable="false"
        >
          <div>核查意见：{{ formData.rectifyAuditOpinion }}</div>
          <!--          <div class="flex">-->
          <!--            <span>附件：</span>-->
          <!--            <p v-for="(file,fileIndex) in formData.rectifyResultAppendix&&JSON.parse(formData.rectifyResultAppendix)" :key="fileIndex" class="mb-4">-->
          <!--              <accessory :file-name="file.originalFileName" :file-id="file.id" />-->
          <!--            </p>-->
          <!--          </div>-->
        </el-alert>
      </div>
      <el-form-item label="整改结果说明" prop="rectifyResult">
        <el-input
          v-if="isEdit"
          v-model="formData.rectifyResult"
          type="textarea"
          show-word-limit
          :maxlength="500"
        />
        <span v-else>{{ formData.rectifyResult }}</span>
      </el-form-item>
      <el-form-item label="相关材料">
        <el-upload
          v-if="isEdit"
          v-model:file-list="formData.rectifyResultAppendix"
          :action="uploadUrl"
          multiple
          :on-success="res => handleFileSuccess(res, formData.rectifyResultAppendix, true)"
          :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
          :accept="fileTypes.map(i=>`.${i}`).join(',')"
          :on-preview="handleFilePreview"
        >
          <el-button plain type="primary">选择文件</el-button>
          <template #tip>
            <div class="el-upload__tip">
              支持{{ fileDesc }}文件
            </div>
          </template>
        </el-upload>
        <template v-else>
          <p v-for="(file,fileIndex) in formData.rectifyResultAppendix?.length&&formData.rectifyResultAppendix" :key="fileIndex" class="mb-4">
            <accessory :file-name="file.name" :file-id="file.response.data.id" />
          </p>
        </template>
      </el-form-item>
    </el-form>
    <template v-if="props.isEdit" #footer>
      <div class="flex justify-center">
        <el-button
          class="ml-22"
          type="primary"
          :loading="loading"
          @click="submit(formRef)"
        >
          提交
        </el-button>
        <el-button @click="emits('close')">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>
