<template>
    <el-form :model="effectivenessAnalysisData" :rules="rules" ref="form2Ref" label-width="auto">
        <el-form-item label="成效分析概述:" style="margin-top: 10px" prop="descriptionZlxg">
            <el-input
                :disabled="prop.disabled"
                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"
                v-if="!prop.disabled"
                :on-exceed="handleExceed"
                :auto-upload="false"
            >
                <template #trigger>
                    <el-button type="primary">上传文件</el-button>
                </template>
            </el-upload>
            <el-button type="primary" link @click="download" v-else>
                {{ prop.List.fileNameZlxg }}
            </el-button>
        </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, onMounted } from 'vue'
const prop = defineProps({
    addData: {
        type: Object,
        default: () => {}
    },
    List: {
        type: Object,
        default: () => {}
    },
    disabled: {
        type: Boolean
    }
})
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 download = async () => {
    // window.open(addForm.value.ifile)
    let response = await fetch(prop.List.filePathZlxg)
    let blob = await response.blob()
    let a = document.createElement('a')
    a.href = window.URL.createObjectURL(blob)
    a.download = prop.List.fileNameZlxg
    a.click()
    a.remove()
}
onMounted(() => {
    if (prop.List.filePathZlxg != null) {
        effectivenessAnalysisData.value = prop.List
    }
})

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>
