import createAxios from '@/utils/request'
import { genFileId, ElMessage, ElNotification } from 'element-plus'

//事件报告
// export function getEventReport(data) {
//     return createAxios({
//         url: '/event-boot/report/getEventReport',
//         method: 'post',
//         data
//     })
// }

export function getEventReport(data) {
    return createAxios({
        url: '/event-boot/report/createEventReport',
        method: 'post',
        data,
        responseType: 'blob'
    }).then(async res => {
        let load: any = await readJsonBlob(res)
        if (load.code) {
            if (load.data.code == 'A0011') {
                ElMessage.warning('下载失败!')
            } else {
                ElMessage.warning(load.data.message)
            }
        } else {
            return res
        }
    })
}


// 生成报告
export function getAreaReport(data) {
    return createAxios({
        url: '/event-boot/report/getAreaReport',
        method: 'post',
        data,
        responseType: 'blob'
    }).then(async res => {
        let load: any = await readJsonBlob(res)
        if (load.code) {
            if (load.data.code == 'A0011') {
                ElMessage.warning('下载失败!')
            } else {
                ElMessage.warning(load.data.message)
            }
        } else {
            return res
        }
    })
}
async function readJsonBlob(blob) {
    try {
        // 1. Blob.text() 读取二进制 → 直接转为 字符串（自动处理编码）
        const jsonStr = await blob.text()
        // 2. JSON.parse 解析字符串 → 得到可用的 JS 对象/数组
        const jsonData = JSON.parse(jsonStr)
        // 3. 拿到数据，后续随便用
        return {
            code: true,
            data: jsonData
        }
    } catch (err) {
        return {
            code: false,
            data: {}
        }
        // console.error('解析Blob的JSON数据失败：', err)
    }
}

//查询所有模板
export function getList(data) {
    return createAxios({
        url: '/system-boot/EventTemplate/getList',
        method: 'post',
        data
    })
}
export function selectReleation(data) {
    return createAxios({
        url: '/system-boot/EventTemplate/selectReleation',
        method: 'post',
        params: data
    })
}
export function getLineExport(data) {
    return createAxios({
        url: '/event-boot/report/getLineExport',
        method: 'post',
        data: data,
        responseType: 'blob'
    }).then(async res => {
        let load: any = await readJsonBlob(res)
        if (load.code) {
            if (load.data.code == 'A0011') {
                ElMessage.warning('下载失败!')
            } else {
                ElMessage.warning(load.data.message)
            }
        } else {
            return res
        }
    })
}
export function getVoltage(data: any) {
    return createAxios({
        url: '/event-boot/report/getVoltage',
        method: 'post',
        data
    })
}
export function getGeneralSituation(data: any) {
    return createAxios({
        url: '/event-boot/report/getGeneralSituation',
        method: 'post',
        data
    })
}
export function getTransientValue(data: any) {
    return createAxios({
        url: '/event-boot/transient/getTransientValue',
        method: 'post',
        data
    })
}
// 周报导出
export function getExport(data: any) {
    return createAxios({
        url: '/event-boot/report/getExport',
        method: 'post',
        data,
        responseType: 'blob'
    })
}
