<template>
    <el-form :model="rectificationMeasuresData" :rules="rules" ref="form2Ref" label-width="auto">
        <el-form-item label="电网侧实际采取措施:" prop="reportProcessContentSjcq" >
            <el-checkbox-group v-model="rectificationMeasuresData.reportProcessContentSjcq" :disabled="prop.disabled">
                <el-checkbox v-for="(item, ind) in SjcqList" :key="ind" :label="item.code">{{ item.name }}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>

        <el-divider></el-divider>

        <el-form-item label="用户侧实际采取措施:" prop="userReportProcessContentSjcq" >
            <el-checkbox-group v-model="rectificationMeasuresData.userReportProcessContentSjcq" :disabled="prop.disabled">
                <el-checkbox v-for="(item, ind) in SjcqList" :key="ind" :label="item.code">{{ item.name }}</el-checkbox>
            </el-checkbox-group>
        </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="rectificationMeasuresData.fileList"
                ref="upload"
                action=""
                v-if="!prop.disabled"
                :limit="1"
                :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.fileNameSjcq }}
            </el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
import { useDictData } from '@/stores/dictData'
import { uploadFile, takeAction } 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 rectificationMeasuresData: any = ref({
    reportProcessContentSjcq: [],
    userReportProcessContentSjcq: [],
    fileNameSjcq: '', //实际采取措施报告文件名称
    filePathSjcq: '', //	实际采取措施报告文件路径
    fileList: []
})
const rules = {
    reportProcessContentSjcq: [{ required: true, message: '请选择电网侧实际采取措施', trigger: 'change' }],
    userReportProcessContentSjcq: [{ required: true, message: '请选择用户侧实际采取措施', trigger: 'change' }],
    fileList: [{ required: true, message: '请上传文件', trigger: 'change' }]
}
// 电网侧整改治理措施
const SjcqList = dictData.getBasicData('Plan_Take', ['Rectify_Orders'])

// 上传
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.filePathSjcq)
    let blob = await response.blob()
    let a = document.createElement('a')
    a.href = window.URL.createObjectURL(blob)
    a.download = prop.List.fileNameSjcq
    a.click()
    a.remove()
}
onMounted(() => {
    if (prop.List.filePathSjcq != null) {
        rectificationMeasuresData.value = prop.List
    }
})

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

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