<script name="applyDialog" setup>
import { ref, getCurrentInstance, watch } from 'vue'
import { changFilesParam, fileFormatVerification, handleFileSuccess, fileTypes, fileDesc, handleFilePreview } from '@/utils/uploadAction.js'
import { delayedApply } from '@/http/apis/declareMange/delayApply'
import store from '@/store'
const { proxy } = getCurrentInstance(),
  uploadUrl = store.dictStore.uploadUrl,
  props = defineProps({
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    data: Object
  }),
  emits = defineEmits(['close']),
  loading = ref(false),
  dialogForm = ref({
    supportingMaterials: []
  }),
  rules = {
    'delayedMonth': [{ required: true, message: '请输入延期时长' }],
    'delayedReason': [{ required: true, message: '请输入延期理由' }],
    'supportingMaterials': [{ required: true, message: '请上传' }]
  },
  dialogFormRef = ref(),
  submit = async (formEl) => {
    if (!formEl) {
      return
    }
    formEl.validate(async (valid) => {
      if (valid) {
        loading.value = true
        try {
          const postData = {
            ...dialogForm.value,
            supportingMaterials: dialogForm.value?.supportingMaterials?.length && JSON.stringify(changFilesParam(dialogForm.value.supportingMaterials)) || undefined,
            projectId: props.data.id
          }
          await delayedApply(postData)
          proxy.$message.success('提交成功！')
          loading.value = false
          emits('close', true)
        } catch (e) {
          loading.value = false
        }
      }
    }
    )
  }
watch(
  () => props.visible,
  async val => {
    if (val) {
      console.log('props.data', props.data)
    } else {
      dialogForm.value = { higherLineSuperOrgReviewComments: [] }
    }
  }
)
</script>

<template>
  <el-dialog
    :model-value="visible"
    title="申请延期"
    width="600px"
    destroy-on-close
    @close="emits('close')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :rules="rules"
      label-width="auto"
      status-icon
      class="mt-16"
    >
      <el-form-item label="延期时长" prop="delayedMonth">
        <el-input
          v-model="dialogForm.delayedMonth"
          placeholder="请填写需要延长几个月"
          @input="dialogForm.delayedMonth=dialogForm.delayedMonth?.replace(/[^\d]/g,'')"
        >
          <template #suffix>月</template>
        </el-input>
      </el-form-item>
      <el-form-item label="延期理由" prop="delayedReason">
        <el-input
          v-model="dialogForm.delayedReason"
          type="textarea"
          maxlength="1000"
          show-word-limit
          placeholder="请填写"
        />
      </el-form-item>
      <el-form-item label="佐证材料:" prop="supportingMaterials">
        <el-upload
          v-model:file-list="dialogForm.supportingMaterials"
          class=" w-full"
          :action="uploadUrl"
          :limit="1"
          :on-success="res => handleFileSuccess(res, dialogForm.supportingMaterials, 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-form>
    <template #footer>
      <el-button
        type="primary"
        :loading="loading"
        @click="submit(dialogFormRef)"
      >
        提交
      </el-button>
      <el-button
        @click="emits('close')"
      >
        关闭
      </el-button>
    </template>
  </el-dialog>
</template>
