<template>
    <el-form :model="effectivenessAnalysisData" :rules="rules" ref="form2Ref" label-width="auto">
        <el-form-item label="成效分析概述:" style="margin-top: 10px" prop="descriptionZlxg">
            <el-input
                type="textarea"
                style="width: 400px"
                :autosize="{ minRows: 2, maxRows: 4 }"
                placeholder="请输入内容"
                v-model="effectivenessAnalysisData.descriptionZlxg"
            ></el-input>
        </el-form-item>

        <el-divider></el-divider>
        <el-form-item class="item" label="实际采取措施报告:" style="margin-top: 10px" prop="fileList">
            <el-upload
                v-model:file-list="effectivenessAnalysisData.fileList"
                ref="upload"
                action=""
                :limit="1"
                :on-exceed="handleExceed"
                :auto-upload="false"
            >
                <template #trigger>
                    <el-button type="primary">上传文件</el-button>
                </template>
            </el-upload>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { useDictData } from '@/stores/dictData'
import { uploadFile, effectAnalysis } from '@/api/process-boot/electricitymanagement'
import { UploadInstance, UploadProps, UploadRawFile, ElMessage, ElMessageBox } from 'element-plus'
import { genFileId } from 'element-plus'
import { ref, reactive } from 'vue'
const prop = defineProps({
    addData: {
        type: Object,
        default: () => {}
    }
})
const emit = defineEmits(['handleClose'])
const dictData = useDictData()
const upload = ref()
const form2Ref = ref()
const effectivenessAnalysisData: any = ref({
    descriptionZlxg: '',
    fileNameZlxg: '', //实际采取措施报告文件名称
    filePathZlxg: '', //实际采取措施报告文件路径
    fileList: []
})
const rules = {
    descriptionZlxg: [{ required: true, message: '请填写', trigger: 'blur' }],
    fileList: [{ required: true, message: '请上传文件', trigger: 'change' }]
}

// 上传
const handleExceed: UploadProps['onExceed'] = files => {
    upload.value!.clearFiles()
    const file = files[0] as UploadRawFile
    file.uid = genFileId()
    upload.value!.handleStart(file)
}

const submit = () => {
    form2Ref.value.validate(async (valid: any) => {
        if (valid) {
            let form = new FormData()
            form.append('file', effectivenessAnalysisData.value.fileList[0].raw)
            effectivenessAnalysisData.value.powerQualityProblemNo = prop.addData.powerQualityProblemNo
            await uploadFile(form).then((res: any) => {
                effectivenessAnalysisData.value.filePathZlxg = res.data.minFileUrl
                effectivenessAnalysisData.value.fileNameZlxg = res.data.minFileName
            })

            await effectAnalysis(effectivenessAnalysisData.value).then((res: any) => {
                ElMessage.success('提交成功')
                emit('handleClose')
            })
        }
    })
}
defineExpose({
    submit
})
</script>
<style lang="scss" scoped></style>
