<script name="approvalDialog" setup>
import { ref, getCurrentInstance, watch } from 'vue'
import { fileFormatVerification, handleFileSuccess, handleFilePreview, handleFileError, fileTypes, fileDesc } from '@/utils/uploadAction.js'
import store from '@/store'
import { projectApprove } from '@/http/apis/projectStoreManage/annualPlanStore'
const { proxy } = getCurrentInstance(),
  uploadUrl = store.dictStore.uploadUrl,
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    title: {
      type: String,
      default: '立项批复'
    },
    data: Object
  }),
  emits = defineEmits(['close']),
  formRef = ref(),
  form = ref({}),
  rules = {
    approvedAmount: [{ required: true, message: '请输入批复金额', trigger: 'blur' }],
    approvedDate: [{ required: true, message: '请输入批复时间' }],
    buildCycle: [{ required: true, message: '请输入建设周期' }],
    approvedFileId: [{ required: true, message: '请输入批复文件' }],
    buildPlanFileId: [{ required: true, message: '请输入建设方案' }],
    approvalBudget: [{ required: true, message: '请输入建议批复年度预算', trigger: 'blur' }]
  },
  submit = async formEl => {
    if (!formEl) {
      return
    }
    await formEl.validate(async valid => {
      if (valid) {
        const postData = {
          ...form.value,
          projectId: props.data.projectId,
          buildCycle: form.value.buildCycle * 1,
          approvedFileId: form.value.approvedFileId?.map(i => i.response.data.id)[0] || undefined,
          buildPlanFileId: form.value.buildPlanFileId?.map(i => i.response.data.id)[0] || undefined
        }
        await projectApprove(postData)
        proxy.$message.success('提交成功！')
        emits('close', true)
      }
    })
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      console.log('props.data', props.data)
    } else {
      formRef.value.resetFields()
    }
  }
)
</script>

<template>
  <el-dialog
    :model-value="visible"
    :title="title"
    width="600px"
    destroy-on-close
    @close="emits('close')"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="140px"
      label-suffix=":"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item
            label="建议批复总投资"
            prop="approvedAmount"
          >
            <el-input
              v-model="form.approvedAmount"
              placeholder="请输入"
            >
              <template #append>万元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="批复时间"
            prop="approvedDate"
          >
            <el-date-picker
              v-model="form.approvedDate"
              type="date"
              placeholder="请选择"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="建议批复年度预算"
            prop="approvalBudget"
          >
            <el-input
              v-model="form.approvalBudget"
              placeholder="请输入"
            >
              <template #append>万元</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="建设周期"
            prop="buildCycle"
          >
            <el-input
              v-model="form.buildCycle"
              :min="0"
              placeholder="请填写建设周期，单位「月」"
              :controls="false"
            >
              <template #append>个月</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="批复文件"
            prop="approvedFileId"
          >
            <el-upload
              v-model:file-list="form.approvedFileId"
              class="w-full"
              :action="uploadUrl"
              :on-success="res => handleFileSuccess(res, form.approvedFileId, true)"
              :on-error="handleFileError"
              :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
              :accept="fileTypes.map(i=>`.${i}`).join(',')"
              multiple
              :limit="1"
              :on-preview="handleFilePreview"
            >
              <el-button type="primary">上传附件</el-button>
              <template #tip>
                <div class="el-upload__tip">支持{{ fileDesc }}文件</div>
              </template>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="建设方案"
            prop="buildPlanFileId"
          >
            <el-upload
              v-model:file-list="form.buildPlanFileId"
              class="w-full"
              :action="uploadUrl"
              :on-success="res => handleFileSuccess(res, form.buildPlanFileId, true)"
              :on-error="handleFileError"
              :before-upload="file=>fileFormatVerification(file, {types: fileTypes})"
              :accept="fileTypes.map(i=>`.${i}`).join(',')"
              multiple
              :limit="1"
              :on-preview="handleFilePreview"
            >
              <el-button 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 #footer>
      <el-button
        type="primary"
        @click="submit(formRef)"
      >
        提交
      </el-button>
      <el-button
        @click="emits('close')"
      >
        关闭
      </el-button>
    </template>
  </el-dialog>
</template>
