<template>
    <div>
        <div v-show="view">
            <TableHeader ref="tableHeaderRef" :showSearch="false" @selectChange="selectChange">
                <template v-slot:select>
                    <el-form-item label="多监测点">
                        <el-checkbox v-model="checked" @change="checkChange" />
                    </el-form-item>
                    <el-form-item label="统计时间">
                        <DatePicker ref="datePickerRef" :theCurrentTime="true"></DatePicker>
                    </el-form-item>
                    <el-form-item label="指标类型:">
                        <el-select
                            v-model="formData.condition"
                            multiple
                            collapse-tags
                            :multiple-limit="checked ? 1 : 3"
                            filterable
                            placeholder="请选择指标"
                            @change="conditionChange"
                        >
                            <el-option-group v-for="group in indexOptions" :key="group.label" :label="group.label">
                                <el-option
                                    v-for="item in group.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-option-group>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="数值类型:">
                        <el-select style="width: 100%" v-model="formData.valueType" placeholder="请选择类型" filterable>
                            <el-option
                                v-for="item in typeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="谐波次数:" v-show="showXieBoCiShu">
                        <el-select style="width: 100%" v-model="formData.harmonic" placeholder="请选择谐波" filterable>
                            <el-option
                                v-for="item in harmonicOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item v-show="showJianXieBoCiShu" label="间谐波次数:">
                        <el-select style="width: 100%" v-model="formData.inHarmonic" placeholder="请选择间谐波" filterable>
                            <el-option
                                v-for="item in inharmonicOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </template>
                <template v-slot:operation>
                    <el-button type="primary" @click="init" icon="el-icon-Search">查询</el-button>
                </template>
            </TableHeader>
            <!-- <el-form :inline="true">
           
        </el-form> -->
            <div
                id="canvas"
                class="mt10"
                :style="height1"
                style="overflow-y: auto; overflow-x: hidden"
                v-loading="loading"
            >
                <my-echart
                    :options="item.option"
                    v-for="item in list"
                    style="min-height: 190px"
                    :style="height"
                    @triggerPoint="triggerPoint"
                    @group="group"
                />
            </div>
        </div>
        <div :style="{ height: pageHeight.height }" style="padding: 10px; overflow: hidden" v-if="!view">
            <waveForm ref="waveFormRef" senior @backbxlb="backbxlb" />
        </div>
    </div>
</template>

<script setup lang="ts">
import { nextTick, onMounted, reactive, ref, onBeforeUnmount } from 'vue'
import DatePicker from '@/components/form/datePicker/index.vue'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { useMonitoringPoint } from '@/stores/monitoringPoint'
import { indexOptions, harmonicOptions, inharmonicOptions } from '@/utils/dictionary'
import { getHistoryResult } from '@/api/harmonic-boot/harmonic'
import { mainHeight } from '@/utils/layout'
import TableHeader from '@/components/table/header/index.vue'
import { queryEventDetailByEventId } from '@/api/event-boot/highAndLowPressure'
import TableStore from '@/utils/tableStore'
import waveForm from '@/components/echarts/waveForm.vue'
import html2canvas from 'html2canvas'
import { ElMessage } from 'element-plus'
import { yMethod, completeTimeSeries } from '@/utils/echartMethod'
import * as echarts from 'echarts' // 全引入

const datePickerRef = ref()
const monitoringPoint = useMonitoringPoint()
const checked = ref(monitoringPoint.state.showCheckBox)
const loading = ref(true)
const formData = reactive<{
    lineId: string[]
    searchBeginTime: string
    searchEndTime: string
    condition: string[]
    valueType: number
    harmonic: number
    inHarmonic: number
    ptType: number
}>({
    lineId: [],
    searchBeginTime: '',
    searchEndTime: '',
    condition: ['10', '11', '12'],
    valueType: 1,
    harmonic: 1,
    inHarmonic: 1,
    ptType: 0
})
const wp = ref({})
const pageHeight = mainHeight(20)
const view = ref(true)
const waveFormRef = ref()
const tableStore = new TableStore({
    url: '',
    method: 'POST',
    column: []
})
const tableData = ref([])
const tableHeaderRef = ref()
const options = ref({})
const traceability = ref<any>([])
const list = ref<any>([])
const showXieBoCiShu = ref(false)
const showJianXieBoCiShu = ref(false)
const typeOptions = ref([
    { label: '平均值', value: 1 },
    { label: '最小值', value: 2 },
    { label: '最大值', value: 3 },
    { label: 'cp95值', value: 4 }
])
onMounted(() => {
    datePickerRef.value.setTheDate(4)
    init()
})
const height: any = ref(mainHeight(200, 1))
const height1: any = ref(mainHeight(160))
const checkChange = () => {
    if (checked.value) {
        monitoringPoint.setShowCheckBox(true)
        if (formData.condition.length > 0) {
            formData.condition = formData.condition.slice(0, 1)
        }
    } else {
        monitoringPoint.setShowCheckBox(false)
        init()
    }
}
const group = (e: any) => {
    e.group = 'group'
}
const init = () => {
    if (formData.condition.length == 0) {
        return ElMessage.warning('请选择指标类型')
    }

    formData.lineId = checked.value ? monitoringPoint.state.lineIds : [monitoringPoint.state.lineId]
    formData.searchBeginTime = datePickerRef.value.timeValue[0]
    formData.searchEndTime = datePickerRef.value.timeValue[1]
    if (formData.lineId.length > 3) {
        return ElMessage.warning('最多只能选择3个监测点')
    }
    loading.value = true
    let directionValue = formData.condition.findIndex(item => {
        if (item === '39') {
            item = '50'
            return true
        } else {
            return false
        }
    })

    getHistoryResult(formData)
        .then(async (res: any) => {
            if (directionValue >= 0) {
                res.data[directionValue].targetName = '谐波电流方向'
                traceability.value = [(res.data as [])[directionValue]]
            }
            list.value = []
            await shujuchuli(res)
            await selectChange(tableHeaderRef.value.showSelect)
            loading.value = false
        })
        .catch(() => {
            loading.value = false
        })
}
const shujuchuli = (res: any) => {
    let shujuData = res.data
    tableData.value = res.data
    shujuData.forEach((item: any, i: number) => {
        //判断是否存在暂降点

        if (item.eventDetail !== null || item.eventDetail.length === 0) {
            //向项别中添加暂降点
            let chufa = []
            item.phaiscType.push('暂态触发点')
            //判断是否有限值(有上下限)
            if (item.topLimit !== 0 && item.lowerLimit !== 0) {
                item.phaiscType.push('上限')
                item.phaiscType.push('下限')
                if (item.minValue !== null && item.maxValue !== null) {
                    //最小值等于下限值

                    //图列为A,B,C,暂降,上限,下限
                    if (item.phaiscType.length == 6) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        let topLimit = []
                        let lowerLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                                //上下限值
                                topLimit.push([item.value[j][0], item.topLimit])
                                lowerLimit.push([item.value[j][0], item.lowerLimit])
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3), item.eventDetail[k].id, 99])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue,
                            topLimit: topLimit,
                            lowerLimit: lowerLimit,
                            chufa: chufa
                        }

                        list.value.push(shuju)
                    }
                    //图列为频率等,暂降,上限,下限
                    if (item.phaiscType.length == 4) {
                        let gvalue = []
                        let topLimit = []
                        let lowerLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                                //上下限值
                                topLimit.push([item.value[j][0], item.topLimit])
                                lowerLimit.push([item.value[j][0], item.lowerLimit])
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3), item.eventDetail[k].id, 99])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            gvalue: gvalue,
                            topLimit: topLimit,
                            lowerLimit: lowerLimit,
                            chufa: chufa
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
            //有上限值
            if (item.topLimit !== 0 && item.lowerLimit == 0) {
                item.phaiscType.push('上限')
                if (item.minValue !== null && item.maxValue !== null) {
                    //最小值等于下限值
                    //图列为A,B,C,暂降,上限
                    if (item.phaiscType.length == 5) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        let topLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                                //上限值
                                topLimit.push([item.value[j][0], item.topLimit])
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3), item.eventDetail[k].id, 99])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue,
                            topLimit: topLimit,
                            chufa: chufa
                        }
                        list.value.push(shuju)
                    }
                    //图列为频率等,暂降,上限
                    if (item.phaiscType.length == 3) {
                        let gvalue = []
                        let topLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                                //上限值
                                topLimit.push([item.value[j][0], item.topLimit])
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3)])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            gvalue: gvalue,
                            topLimit: topLimit,
                            chufa: chufa
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
            //有下限值
            if (item.topLimit == 0 && item.lowerLimit !== 0) {
                item.phaiscType.push('下限')
                if (item.minValue !== null && item.maxValue !== null) {
                    //最小值等于下限值
                    //图列为A,B,C,暂降,下限
                    if (item.phaiscType.length == 5) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        let lowerLimit = []
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                                //下限值
                                lowerLimit.push([item.value[j][0], item.lowerLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3)])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue,
                            lowerLimit: lowerLimit,
                            chufa: chufa
                        }
                        list.value.push(shuju)
                    }
                    //图列为频率等,暂降,下限
                    if (item.phaiscType.length == 3) {
                        let gvalue = []
                        let lowerLimit = []
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                                //下限值
                                lowerLimit.push([item.value[j][0], item.lowerLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3)])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            gvalue: gvalue,
                            lowerLimit: lowerLimit,
                            chufa: chufa
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
            //无限值
            if (item.topLimit == 0 && item.lowerLimit == 0) {
                if (item.minValue !== null && item.maxValue !== null) {
                    //最小值等于下限值
                    //图列为A,B,C,暂降
                    if (item.phaiscType.length == 4) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3), item.eventDetail[k].id, 99])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            minValue: item.minValue > 1 ? (item.minValue - 0.5).toFixed(3) : item.minValue.toFixed(3),
                            maxValue: item.maxValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue,
                            chufa: chufa
                        }
                        list.value.push(shuju)
                    }
                    //图列为频率等,暂降
                    if (item.phaiscType.length == 2) {
                        let gvalue = []
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        //不存在一个暂降点或1个 //处理暂降点
                        if (item.eventDetail.length > 0) {
                            for (let k = 0; k < item.eventDetail.length; k++) {
                                chufa.push([item.eventDetail[k].time, (0).toFixed(3), item.eventDetail[k].id, 99])
                            }
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            minValue: item.minValue.toFixed(3),
                            maxValue: item.maxValue.toFixed(3),
                            gvalue: gvalue,
                            chufa: chufa
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
        } else if (item.eventDetail == null) {
            //判断是否有限值(有上下限)
            if (item.topLimit !== 0 && item.lowerLimit !== 0) {
                item.phaiscType.push('上限')
                item.phaiscType.push('下限')
                if (item.minValue !== null && item.maxValue !== null) {
                    //最小值等于下限值
                    //图列为A,B,C,上限,下限
                    if (item.phaiscType.length == 5) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        let topLimit = []
                        let lowerLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                                //上下限值
                                topLimit.push([item.value[j][0], item.topLimit.toFixed(3)])
                                lowerLimit.push([item.value[j][0], item.lowerLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue,
                            topLimit: topLimit,
                            lowerLimit: lowerLimit
                        }
                        list.value.push(shuju)
                    }
                    //图列为频率等,上限,下限
                    if (item.phaiscType.length == 3) {
                        let gvalue = []
                        let topLimit = []
                        let lowerLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                                //上下限值
                                topLimit.push([item.value[j][0], item.topLimit.toFixed(3)])
                                lowerLimit.push([item.value[j][0], item.lowerLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            gvalue: gvalue,
                            topLimit: topLimit,
                            lowerLimit: lowerLimit,
                            chufa: []
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
            //有上限值
            if (item.topLimit !== 0 && item.lowerLimit == 0) {
                item.phaiscType.push('上限')
                if (item.minValue !== null) {
                    //最小值等于下限值
                    //图列为A,B,C,上限
                    if (item.phaiscType.length == 4) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        let topLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        // item.minValue=item.lowerLimit
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                                //上限值
                                topLimit.push([item.value[j][0], item.topLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            maxValue: item.maxValue.toFixed(3),
                            minValue: item.minValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue,
                            topLimit: topLimit
                        }
                        list.value.push(shuju)
                    }
                    //图列为频率等,上限
                    if (item.phaiscType.length == 2) {
                        let gvalue = []
                        let topLimit = []
                        item.maxValue = item.topLimit > item.maxValue ? item.topLimit : item.maxValue
                        // item.minValue=item.lowerLimit
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                                //上限值
                                topLimit.push([item.value[j][0], item.topLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            minValue: item.minValue.toFixed(3),
                            maxValue: item.maxValue.toFixed(3),
                            gvalue: gvalue,
                            topLimit: topLimit
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
            //有下限值
            if (item.topLimit == 0 && item.lowerLimit !== 0) {
                item.phaiscType.push('下限')
                if (item.minValue !== null) {
                    //最小值等于下限值
                    //图列为A,B,C,下限
                    if (item.phaiscType.length == 4) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        let lowerLimit = []
                        // item.maxValue=item.topLimit
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                                //下限值
                                lowerLimit.push([item.value[j][0], item.lowerLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            minValue: item.minValue.toFixed(3),
                            maxValue: item.maxValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue,
                            lowerLimit: lowerLimit
                        }
                        list.value.push(shuju)
                    }
                    //图列为频率等,下限
                    if (item.phaiscType.length == 2) {
                        let gvalue = []
                        let lowerLimit = []
                        // item.maxValue=item.topLimit
                        item.minValue = item.lowerLimit < item.minValue ? item.lowerLimit : item.minValue
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                                //下限值
                                lowerLimit.push([item.value[j][0], item.lowerLimit.toFixed(3)])
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            minValue: item.minValue.toFixed(3),
                            maxValue: item.maxValue.toFixed(3),
                            gvalue: gvalue,
                            lowerLimit: lowerLimit
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
            //无限值
            if (item.topLimit == 0 && item.lowerLimit == 0) {
                if (item.minValue !== null) {
                    //最小值等于下限值
                    //图列为A,B,C
                    if (item.phaiscType.length == 3) {
                        let avalue = []
                        let bvalue = []
                        let cvalue = []
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    avalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    avalue.push([])
                                }
                                //判断存在缺失值b
                                if (item.value[j][2] != undefined) {
                                    bvalue.push([item.value[j][0], item.value[j][2].toFixed(3)])
                                } else {
                                    bvalue.push([])
                                }
                                //判断存在缺失值c
                                if (item.value[j][3] != undefined) {
                                    cvalue.push([item.value[j][0], item.value[j][3].toFixed(3)])
                                } else {
                                    cvalue.push([])
                                }
                            }
                            //数据为空
                        } else {
                            avalue = []
                            bvalue = []
                            cvalue = []
                        }

                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            minValue: item.minValue.toFixed(3),
                            maxValue: item.maxValue.toFixed(3),
                            avalue: avalue,
                            bvalue: bvalue,
                            cvalue: cvalue
                        }
                        list.value.push(shuju)
                    }
                    //图列为频率等
                    if (item.phaiscType.length == 1) {
                        let gvalue = []
                        //判断数据是否存在
                        if (item.value !== null) {
                            for (let j = 0; j < item.value.length; j++) {
                                //判断存在缺失值a
                                if (item.value[j][1] != undefined) {
                                    gvalue.push([item.value[j][0], item.value[j][1].toFixed(3)])
                                } else {
                                    gvalue.push([])
                                }
                            }
                            //数据为空
                        } else {
                            gvalue = []
                        }
                        let shuju = {
                            id: 'qushifenx' + i,
                            title: item.lineName + '--' + item.targetName,
                            wiringMethod: item.wiringMethod || '0',
                            targetName: item.targetName,
                            value: item.value,
                            legend: item.phaiscType,
                            valueName: item.unit[0],
                            minValue: item.minValue.toFixed(3),
                            maxValue: item.maxValue.toFixed(3),
                            gvalue: gvalue
                        }
                        list.value.push(shuju)
                    }
                } else {
                    let shuju = {
                        id: 'qushifenx' + i,
                        title: item.lineName + '--' + item.targetName,
                        wiringMethod: item.wiringMethod || '0',
                        targetName: item.targetName,
                        value: item.value,
                        legend: item.phaiscType,
                        valueName: item.unit[0],
                        minValue: null,
                        avalue: [],
                        bvalue: [],
                        cvalue: [],
                        topLimit: [],
                        lowerLimit: [],
                        chufa: []
                    }
                    list.value.push(shuju)
                }
            }
        }
    })
    rendering()
}
const getSeriesDataValues = (data: any[]) => {
    if (!data?.length) return []
    return data
        .map(d => (Array.isArray(d) && d[1] != null && d[1] !== '' ? Number(d[1]) : null))
        .filter((v): v is number => v != null && !Number.isNaN(v))
}
const buildItemYAxis = (item: any) => {
    console.log('🚀 ~ buildItemYAxis ~ item:', item)
    if (item.useDualAxis && item.serise?.length) {
        const uniqueUnits = [...new Set(item.units.filter((u: string) => u != null && u !== ''))] as string[]
        const valuesByUnit: Record<string, number[]> = {}
        uniqueUnits.forEach(u => {
            valuesByUnit[u] = []
        })
        item.serise.forEach((s: any) => {
            const axisIdx = s.yAxisIndex ?? 0
            const unit = uniqueUnits[axisIdx]
            if (unit) valuesByUnit[unit].push(...getSeriesDataValues(s.data))
        })
        return uniqueUnits.map((unit, index) => {
            const [min, max] = yMethod(valuesByUnit[unit].length ? valuesByUnit[unit] : [0])

            return {
                type: 'value',
                min,
                max,
                name: unit,
                position: index === 0 ? 'left' : 'right',
                minInterval: index === 0 ? 1 : undefined,
                axisLine: {
                    show: true,
                    onZero: false
                },
                splitLine: index === 0 ? undefined : { show: false }
            }
        })
    }
    const values = item.value?.length ? item.value.map((k: any) => k[1]) : [0]

    const [min, max] = yMethod([
        item.maxValue,
        item.minValue,
        ...values,
        item.topLimit?.[0]?.[1] || null,
        item.lowerLimit?.[0]?.[1] || null
    ])
    return [
        {
            type: 'value',
            min,
            max,
            name: item.valueName,
            minInterval: 1,
            axisLine: {
                show: true,
                onZero: false
            }
        }
    ]
}
const rendering = () => {
    height.value = mainHeight(160, list.value.length > 3 ? 3 : list.value.length)

    list.value.forEach((item: any) => {
        const idx = parseInt(String(item.id).replace('qushifenx', ''))
        item.units = (tableData.value[idx] as any)?.unit || (item.valueName ? [item.valueName] : [''])
        const uniqueUnits = [...new Set(item.units.filter((u: string) => u != null && u !== ''))] as string[]
        item.useDualAxis = uniqueUnits.length > 1
        const getAxisIndex = (unitIndex: number) => {
            if (!item.useDualAxis) return 0
            const unit = item.units[unitIndex] ?? item.units[0]
            return uniqueUnits.indexOf(unit)
        }
        const withAxis = (data: any, unitIndex: number) => {
            if (item.useDualAxis) data.yAxisIndex = getAxisIndex(unitIndex)
            return data
        }
        const isShanBian = item.targetName.includes('闪变')
        const lineSeriesOpts = isShanBian ? { smooth: false, step: 'end' as const } : { smooth: true }

        if (item.targetName == '电压不平衡') {
            item.valueName = ''
            const phaseNames = ['零序电压', '正序电压', '负序电压']
            item.legend = item.legend.map((item2: any) => {
                const phaseIdx = phaseNames.indexOf(item2)
                if (phaseIdx >= 0 && item.units[phaseIdx]) {
                    return item2 + '(' + item.units[phaseIdx] + ')'
                }
                return item2
            })
        }

        let opitonserise: any[] = []
        item.legend.forEach((item2: any) => {
            if (item.avalue !== undefined && (item2 == 'A相' || item2 == 'AB相' || item2.startsWith('零序电压'))) {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    ...lineSeriesOpts,
                    itemStyle: {
                        normal: {
                            color: '#DAA520'
                        }
                    },

                    data: item.avalue
                }
                opitonserise.push(withAxis(data, 0))
            } else if (
                item.bvalue !== undefined &&
                (item2 == 'B相' || item2 == 'BC相' || item2.startsWith('正序电压'))
            ) {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    ...lineSeriesOpts,
                    itemStyle: {
                        normal: {
                            color: '#2E8B57'
                        }
                    },

                    data: item.bvalue
                }
                opitonserise.push(withAxis(data, 1))
            } else if (
                item.cvalue !== undefined &&
                (item2 == 'C相' || item2 == 'CA相' || item2.startsWith('负序电压'))
            ) {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    ...lineSeriesOpts,
                    barWidth: 22,
                    itemStyle: {
                        normal: {
                            color: '#A52a2a'
                        }
                    },
                    data: item.cvalue
                }
                opitonserise.push(withAxis(data, 2))
            } else if (item.topLimit !== undefined && item2 == '上限') {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    ...lineSeriesOpts,
                    itemStyle: {
                        normal: {
                            color: '#FF33FF',
                            lineStyle: {
                                width: 2,
                                color: '#FF33FF',
                                type: 'dotted' //'dotted'虚线 'solid'实线
                            }
                        }
                    },
                    data: item.topLimit
                }
                opitonserise.push(withAxis(data, 0))
            } else if (item.lowerLimit !== undefined && item2 == '下限') {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    ...lineSeriesOpts,
                    barWidth: 22,
                    itemStyle: {
                        normal: {
                            color: '#3399FF',
                            lineStyle: {
                                width: 2,
                                color: '#3399FF',
                                type: 'dotted' //'dotted'虚线 'solid'实线
                            }
                        }
                    },
                    data: item.lowerLimit
                }
                opitonserise.push(withAxis(data, 0))
            } else if (item.chufa !== undefined && item2 == '暂态触发点') {
                let data = {
                    name: item2,
                    type: 'scatter',
                    symbol: `image://` + new URL('@/assets/point.png', import.meta.url).href,
                    symbolSize: 16,
                    itemStyle: {
                        width: '22px',
                        height: '22px'
                    },
                    data: item.chufa
                }
                opitonserise.push(withAxis(data, 0))
            } else if (item.gvalue !== undefined && item2 == '频率') {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    ...lineSeriesOpts,
                    itemStyle: {
                        normal: {
                            color: '#990099',
                            lineStyle: {
                                width: 2,
                                color: '#990099',
                                type: 'solid' //'dotted'虚线 'solid'实线
                            }
                        }
                    },
                    data: item.gvalue
                }
                opitonserise.push(withAxis(data, 0))
            } else if (item.gvalue !== undefined && item2 == '负序电流') {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    barWidth: 22,
                    itemStyle: {
                        normal: {
                            color: '#999933'
                        }
                    },
                    data: item.gvalue
                }
                opitonserise.push(withAxis(data, 0))
            } else if (item.gvalue !== undefined && item2 == '三相电压不平衡度') {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    barWidth: 22,
                    itemStyle: {
                        normal: {
                            color: '#FF6600'
                        }
                    },
                    data: item.gvalue
                }
                opitonserise.push(withAxis(data, 0))
            } else if (
                item.gvalue !== undefined &&
                (item2 == '三相总有功功率' || item2 == '三相总无功功率' || item2 == '三相总视在功率')
            ) {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    barWidth: 22,
                    itemStyle: {
                        normal: {
                            color: '#526ADE'
                        }
                    },
                    data: item.gvalue
                }
                opitonserise.push(withAxis(data, 0))
            } else if (item.gvalue !== undefined && (item2 == '总视在功率因数' || item2 == '总位移功率因数')) {
                let data = {
                    name: item2,
                    symbol: 'none',
                    symbolSize: 5,
                    type: 'line',
                    barWidth: 22,
                    itemStyle: {
                        normal: {
                            color: '#526ADE'
                        }
                    },

                    data: item.gvalue
                }
                opitonserise.push(withAxis(data, 0))
            }
            // else {
            //     opitonserise.push([])
            // }
        })
        if (item.valueName == undefined) {
            item.valueName = ''
        }
        //console.log('渲染数据体',opitonserise)
        item.serise = opitonserise
    })
    getEcharts()
}
const measureTextWidth = (text: string, fontSize = 14) => {
    const canvas = document.createElement('canvas')
    const ctx = canvas.getContext('2d')
    if (!ctx) return text.length * fontSize * 0.6
    ctx.font = `${fontSize}px dinproRegular, sans-serif`
    return ctx.measureText(text).width
}
const getYAxisLeftPadding = (yAxis: any) => {
    const splitNumber = yAxis.splitNumber ?? 5
    const min = yAxis.min ?? 0
    const max = yAxis.max ?? 0
    let maxLabelWidth = 0
    const formatter = yAxis.axisLabel?.formatter
    const formatLabel = (value: number) => {
        if (formatter) {
            const result = formatter(value)
            return result != null && result !== '' ? String(result) : ''
        }
        return String(parseFloat(value.toFixed(1)))
    }
    if (yAxis.interval) {
        for (let v = min; v <= max + yAxis.interval * 0.01; v += yAxis.interval) {
            const label = formatLabel(v)
            if (label) maxLabelWidth = Math.max(maxLabelWidth, measureTextWidth(label))
        }
    } else {
        const step = splitNumber > 0 ? (max - min) / splitNumber : 0
        for (let i = 0; i <= splitNumber; i++) {
            const label = formatLabel(min + step * i)
            if (label) maxLabelWidth = Math.max(maxLabelWidth, measureTextWidth(label))
        }
    }
    const nameWidth = yAxis.name ? measureTextWidth(yAxis.name) : 0
    return Math.ceil(Math.max(maxLabelWidth, nameWidth)) + 15
}
const alignChartsYAxis = () => {
    let maxLeft = 50
    let maxRight = 70
    let hasDualAxis = false
    list.value.forEach((item: any) => {
        const yAxes = item.option?.yAxis || []
        if (yAxes.length > 1) hasDualAxis = true
        yAxes.forEach((yAxis: any, index: number) => {
            const padding = getYAxisLeftPadding(yAxis)
            if (yAxis.position === 'right' || (yAxes.length > 1 && index > 0)) {
                maxRight = Math.max(maxRight, padding)
            } else {
                maxLeft = Math.max(maxLeft, padding)
            }
        })
    })
    if (list.value.length <= 1 && !hasDualAxis) return
    list.value.forEach((item: any) => {
        const gridUpdate: any = { containLabel: false }
        if (list.value.length > 1 || hasDualAxis) gridUpdate.left = maxLeft
        if (hasDualAxis) gridUpdate.right = maxRight
        item.option.grid = {
            ...item.option.grid,
            ...gridUpdate
        }
    })
}
const getEcharts = () => {
    list.value.forEach((item: any, i: number) => {
        const yAxisConfig = buildItemYAxis(item)
        item.option = {
            backgroundColor: '#fff',
            title: {
                left: 'center',
                text: item.title,
                subtext:
                    item.wiringMethod == '1'
                        ? item.targetName == '相电压有效值'
                            ? '该监测点为三角型接线，相电压有效值没有数据'
                            : ''
                        : '', // 副标题
                subtextStyle: {
                    fontSize: 14,
                    color: '#ff0000'
                }
            },
            tooltip: {
                trigger: 'axis',
                // axisPointer: {
                //     //type: "shadow",
                //     type: 'cross', // 默认为直线，可选为：'line' | 'shadow'
                //     label: {
                //         color: '#fff',
                //         fontSize: 16
                //     }
                // },
                textStyle: {
                    color: '#fff',
                    fontStyle: 'normal',
                    opacity: 0.35,
                    fontSize: 14
                },
                backgroundColor: 'rgba(0,0,0,0.55)',

                formatter: function (params: any) {
                    let tips = ''
                    tips += '时刻:' + params[0].data[0] + '</br/>'
                    for (let i = 0; i < params.length; i++) {
                        if (params[i].seriesName == '暂态触发点') {
                            tips += ''

                            return tips
                        }
                        if (params[i].seriesName == 'A相谐波电流方向') {
                            tips +=
                                params[i].marker + params[i].value[1] > 0
                                    ? 'A相谐波电流方向:流入<br/>'
                                    : params[i].value[1] == 0
                                    ? 'A相谐波电流方向:无<br/>'
                                    : 'A相谐波电流方向:流出<br/>'
                        } else if (params[i].seriesName == 'B相谐波电流方向') {
                            tips +=
                                params[i].marker + params[i].value[1] > 0
                                    ? 'B相谐波电流方向:流入<br/>'
                                    : params[i].value[1] == 0
                                    ? 'B相谐波电流方向:无<br/>'
                                    : 'B相谐波电流方向:流出<br/>'
                        } else if (params[i].seriesName == 'C相谐波电流方向') {
                            tips +=
                                params[i].marker + params[i].value[1] > 0
                                    ? 'C相谐波电流方向:流入<br/>'
                                    : params[i].value[1] == 0
                                    ? 'C相谐波电流方向:无<br/>'
                                    : 'C相谐波电流方向:流出<br/>'
                        } else if (params[i].seriesName == '总谐波电流方向') {
                            tips +=
                                params[i].marker + params[i].value[1] > 0
                                    ? '总谐波电流方向:流入<br/>'
                                    : params[i].value[1] == 0
                                    ? '总谐波电流方向:无<br/>'
                                    : '总谐波电流方向:流出<br/>'
                        } else if (params[i].seriesName.includes('正序电压')) {
                            let str = (params[i].value[1] * 1).toString()
                            let reg = str.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
                            let str1 = str.replace(reg, '$1,')
                            let cleanStr = str1.replace(/,/g, '')
                            let num = Number(cleanStr)
                            const unitMatch = params[i].seriesName.match(/\(([^)]+)\)$/)
                            const unit = unitMatch ? unitMatch[1] : 'kV'
                            tips +=
                                params[i].marker +
                                params[i].seriesName.replace(/\([^)]+\)$/, '') +
                                ':' +
                                (Number.isNaN(Number(num)) ? '/' : num) +
                                unit +
                                '<br/>'
                        } else if (
                            params[i].seriesName.includes('零序电压') ||
                            params[i].seriesName.includes('负序电压')
                        ) {
                            let str = (params[i].value[1] * 1).toString()
                            let reg = str.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
                            let str1 = str.replace(reg, '$1,')
                            let cleanStr = str1.replace(/,/g, '')
                            let num = Number(cleanStr)
                            const unitMatch = params[i].seriesName.match(/\(([^)]+)\)$/)
                            const unit = unitMatch ? unitMatch[1] : 'V'
                            tips +=
                                params[i].marker +
                                params[i].seriesName.replace(/\([^)]+\)$/, '') +
                                ':' +
                                (Number.isNaN(Number(num)) ? '/' : num) +
                                unit +
                                '<br/>'
                        } else if (!params[i].seriesName.includes('正序电压')) {
                            let str = (params[i].value[1] * 1).toString()
                            let reg = str.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
                            let str1 = str.replace(reg, '$1,')
                            let cleanStr = str1.replace(/,/g, '')
                            let num = Number(cleanStr)
                            tips +=
                                params[i].marker +
                                params[i].seriesName +
                                ':' +
                                (Number.isNaN(Number(num)) ? '/' : num) +
                                '<br/>'
                        } else {
                            let str = (params[i].value[2] * 1).toString()

                            let reg = str.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
                            let str1 = str.replace(reg, '$1,')
                            let cleanStr = str1.replace(/,/g, '')
                            let num = Number(cleanStr)
                            tips +=
                                params[i].marker +
                                params[i].seriesName +
                                ':' +
                                (Number.isNaN(Number(num)) ? '/' : num) +
                                '<br/>'

                            let str2 = (params[i].value[3] * 1).toString()
                            let reg2 = str2.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
                            let str12 = str.replace(reg2, '$1,')
                            // if(params[i].seriesName){

                            // }
                            tips +=
                                params[i].marker +
                                params[i].seriesName +
                                ':' +
                                (Number.isNaN(Number(str2)) ? '/' : str2) +
                                '<br/>'
                        }
                    }

                    return tips
                }
            },
            toolbox: {
                right: 20,
                top: 15,
                show: i == 0 ? true : false,
                feature: {
                    // 移除默认的 saveAsImage
                    // saveAsImage: { title: '保存图片' }
                    myCustomDownload: {
                        title: '', // 按钮标题
                        icon: 'path://M892.342857 463.238095l-73.142857-68.266666-258.438095 258.438095V29.257143h-97.52381v624.152381L204.8 394.971429 131.657143 463.238095l380.342857 380.342857zM107.27619 897.219048h804.571429v97.523809H107.27619z', // 自定义图标路径

                        onclick: function () {
                            html2canvas(document.getElementById('canvas'), {
                                scale: 1
                            }).then(function (canvas) {
                                // 创建a标签，实现下载
                                let creatIMg = document.createElement('a')
                                creatIMg.download = '稳态数据分析.png' // 设置下载的文件名，
                                creatIMg.href = canvas.toDataURL() // 下载url
                                creatIMg.click()
                                creatIMg.remove() // 下载之后把创建的元素删除
                            })
                        }
                    }
                }
            },
            legend: {
                right: 50,
                top: 25,
                verticalAlign: 'top',
                enabled: true,
                itemDistance: 5,
                textStyle: {
                    rich: {
                        a: {
                            verticalAlign: 'middle'
                        }
                    },

                    padding: [0, 0, 0, 0] //[上、右、下、左]
                }
            },
            grid: {
                top: '50px',
                bottom: '45px'
            },
            dataZoom: [
                {
                    type: 'inside',
                    height: 13,
                    start: 0,
                    bottom: '10px',
                    end: 100
                },
                {
                    start: 0,
                    height: 13,
                    bottom: '10px',
                    end: 100
                }
            ],
            xAxis: [
                {
                    name: '时间',
                    type: 'time',
                    axisLine: {
                        show: true,
                        onZero: false
                    },
                    axisLabel: {
                        textStyle: {
                            fontFamily: 'dinproRegular'
                        }
                    }
                }
            ],
            // toolbox: {
            //     show: true,
            //     feature: {
            //         dataZoom: {
            //             // bottom: '10px',
            //             yAxisIndex: 'none'
            //         }
            //     }
            // },
            yAxis: yAxisConfig,
            series: item.serise
        }
        let aValues = []
        let bValues = []
        let CValues = []
        let ZValues = []

        if (
            traceability.value.length > 0 &&
            traceability.value[0].value != null &&
            traceability.value[0].value.length > 0
        ) {
            for (let i = 0; i < traceability.value[0].value.length; i++) {
                let T = traceability.value[0].value[i][0]
                let A = traceability.value[0].value[i][1]
                let B = traceability.value[0].value[i][2]
                let C = traceability.value[0].value[i][3]
                let Z = A + B + C
                aValues.push([T, A > 0 ? 1 : A == 0 ? 0 : -1])
                bValues.push([T, B > 0 ? 1 : B == 0 ? 0 : -1])
                CValues.push([T, C > 0 ? 1 : C == 0 ? 0 : -1])
                ZValues.push([T, Z > 0 ? 1 : Z == 0 ? 0 : -1])
            }
        }

        if (item.targetName == '谐波电流方向') {
            item.option.tooltip.axisPointer = null
            item.option.yAxis[0] = {
                type: 'value',
                interval: 1,
                min: -2, //最小值
                max: 2,
                axisLine: {
                    show: true,
                    onZero: false //-----------重点
                },
                splitLine: {
                    lineStyle: {
                        // 使用深浅的间隔色
                        type: 'dashed',
                        opacity: 0.5
                    }
                },
                axisLabel: {
                    formatter: function (value: any) {
                        if (value == 0) {
                            return '无'
                        } else if (value == -1) {
                            return '流出'
                        } else if (value == 1) {
                            return '流入'
                        }
                    }
                }
            }
            item.option.series = [
                {
                    name: 'A相谐波电流方向',
                    type: 'line',

                    data: aValues,
                    symbol: 'none',
                    symbolSize: 0,
                    itemStyle: {
                        normal: {
                            color: '#DAA520'
                        }
                    }
                },
                {
                    name: 'B相谐波电流方向',
                    type: 'line',

                    data: bValues,
                    symbol: 'none',
                    symbolSize: 0,
                    itemStyle: {
                        normal: {
                            color: '#2E8B57'
                        }
                    }
                },
                {
                    name: 'C相谐波电流方向',
                    type: 'line',

                    data: CValues,
                    symbol: 'none',
                    symbolSize: 0,
                    itemStyle: {
                        normal: {
                            color: '#A52a2a'
                        }
                    }
                },
                {
                    name: '总谐波电流方向',
                    type: 'line',

                    data: ZValues,
                    symbol: 'none',
                    symbolSize: 0,
                    itemStyle: {
                        normal: {
                            color: '#526ADE'
                        }
                    }
                }
            ]
        }
    })
    alignChartsYAxis()
    echarts.connect('group')
}
const conditionChange = () => {
    //判断一个指标时
    if (formData.condition.length == 1) {
        if (
            formData.condition.includes('40') ||
            formData.condition.includes('43') ||
            formData.condition.includes('44') ||
            formData.condition.includes('50') ||
            formData.condition.includes('39') ||
            formData.condition.includes('51') ||
            formData.condition.includes('52') ||
            formData.condition.includes('45')
        ) {
            showXieBoCiShu.value = true
        } else {
            showXieBoCiShu.value = false
        }
        if (
            formData.condition.includes('46') ||
            //formData.condition.includes('47') ||
            // formData.condition.includes('48') ||
            formData.condition.includes('49')
        ) {
            showJianXieBoCiShu.value = true
        } else {
            showJianXieBoCiShu.value = false
        }
    }
    //判断2个指标时
    if (formData.condition.length == 2) {
        if (
            formData.condition.includes('40') ||
            formData.condition.includes('43') ||
            formData.condition.includes('44') ||
            (formData.condition.includes('45') && formData.condition.includes('46')) ||
            //formData.condition.includes('47') ||
            // formData.condition.includes('48') ||
            formData.condition.includes('49') ||
            formData.condition.includes('50') ||
            formData.condition.includes('39') ||
            formData.condition.includes('51') ||
            formData.condition.includes('52')
        ) {
            showXieBoCiShu.value = true
            showJianXieBoCiShu.value = true
        }
        if (
            formData.condition.includes('40') ||
            formData.condition.includes('43') ||
            formData.condition.includes('44') ||
            formData.condition.includes('45') ||
            formData.condition.includes('50') ||
            formData.condition.includes('39') ||
            formData.condition.includes('51') ||
            formData.condition.includes('52')
        ) {
            showXieBoCiShu.value = true
        } else {
            showXieBoCiShu.value = false
        }
        if (
            formData.condition.includes('46') ||
            //formData.condition.includes('47') ||
            // formData.condition.includes('48') ||
            formData.condition.includes('49')
        ) {
            showJianXieBoCiShu.value = true
        } else {
            showJianXieBoCiShu.value = false
        }
    }
    //判断3个指标时
    if (formData.condition.length == 3) {
        if (
            (formData.condition.includes('40') ||
                formData.condition.includes('43') ||
                formData.condition.includes('44') ||
                formData.condition.includes('50') ||
                formData.condition.includes('39') ||
                formData.condition.includes('51') ||
                formData.condition.includes('52') ||
                formData.condition.includes('45')) &&
            (formData.condition.includes('46') ||
                //formData.condition.includes('47') ||
                // formData.condition.includes('48') ||
                formData.condition.includes('49'))
        ) {
            showXieBoCiShu.value = true
            showJianXieBoCiShu.value = true
        } else {
            showXieBoCiShu.value = false
            showJianXieBoCiShu.value = false
        }
        if (
            formData.condition.includes('46') ||
            //formData.condition.includes('47') ||
            // formData.condition.includes('48') ||
            formData.condition.includes('49')
        ) {
            showJianXieBoCiShu.value = true
        } else {
            showJianXieBoCiShu.value = false
        }
        if (
            formData.condition.includes('40') ||
            formData.condition.includes('43') ||
            formData.condition.includes('44') ||
            formData.condition.includes('45') ||
            formData.condition.includes('50') ||
            formData.condition.includes('39') ||
            formData.condition.includes('51') ||
            formData.condition.includes('52')
        ) {
            showXieBoCiShu.value = true
        } else {
            showXieBoCiShu.value = false
        }
    }
    //判断无指标时
    if (formData.condition.length == 0) {
        showJianXieBoCiShu.value = false
        showXieBoCiShu.value = false
    }
    //根据指标判断值类型
    if (formData.condition.includes('60') || formData.condition.includes('61') || formData.condition.includes('62')) {
        typeOptions.value = [{ label: '平均值', value: 1 }]
        formData.valueType = 1
    } else {
        typeOptions.value = [
            { label: '平均值', value: 1 },
            { label: '最小值', value: 2 },
            { label: '最大值', value: 3 },
            { label: 'cp95值', value: 4 }
        ]
    }
    setTimeout(() => {
        //console.log('🚀 ~ setTimeout ~  tableHeaderRef.value:', tableHeaderRef.value)
        //tableHeaderRef.value && tableHeaderRef.value?.computedSearchRow()
    }, 100)
}

// 点击暂降触发点
const triggerPoint = (data: any) => {
    let nameList = monitoringPoint.state.lineName.split('>')

    view.value = false
    setTimeout(() => {
        queryEventDetailByEventId({ eventId: data[2] }).then(res => {
            waveFormRef.value.open({
                ...res.data,
                subName: nameList[nameList.length - 2].split('（')[0] || nameList[nameList.length - 2],
                lineName: nameList[nameList.length - 1].split('_')[1] || nameList[nameList.length - 1]
            })
            waveFormRef.value.setHeight(220, 180)
        })
    }, 100)
}
// 计算高度
const selectChange = (flag: boolean) => {
    // const height1: any = ref(mainHeight(160))
    height1.value = mainHeight(flag ? 220 : 160)
    height.value = mainHeight(flag ? 220 : 160, list.value.length > 3 ? 3 : list.value.length)
}
const backbxlb = () => {
    view.value = true
}
provide('tableStore', tableStore)
onBeforeUnmount(() => {
    echarts.disconnect('group')
})
</script>
<style></style>
