import request from '@/utils/request'
import { genFileId, ElMessage, ElNotification } from 'element-plus'
// 主要监测点列表查询>>分页
export function mainLineList(data: any) {
    return request({
        url: '/cs-harmonic-boot/mainLine/list',
        method: 'post',
        data: data
    })
}
// 主要监测点指标越限详情
export function statLimitRateDetails(data: any) {
    return request({
        url: '/cs-harmonic-boot/mainLine/statLimitRateDetails',
        method: 'post',
        data: data
    })
}

// 查询监测点列表=全部>>不分页
export function cslineList(data: any) {
    return request({
        url: '/cs-device-boot/csline/list',
        method: 'post',
        data: data
    })
}

// 监测点详情 趋势图数据
export function trendData(data: any) {
    return request({
        url: '/cs-device-boot/csGroup/trendData',
        method: 'post',
        data: data
    })
}

// 每日越限占比统计
export function totalLimitStatisticsDetails(data: any) {
    return request({
        url: '/cs-harmonic-boot/totalLimitStatistics/details',
        method: 'post',
        data: data
    })
}

// 总体指标越限统计列表
export function totalLimitStatisticsList(data: any) {
    return request({
        url: '/cs-harmonic-boot/totalLimitStatistics/list',
        method: 'post',
        data: data
    })
}

// 总体指标越限统计数据
export function totalLimitStatisticsData(data: any) {
    return request({
        url: '/cs-harmonic-boot/totalLimitStatistics/data',
        method: 'post',
        data: data
    })
}

// 指标越限程度数据
export function limitExtentData(data: any) {
    return request({
        url: '/cs-harmonic-boot/limitRateDetailD/limitExtentData',
        method: 'post',
        data: data
    })
}
// 指标日趋势图数据
export function limitExtentDayData(data: any) {
    return request({
        url: '/cs-harmonic-boot/limitRateDetailD/limitExtentDayData',
        method: 'post',
        data: data
    })
}

// 指标越限明细日历数据
export function limitCalendarData(data: any) {
    return request({
        url: '/cs-harmonic-boot/limitRateDetailD/limitCalendarData',
        method: 'post',
        data: data
    })
}

//指标拟合图数据
export function fittingData(data: any) {
    return request({
        url: '/cs-device-boot/csGroup/fittingData',
        method: 'post',
        data: data
    })
}

//指标越限时间概率分布
export function limitTimeProbabilityData(data: any) {
    return request({
        url: '/cs-harmonic-boot/limitRateDetailD/limitTimeProbabilityData',
        method: 'post',
        data: data
    })
}

//指标越限程度概率分布
export function limitProbabilityData(data: any) {
    return request({
        url: '/cs-harmonic-boot/limitRateDetailD/limitProbabilityData',
        method: 'post',
        data: data
    })
}

// 电网侧指标越限统计列表
export function gridSideLimitStatisticsList(data: any) {
    return request({
        url: '/cs-harmonic-boot/gridSideLimitStatistics/list',
        method: 'post',
        data: data
    })
}

// 电网侧指标越限统计数据
export function gridSideLimitStatisticsData(data: any) {
    return request({
        url: '/cs-harmonic-boot/gridSideLimitStatistics/data',
        method: 'post',
        data: data
    })
}

// 敏感负荷用户监测点列表
export function governLineList(data: any) {
    return request({
        url: '/cs-device-boot/csline/getSensitiveUserLineList',
        method: 'post',
        data: data
    })
}

// 根据id集合获取敏感负荷用户列表
export function getListByIds(data: any) {
    return request({
        url: '/cs-harmonic-boot/pqSensitiveUser/getListByIds',
        method: 'post',
        data: data
    })
}

// 上传治理报告
export function uploadReport(data: any) {
    return request({
        url: '/cs-device-boot/csline/uploadReport',
        method: 'post',
        data: data
    })
}

// 获取治理报告链接
export function getReportUrl(data: any) {
    return request({
        url: '/cs-device-boot/csline/getReportUrl',
        method: 'post',
        params: data
    })
}

// 查询监测对象电网侧和负载侧监测点指标趋势对比数据
export function sensitiveUserTrendData(data: any) {
    return request({
        url: '/cs-device-boot/csGroup/sensitiveUserTrendData',
        method: 'post',
        data: data
    })
}

// 获取敏感负荷用户列表
export function getList(data: any) {
    return request({
        url: '/cs-harmonic-boot/pqSensitiveUser/getList',
        method: 'post',
        data: data
    })
}

// F47曲线
export function f47Curve(data: any) {
    return request({
        url: '/cs-harmonic-boot/csevent/f47Curve',
        method: 'post',
        data: data
    })
}

// 获取电压暂态表及密度坐标图
export function getEventCoords(data: any) {
    return request({
        url: '/cs-harmonic-boot/csevent/getEventCoords',
        method: 'post',
        data: data
    })
}

// 日历暂降事件详情
export function getEventDate(data: any) {
    return request({
        url: '/cs-harmonic-boot/csevent/getEventDate',
        method: 'post',
        data: data
    })
}

// 暂降类型分类统计Echart
export function netEventEcharts(data: any) {
    return request({
        url: '/cs-harmonic-boot/csevent/netEventEcharts',
        method: 'post',
        data: data
    })
}

// 暂降类型分类统计表格
export function netEventTable(data: any) {
    return request({
        url: '/cs-harmonic-boot/csevent/netEventTable',
        method: 'post',
        data: data
    })
}

// 分页查询暂降事件
export function pageEvent(data: any) {
    return request({
        url: '/cs-harmonic-boot/event/pageEvent',
        method: 'post',
        data: data
    })
}

// 暂态事件波形分析
export function analyseWave(data: any) {
    return request({
        url: '/cs-harmonic-boot/event/analyseWave',
        method: 'get',
        params: data
    })
}

// 暂态监测点下拉框接口
export function getSimpleLine() {
    return request({
        url: '/cs-device-boot/csline/getSimpleLine',
        method: 'get'
    })
}

export function getLineExport(data: any) {
    return request({
        url: '/cs-harmonic-boot/eventReport/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
        }
    })
}

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)
    }
}
