<script setup name="upLoadZxDialog">
import { watch, ref, getCurrentInstance } from 'vue'
import store from '@/store'
import {
  fileFormatVerification,
  handleFileSuccess,
  handleFilePreview,
  handleFileError,
  changFilesParam,
  handleFileExceed
} from '@/utils/uploadAction.js'
import { doAudit } from '@/http/apis/performanceEvaluation/inquirySheetManage'

const uploadUrl = store.dictStore.uploadUrl,
  { proxy } = getCurrentInstance(),
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: Object
  }),
  emits = defineEmits(['close']),
  form = ref({
    consultationForm: []
  }),
  formRef = ref(),
  submit = async formEl => {
    if (!formEl) {
      return
    }
    await formEl.validate(async valid => {
      if (valid) {
        const postData = {
          status: 5,
          formId: props.data.id,
          consultationForm: form.value.consultationForm?.length && JSON.stringify(changFilesParam(form.value.consultationForm)) || undefined
        }
        await doAudit({
          ...postData
        })
        proxy.$message.success('提交成功！')
        emits('close', true)
      }
    })
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      form.value.consultationForm = []
    }
  }
)

</script>
<template>
  <el-dialog
    :model-value="visible"
    title="上传咨询单"
    width="600px"
    destroy-on-close
    @close="emits('close')"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="120px"
      label-suffix=":"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="上传新咨询单" prop="consultationForm" :rules="[{ required: true, message: '请上传文件', trigger: 'blur' }]">
            <el-upload
              v-model:file-list="form.consultationForm"
              class="w-full"
              :action="uploadUrl"
              :limit="1"
              :on-exceed="handleFileExceed"
              :on-success="res => handleFileSuccess(res, form.consultationForm, true)"
              :on-error="handleFileError"
              :on-preview="handleFilePreview"
              :before-upload="
                file =>
                  fileFormatVerification(file, {
                    types: [
                      'doc',
                      'docx',
                      'wps',
                    ]
                  })
              "
              accept=".doc,.docx,.wps"
            >
              <el-button type="primary" class="mr-4">上传附件</el-button>
              <template #tip>
                <div class="el-upload__tip">支持word文件</div>
                <a class="ml-4" href="/丽水市本级政府投资信息化项目绩效评价咨询单.wps" download="丽水市本级政府投资信息化项目绩效评价咨询单.wps">下载模板</a>
              </template>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submit(formRef)">
        提交
      </el-button>
      <el-button @click="emits('close')">
        关闭
      </el-button>
    </template>
  </el-dialog>
</template>
