<template>
    <el-form
        :model="rectificationMeasuresData"
        :rules="rules"
        ref="form2Ref"
        label-width="auto"
      
    >
        <el-form-item label="电网侧整改治理措施:" prop="reportProcessContentJhzg"  >
            <el-checkbox-group v-model="rectificationMeasuresData.reportProcessContentJhzg" :disabled="prop.disabled">
                <el-checkbox v-for="item in JhzgList" :label="item.code">{{ item.name }}</el-checkbox>
            </el-checkbox-group>
        </el-form-item>

        <el-divider></el-divider>

        <el-form-item label="用户侧整改治理措施:" prop="userReportProcessContentJhzg"  >
            <el-checkbox-group v-model="rectificationMeasuresData.userReportProcessContentJhzg" :disabled="prop.disabled">
                <el-checkbox v-for="item in JhzgList" :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.fileNameJhzg }}
                </el-button>
            </el-form-item>
    
    </el-form>
</template>
<script setup lang="ts">
import { useDictData } from '@/stores/dictData'
import { uploadFile, correctiveAction } 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({
    reportProcessContentJhzg: [],
    userReportProcessContentJhzg: [],
    fileNameJhzg: '', //计划整改文件名称
    filePathJhzg: '', //	计划整改文件路径
    fileList: []
})
const rules = {
    reportProcessContentJhzg: [{ required: true, message: '请选择电网侧整改治理措施', trigger: 'change' }],
    userReportProcessContentJhzg: [{ required: true, message: '请选择用户侧整改治理措施', trigger: 'change' }],
    fileList: [{ required: true, message: '请上传文件', trigger: 'change' }]
}
// 电网侧整改治理措施
const JhzgList = dictData.getBasicData('Plan_Take')

// 上传
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.filePathJhzg)
    let blob = await response.blob()
    let a = document.createElement('a')
    a.href = window.URL.createObjectURL(blob)
    a.download = prop.List.fileNameJhzg
    a.click()
    a.remove()
}

onMounted(() => {
    if (prop.List.filePathJhzg != 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.filePathJhzg = res.data.minFileUrl
                rectificationMeasuresData.value.fileNameJhzg = res.data.minFileName
            })

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