<template>
    <div class="view" v-loading="loading">
        <el-collapse v-model.trim="activeNames">
            <el-collapse-item title="基础数据" name="1">
                <div class="view_top">
                    <!-- 左侧仪表盘 -->
                    <div class="view_top_left">
                        <div class="left_charts_title">电压有效值({{ voltageUnit }})</div>
                        <div class="left_charts">
                            <MyEchart :pieInterVal="true" ref="pieChart1" :options="echartsDataV1"></MyEchart>
                        </div>
                        <div class="left_charts">
                            <MyEchart :pieInterVal="true" ref="pieChart2" :options="echartsDataV2"></MyEchart>
                        </div>
                        <div class="left_charts">
                            <MyEchart :pieInterVal="true" ref="pieChart3" :options="echartsDataV3"></MyEchart>
                        </div>
                    </div>
                    <div class="view_top_mid">
                        <div class="mid_charts_title">基波电压/电流幅值(相位)</div>
                        <div class="mid_charts">
                            <MyEchart :pieInterVal="true" :options="echartsData1"></MyEchart>
                        </div>
                    </div>
                    <!-- 右侧仪表盘 -->
                    <div class="view_top_right">
                        <div class="right_charts_title">电流有效值(A)</div>
                        <div class="right_charts">
                            <MyEchart :pieInterVal="true" ref="pieChart4" :options="echartsDataA1"></MyEchart>
                        </div>
                        <div class="right_charts">
                            <MyEchart :pieInterVal="true" ref="pieChart5" :options="echartsDataA2"></MyEchart>
                        </div>
                        <div class="right_charts">
                            <MyEchart :pieInterVal="true" ref="pieChart6" :options="echartsDataA3"></MyEchart>
                        </div>
                    </div>
                </div>
            </el-collapse-item>



        </el-collapse>

        <div class="view_bot">
            <vxe-table border height="" :data="realList" :column-config="{ resizable: true, tooltip: true }"
                :tooltip-config="{ enterable: true }">
                <vxe-colgroup align="center" :title="`电压有效值(${voltageUnit})`">
                    <vxe-column align="center" field="vRmsA" title="A相"></vxe-column>
                    <vxe-column align="center" field="vRmsB" title="B相"></vxe-column>
                    <vxe-column align="center" field="vRmsC" title="C相"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="电流有效值(A)">
                    <vxe-column align="center" field="iRmsA" title="A相"></vxe-column>
                    <vxe-column align="center" field="iRmsB" title="B相"></vxe-column>
                    <vxe-column align="center" field="iRmsC" title="C相"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="基波电压相位(°)">
                    <vxe-column align="center" field="v1AngA" title="A相"></vxe-column>
                    <vxe-column align="center" field="v1AngB" title="B相"></vxe-column>
                    <vxe-column align="center" field="v1AngC" title="C相"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="基波电流相位(°)">
                    <vxe-column align="center" field="i1AngA" title="A相"></vxe-column>
                    <vxe-column align="center" field="i1AngB" title="B相"></vxe-column>
                    <vxe-column align="center" field="i1AngC" title="C相"></vxe-column>
                </vxe-colgroup>
            </vxe-table>
            <br />
            <vxe-table border height="" :data="realList" :column-config="{ resizable: true, tooltip: true }"
                :tooltip-config="{ enterable: true }">
                <vxe-column align="center" field="freq" width="140" title="频率(Hz)"></vxe-column>
                <vxe-column align="center" field="freqDev" width="120" title="频率偏差(Hz)"></vxe-column>
                <vxe-column align="center" width="180" field="vUnbalance" title="电压不平衡度(%)"></vxe-column>
                <vxe-column align="center" width="180" field="iUnbalance" title="电流不平衡度(%)"></vxe-column>
                <vxe-colgroup align="center" :title="`基波电压幅值(${voltageUnit})`">
                    <vxe-column align="center" field="v1A" title="A相"></vxe-column>
                    <vxe-column align="center" field="v1B" title="B相"></vxe-column>
                    <vxe-column align="center" field="v1C" title="C相"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="基波电流幅值(A)">
                    <vxe-column align="center" field="i1A" title="A相"></vxe-column>
                    <vxe-column align="center" field="i1B" title="B相"></vxe-column>
                    <vxe-column align="center" field="i1C" title="C相"></vxe-column>
                </vxe-colgroup>
            </vxe-table>
            <br />
            <vxe-table border height="" :data="realList" :column-config="{ resizable: true, tooltip: true }"
                :tooltip-config="{ enterable: true }">
                <vxe-colgroup align="center" title="电压偏差(%)">
                    <vxe-column align="center" field="vDevA" title="A相"></vxe-column>
                    <vxe-column align="center" field="vDevB" title="B相"></vxe-column>
                    <vxe-column align="center" field="vDevC" title="C相"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="电压总谐波畸变率(%)">
                    <vxe-column align="center" field="vThdA" title="A相"></vxe-column>
                    <vxe-column align="center" field="vThdB" title="B相"></vxe-column>
                    <vxe-column align="center" field="vThdC" title="C相"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="电流总谐波畸变率(%)">
                    <vxe-column align="center" field="iThdA" title="A相"></vxe-column>
                    <vxe-column align="center" field="iThdB" title="B相"></vxe-column>
                    <vxe-column align="center" field="iThdC" title="C相"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" :title="`有功功率(${powerUnit})`">
                    <vxe-column align="center" field="pA" title="A相"></vxe-column>
                    <vxe-column align="center" field="pB" title="B相"></vxe-column>
                    <vxe-column align="center" field="pC" title="C相"></vxe-column>
                    <vxe-column align="center" field="pTot" title="总"></vxe-column>
                </vxe-colgroup>
            </vxe-table>
            <br />
            <vxe-table border height="" :data="realList" :column-config="{ resizable: true, tooltip: true }"
                :tooltip-config="{ enterable: true }">
                <vxe-colgroup align="center" :title="`无功功率(${reactivePowerUnit})`">
                    <vxe-column align="center" field="qA" title="A相"></vxe-column>
                    <vxe-column align="center" field="qB" title="B相"></vxe-column>
                    <vxe-column align="center" field="qC" title="C相">
                        <template #default="{ row }">
                            {{ row.qC }}
                        </template>
                    </vxe-column>
                    <vxe-column align="center" field="qTot" title="总"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" :title="`视在功率(${apparentPowerUnit})`">
                    <vxe-column align="center" field="sA" title="A相"></vxe-column>
                    <vxe-column align="center" field="sB" title="B相"></vxe-column>
                    <vxe-column align="center" field="sC" title="C相"></vxe-column>
                    <vxe-column align="center" field="sTot" title="总"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="视在功率因数">
                    <vxe-column align="center" field="pfA" title="A相"></vxe-column>
                    <vxe-column align="center" field="pfB" title="B相"></vxe-column>
                    <vxe-column align="center" field="pfC" title="C相"></vxe-column>
                    <vxe-column align="center" field="pfTot" title="总"></vxe-column>
                </vxe-colgroup>
                <vxe-colgroup align="center" title="位移功率因数">
                    <vxe-column align="center" field="dpfA" title="A相"></vxe-column>
                    <vxe-column align="center" field="dpfB" title="B相"></vxe-column>
                    <vxe-column align="center" field="dpfC" title="C相"></vxe-column>
                    <vxe-column align="center" field="dpfTot" title="总"></vxe-column>
                </vxe-colgroup>
            </vxe-table>
        </div>




    </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { getRealTimeTableList } from '@/api/cs-device-boot/EquipmentDelivery'
const pieChartRef: any = ref()
const activeNames = ref(['1'])
const pieChart1: any = ref()
const pieChart2: any = ref()
const pieChart3: any = ref()
const pieChart4: any = ref()
const pieChart5: any = ref()
const pieChart6: any = ref()
const echartsData: any = ref({})
const echartsData1: any = ref({})
//电压有效值
const echartsDataV1: any = ref({})
const echartsDataV2: any = ref({})
const echartsDataV3: any = ref({})
//电流有效值
const echartsDataA1: any = ref({})
const echartsDataA2: any = ref({})
const echartsDataA3: any = ref({})

const currentDataLevel = ref('Primary') 
const previousDataLevel = ref('')

//渲染中间相角图
const initRadioCharts = () => {
    echartsData1.value = {
        options: {
            grid: {
                top: 0
            },
            title: {
                text: '',
                x: 'center'
            },
            tooltip: {
                formatter: '{a} <br/>{b} {c}°'
            },
            toolbox: {
                show: false
            },
            legend: {
                show: false
            },
            series: [
                // 外圈电压 内圈电流
                {
                    name: '基波电流相位',
                    type: 'gauge',
                    // 表盘最小值
                    min: 180,
                    // 表盘最大值
                    max: -180,
                    // 表盘分割数
                    splitNumber: 6,
                    // 圆心位置
                    center: ['50%', '50%'],
                    // 半径
                    radius: '50%',
                    // startAngle: 180,
                    // endAngle: -179.99,
                    startAngle: 90,
                    endAngle: -179.99,
                    // 指针方向
                    clockWise: true,
                    title: false,
                    // 表盘外框
                    axisLine: {
                        show: true,
                        lineStyle: {
                            color: [
                                [0.25, '#9D322D'],
                                [0.5, '#9D322D'],
                                [0.75, '#9D322D'],
                                [1, '#9D322D']
                            ],
                            width: 2
                        }
                    },
                    // 表盘细分数
                    axisTick: {
                        show: true,
                        splitNumber: 5,
                        length: 4,
                        lineStyle: {
                            color: '#9D322D',
                            width: 1,
                            type: 'solid'
                        }
                    },
                    // 分割线
                    splitLine: {
                        show: true,
                        length: 10,
                        lineStyle: {
                            color: '#9D322D',
                            width: 2,
                            type: 'solid'
                        }
                    },
                    // 分割线标识
                    axisLabel: {
                        show: true,
                        distance: 5,
                        formatter: function (v) {
                            switch (v + '') {
                                case '-180':
                                    return ''
                                default:
                                    return v
                            }
                        }
                    },

                    // 指针设置
                    pointer: {
                        length: '80%',
                        width: 3
                    },
                    detail: {
                        show: false
                    },
                    data: [
                        {
                            value: 0,
                            name: 'A相',
                            itemStyle: {
                                color: '#DAA520'
                            }
                        },
                        {
                            value: 0,
                            name: 'B相',
                            itemStyle: {
                                color: '#2E8B57'
                            }
                        },
                        {
                            value: 0,
                            name: 'C相',
                            itemStyle: {
                                color: '#A52a2a'
                            }
                        }
                    ]
                },
                {
                    name: '基波电压相位',
                    type: 'gauge',
                    // 表盘最小值
                    min: 180,
                    // 表盘最大值
                    max: -180,
                    // 表盘分割数
                    splitNumber: 6,
                    // 圆心位置
                    center: ['50%', '50%'],
                    // 半径
                    radius: '85%',
                    // startAngle: 180,
                    // endAngle: -179.99,
                    startAngle: 90,
                    endAngle: -179.99,
                    // 指针方向
                    clockWise: true,
                    title: false,
                    // 表盘外框
                    axisLine: {
                        show: true,
                        lineStyle: {
                            color: [
                                [0.25, '#9D322D'],
                                [0.5, '#9D322D'],
                                [0.75, '#9D322D'],
                                [1, '#9D322D']
                            ],
                            width: 1.5
                        }
                    },
                    // 表盘细分数
                    axisTick: {
                        show: true,
                        splitNumber: 5,
                        length: 6,
                        lineStyle: {
                            color: '#9D322D',
                            width: 1,
                            type: 'solid'
                        }
                    },
                    // 分割线
                    splitLine: {
                        show: true,
                        length: 10,
                        lineStyle: {
                            color: '#9D322D',
                            width: 2,
                            type: 'solid'
                        }
                    },
                    // 分割线标识
                    axisLabel: {
                        show: true,
                        distance: 5,
                        formatter: function (v) {
                            switch (v + '') {
                                case '-180':
                                    return ''
                                default:
                                    return v
                            }
                        }
                    },

                    // 指针设置
                    pointer: {
                        length: '90%',
                        width: 6
                    },
                    detail: {
                        show: false
                    },
                    data: [
                        {
                            value: 0,
                            name: 'A相',
                            itemStyle: {
                                color: '#DAA520'
                            }
                        },
                        {
                            value: 0,
                            name: 'B相',
                            itemStyle: {
                                color: '#2E8B57'
                            }
                        },
                        {
                            value: 0,
                            name: 'C相',
                            itemStyle: {
                                color: '#A52a2a'
                            }
                        }
                    ]
                }
            ]
        }
    }
    //电流相角
    echartsData1.value.options.series[0].data.map((item: any) => {
        iRadioList.value.map((vv: any) => {
            if (item.name.includes(vv.phase)) {
                item.value = vv.statisticalData
            }
        })
    })
    //电压相角
    echartsData1.value.options.series[1].data.map((item: any) => {
        vRadioList.value.map((vv: any) => {
            if (item.name.includes(vv.phase)) {
                item.value = vv.statisticalData
            }
        })
    })

    //新的实时数据
    // 电流相角
    echartsData1.value.options.series[0].data[0].value = realData.value.i1AngA || 0
    echartsData1.value.options.series[0].data[1].value = realData.value.i1AngB || 0
    echartsData1.value.options.series[0].data[2].value = realData.value.i1AngC || 0
    // 电压相角
    echartsData1.value.options.series[1].data[0].value = realData.value.v1AngA || 0
    echartsData1.value.options.series[1].data[1].value = realData.value.v1AngB || 0
    echartsData1.value.options.series[1].data[2].value = realData.value.v1AngC || 0

    for (var i = 0; i < echartsData1.value.options.series.length; i++) {
        echartsData1.value.options.series[i].type = 'gauge'
        echartsData1.value.options.series[i].startAngle = 180
        echartsData1.value.options.series[i].endAngle = -360
        echartsData1.value.options.series[i].center = ['50%', '50%']
    }
}
//渲染echarts
const init = () => {
    echartsData.value = {
        options: {
            tooltip: {},
            toolbox: {
                show: false
            },
            series: [
                {
                    type: 'gauge',
                    startAngle: 180,
                    endAngle: 0,
                    min: 0,
                    max: 450,
                    radius: '150%',
                    center: ['55%', '76%'],
                    splitNumber: 2, //刻度数量
                    axisLine: {
                        show: true,
                        lineStyle: {
                            width: 10,
                            shadowBlur: 0,
                            color: [
                                [0.3, '#91c7a1'],
                                [0.7, '#63869e'],
                                [1, '#002B6A']
                            ]
                        }
                    },

                    itemStyle: {
                        normal: {
                            shadowBlur: 10
                        }
                    },
                    //标题位置
                    title: {
                        fontWeight: 'bolder',
                        fontSize: 12,
                        offsetCenter: ['-130%', '-20%']
                    },
                    //数值位置
                    detail: {
                        fontSize: 12,
                        valueAnimation: true,
                        formatter: '{value}',
                        offsetCenter: ['0%', '25%']
                    },
                    data: [
                        {
                            value: 0,
                            name: 'A相'
                        }
                    ]
                }
            ]
        }
    }
    echartsDataV1.value = JSON.parse(JSON.stringify(echartsData.value))
    echartsDataV2.value = JSON.parse(JSON.stringify(echartsData.value))
    echartsDataV3.value = JSON.parse(JSON.stringify(echartsData.value))
    echartsDataV1.value.options.series[0].max = 12
    echartsDataV2.value.options.series[0].max = 12
    echartsDataV3.value.options.series[0].max = 12
    echartsDataA1.value = echartsData.value
    echartsDataA2.value = echartsData.value
    echartsDataA3.value = echartsData.value
}
//接收父组件传递的table数据
const dataList: any = ref([])
const listV: any = ref([])
const listA: any = ref([])
const loading = ref(false)
//定义表格所需要的数据
const tableData: any = ref([])
const columnsData: any = ref([])
const getColumns = () => {
    getRealTimeTableList().then(res => {
        columnsData.value = res.data
        getTableData(dataList.value)
    })
}
//处理表格数据
const columnsDataTop: any = ref([])
const columnsDataBot: any = ref([])
const getTableData = (list: any) => {
    loading.value = true
    tableData.value = list
    columnsDataTop.value = []
    columnsDataBot.value = []

    columnsData.value.map((item: any, index: any) => {
        item.map((vv: any) => {
            vv.statisticalData = list.find((kk: any) => {
                return kk.anotherName == vv.showName && kk.phase == vv.phase
            })?.statisticalData
        })
        if (index < 4) {
            columnsDataTop.value.push(item)
        }
        if (index >= 4) {
            columnsDataBot.value.push(item)
        }
        loading.value = false
    })
}
//电流相角数组&电压相角数组
const iRadioList: any = ref([])
const vRadioList: any = ref([])

const realData = ref<any>({})
const realList = ref<any>([])

// 计算电压单位
const voltageUnit = computed(() => {
  return currentDataLevel.value === 'Primary' ? 'kV' : 'V'
})

// 计算功率单位
const powerUnit = computed(() => {
  return currentDataLevel.value === 'Primary' ? 'kW' : 'W'
})

// 计算无功功率单位
const reactivePowerUnit = computed(() => {
  return currentDataLevel.value === 'Primary' ? 'kVar' : 'Var'
})

// 计算视在功率单位
const apparentPowerUnit = computed(() => {
  return currentDataLevel.value === 'Primary' ? 'kVA' : 'VA'
})

const setRealData = (val: any,dataLevel:string) => {
    // 只有当 dataLevel 真正改变时才更新
    if (dataLevel !== previousDataLevel.value) {
        currentDataLevel.value = dataLevel
        previousDataLevel.value = dataLevel
    }
    realData.value = val
    realList.value = [val]
    initRadioCharts()
    //新的
    echartsDataV1.value.options.series[0].max = realData.value.vRmsA == 0 ? 1 : Math.ceil(realData.value.vRmsA * 1.2)
    // console.log("🚀 ~ setRealData ~  realData.value.vRmsA:", realData.value.vRmsA)
    echartsDataV1.value.options.series[0].data = [
        {
            name: 'A相',
            value: realData.value.vRmsA || 0
        }
    ]
    pieChart1.value.initChart()
    echartsDataV2.value.options.series[0].max = realData.value.vRmsB == 0 ? 1 : Math.ceil(realData.value.vRmsB * 1.2)
    echartsDataV2.value.options.series[0].data = [
        {
            name: 'B相',
            value: realData.value.vRmsB || 0
        }
    ]
    pieChart2.value.initChart()
    echartsDataV3.value.options.series[0].max = realData.value.vRmsC == 0 ? 1 : Math.ceil(realData.value.vRmsC * 1.2)
    echartsDataV3.value.options.series[0].data = [
        {
            name: 'C相',
            value: realData.value.vRmsC || 0
        }
    ]
    pieChart3.value.initChart()
    //新的电流有效值
    echartsDataA1.value.options.series[0].max = realData.value.iRmsA == 0 ? 1 : Math.ceil(realData.value.iRmsA * 1.2)
    echartsDataA1.value.options.series[0].data = [
        {
            name: 'A相',
            value: realData.value.iRmsA || 0
        }
    ]
    pieChart4.value.initChart()
    echartsDataA2.value.options.series[0].max = realData.value.iRmsB == 0 ? 1 : Math.ceil(realData.value.iRmsB * 1.2)
    echartsDataA2.value.options.series[0].data = [
        {
            name: 'B相',
            value: realData.value.iRmsB || 0
        }
    ]
    pieChart5.value.initChart()
    echartsDataA3.value.options.series[0].max = realData.value.iRmsC == 0 ? 1 : Math.ceil(realData.value.iRmsC * 1.2)
    echartsDataA3.value.options.series[0].data = [
        {
            name: 'C相',
            value: realData.value.iRmsC || 0
        }
    ]
    pieChart6.value.initChart()
}
defineExpose({ setRealData })
onMounted(() => {
    init()
    initRadioCharts()
})
</script>
<style lang="scss" scoped>
.view {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;

    .view_top {
        width: 100%;
        height: 340px !important;
        display: flex;
        justify-content: space-between;
        //border: 1px solid #eee;
        // padding: 10px;
        margin-top: 10px;

        .view_top_left,
        .view_top_right {
            // width: 300px;
            flex: 1;
            height: 100%;
            // padding: 10px;
            display: flex;
            flex-direction: column;
            align-items: center;
            border: 1px solid #eee;

            .left_charts,
            .right_charts {
                flex: none;
                width: 100%;
                height: 90px;
                // margin-bottom: 16px;
                // padding: 10px;
                margin-bottom: 8px;
            }
        }

        .view_top_mid {
            // flex: 1;
            width: 40%;
            border: 1px solid #eee;
            margin: 0 10px;
            // padding: 10px;
            position: relative;

            .mid_charts {
                width: 100%;
                height: 280px;
                // position: absolute;
                // top: 20%;
                // bottom: 10px;
            }
        }

        .left_charts_title,
        .mid_charts_title,
        .right_charts_title {
            width: 100%;
            height: 20px;
            text-align: left;
            font-weight: 800;
            font-weight: 16px;
            line-height: 20px;
            margin: 10px 0 0 10px;
            padding-left: 5px;
            box-sizing: border-box;
        }
    }

    .view_bot {
        // min-height: 300px;
        margin: 10px 0 0 0;
        overflow: auto !important;
        flex: 1 !important;
        // padding-bottom: 200px !important;
        box-sizing: border-box !important;

        .view_bot_tables {
            margin-bottom: 10px;
            height: auto;
            min-height: 120px;
        }
    }
}

.view::-webkit-scrollbar {
    display: none;
}

.table_container {
    width: 100%;
    height: auto;
    display: flex;
    align-items: center;
    justify-content: center;

    .table {
        flex: 1;

        .table_info {
            width: 100%;
            height: 120px;
            // border: 1px solid #eee;
            border-bottom: 2px solid #eee;
            margin-bottom: 20px;
            display: flex;
            flex-direction: column;
        }

        .thead {
            width: 100%;
            height: auto;
            background: #f4f6f9;
            text-align: center;
            display: flex;
            flex-direction: column;

            .thead_top {
                width: 100%;
                height: 40px;
                line-height: 40px;
                border: 1px solid #eee;
                color: #111;
                font-size: 14px;
                font-weight: 800;
            }

            .thead_bot {
                width: 100%;
                height: 40px;
                line-height: 40px;
                display: flex;
                color: #111;
                font-size: 14px;
                font-weight: 800;

                .thead_bot_cell {
                    flex: 1;
                    border: 1px solid #eee;
                }
            }
        }

        .tbody {
            flex: 1;
            display: flex;
            text-align: center;

            .tbody_cell {
                flex: 1;
                text-align: center;
                line-height: 40px;
                border: 1px solid #eee;
                border-bottom: 0;
            }
        }

        .tbody:hover {
            background: #f4f6f9;
        }
    }
}

.table_container:hover {
    .table {
        .tbody {
            background: #f4f6f9;
        }
    }
}

:deep(.view_bot) {

    .vxe-table--render-default .vxe-body--column:not(.col--ellipsis),
    .vxe-table--render-default .vxe-footer--column:not(.col--ellipsis),
    .vxe-table--render-default .vxe-header--column:not(.col--ellipsis) {
        padding: 5px !important;
    }

    .vxe-table--body-wrapper {
        min-height: 32px !important;
    }
}

::v-deep .el-collapse-item__header {
    font-size: 16px !important;
    font-weight: 800 !important;
}
</style>
