<template>
    <div class="boxLeft" :style="height">
        <!-- 指标合格率统计 -->

        <el-card>
            <template #header>
                <div class="card-header" style="display: flex; justify-content: space-between">
                    <span>指标合格率统计</span>
                    <span class="info" @click="open(0)">
                        详情
                        <ArrowRight style="width: 12px" />
                    </span>
                </div>
            </template>
            <MyEChart :style="`height:calc(${boxHeight.height} - 40px)`" :options="passingCharts" />
        </el-card>

        <el-card>
            <template #header>
                <div class="card-header">
                    <span>稳态指标超标占比</span>
                </div>
            </template>
            <div>
                <div class="monitoringPoints">
                    <div>在线监测点数：{{ monitorList.onlineNum }}</div>
                    <div>超标监测点数：{{ monitorList.overNum }}</div>
                    <div>超标监测点占比：{{ monitorList.overRatio }}</div>
                </div>

                <MyEChart :style="boxHeight" :options="exceededCharts" />
            </div>
        </el-card>
        <!-- 稳态电能质量指标水平评估 -->
        <el-card>
            <template #header>
                <div class="card-header">
                    <span>稳态电能质量指标水平评估</span>
                </div>
            </template>

            <div :style="`height:calc(${boxHeight.height} + 20px)`" class="boxSteps">
                <el-segmented v-model="active" :options="Voltage" block @change="handleClick" />
                <div class="evaluationData mt10">
                    <div v-for="(item, i) in evaluationData">
                        <el-row style="width: 100%">
                            <el-col :span="12" style="display: flex">
                                <img :src="url[i]" />
                                <span>{{ item.targetName }}</span>
                            </el-col>
                            <el-col :span="12" style="display: flex">
                                <div style="width: 50%">
                                    均值：
                                    <span style="color: #339966">{{ item.avg == 3.14159 ? '--' : item.avg }}</span>
                                </div>
                                <div>
                                    标准差：
                                    <span style="color: #ff9900">{{ item.avg == 3.14159 ? '--' : item.avg }}</span>
                                </div>
                            </el-col>
                        </el-row>
                    </div>
                </div>
            </div>
        </el-card>

        <!-- 暂态事件统计 -->
        <el-card>
            <template #header>
                <div class="card-header">
                    <span>暂态事件统计</span>
                </div>
            </template>
            <MyEChart :style="boxHeight" :options="statisticsCharts" />
        </el-card>
        <!-- 指标合格率统计 -->
        <statistics ref="statisticsRef" />
    </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, provide } from 'vue'
import statistics from '../components/city/statistics.vue'
import MyEChart from '@/components/echarts/MyEchart.vue'
import { useDictData } from '@/stores/dictData'
import { mainHeight } from '@/utils/layout'

import { color } from '@/components/echarts/color'
import { ArrowRight } from '@element-plus/icons-vue'
import { getAssessDetail, evaluationDetail, getGeneralSituation, getEvaluationData } from '@/api/device-boot/panorama'

const dictData = useDictData()
const height = mainHeight(20)
const boxHeight: any = mainHeight(280, 4)
const statisticsRef = ref()
const formRow: any = ref({})
const monitorList: any = ref({})
const statisticsCharts: any = ref({})
const rowList: any = ref({})
const chartRef = ref()
const evaluationData: any = ref([])
const passingCharts = ref()
const exceededCharts = ref()

const Voltage: any = dictData
    .getBasicData('Dev_Voltage_Stand')
    .filter(item => {
        if (item.code == '35kV' || item.code == '500kV' || item.code == '220kV' || item.code == '110kV') {
            return item
        }
    })
    .map(item => {
        return {
            label: item.name,
            value: item.id
        }
    })
const active: any = ref(Voltage[0].value)
const url: any = [
    new URL(`@/assets/img/PLPC.png`, import.meta.url),
    new URL(`@/assets/img/DYPC.png`, import.meta.url),
    new URL(`@/assets/img/JBL.png`, import.meta.url),
    new URL(`@/assets/img/SXDY.png`, import.meta.url),
    new URL(`@/assets/img/SB.png`, import.meta.url)
]
const open = (e: number) => {
    if (e == 0) {
        statisticsRef.value.open(formRow.value)
    }
}
const info = (row: any) => {
    let form = {
        ...row,
        id: row.orgNo,
        deptIndex: row.orgNo,
        orgId: row.orgNo,
        ids: [],
        statisticalType: dictData.getBasicData('Statistical_Type', ['Report_Type'])[0],
        isUpToGrid: row.isUpToGrid,
        monitorFlag: row.isUpToGrid
    }
    formRow.value = form
    // 指标合格率统计
    getAssessDetail(form).then(res => {
        let data = [
            {
                name:
                    '电压偏差：' +
                    (res.data[0].vdevQualifyData == 3.14159 ? '暂无数据' : res.data[0].vdevQualifyData + '%'),
                value: res.data[0].vdevQualifyData == 3.14159 ? '暂无数据' : res.data[0].vdevQualifyData
            },
            {
                name:
                    '频率偏差：' +
                    (res.data[0].freqQualifyData == 3.14159 ? '暂无数据' : res.data[0].freqQualifyData + '%'),
                value: res.data[0].freqQualifyData == 3.14159 ? '暂无数据' : res.data[0].freqQualifyData
            },
            {
                name:
                    '电压总谐波畸变率：' +
                    (res.data[0].harmQualifyData == 3.14159 ? '暂无数据' : res.data[0].harmQualifyData + '%'),
                value: res.data[0].harmQualifyData == 3.14159 ? '暂无数据' : res.data[0].harmQualifyData
            },
            {
                name:
                    '闪变：' +
                    (res.data[0].flickerQualifyData == 3.14159 ? '暂无数据' : res.data[0].flickerQualifyData + '%'),
                value: res.data[0].flickerQualifyData == 3.14159 ? '暂无数据' : res.data[0].flickerQualifyData
            },
            {
                name:
                    '三相电压不平衡度：' +
                    (res.data[0].unbalanceQualifyData == 3.14159 ? '暂无数据' : res.data[0].unbalanceQualifyData + '%'),
                value: res.data[0].unbalanceQualifyData == 3.14159 ? '暂无数据' : res.data[0].unbalanceQualifyData
            }
        ]

        let optionData = getData(data)
        passingCharts.value = {
            tooltip: {
                show: true,
                trigger: 'item',
                formatter: '{b}'
            },
            xAxis: {
                show: false
            },

            yAxis: {
                show: false
            },

            legend: {
                data: data.map((item: any) => item.name),
                type: 'scroll',
                orient: 'vertical',
                icon: 'roundRect',
                right: '10',

                itemGap: 15,
                itemWidth: 12,
                itemHeight: 12,
                textStyle: {
                    fontSize: '0.85rem'
                }
            },

            options: {
                dataZoom: null,
                series: optionData.series
            }
        }

        function getData(data: any) {
            var res: any = {
                series: [],
                yAxis: []
            }
            for (let i = 0; i < data.length; i++) {
                res.series.push({
                    type: 'pie',
                    clockWise: false, //顺时加载
                    hoverAnimation: false, //鼠标移入变大
                    radius: [70 - i * 15 + '%', 80 - i * 15 + '%'],
                    center: ['30%', '50%'],
                    label: {
                        show: false
                    },

                    data: [
                        {
                            value: data[i].value,
                            name: data[i].name
                        },
                        {
                            value: 100 - data[i].value,
                            name: '',
                            itemStyle: {
                                color: 'rgba(0,0,0,0)',
                                borderWidth: 0
                            },
                            tooltip: {
                                show: false
                            },
                            hoverAnimation: true
                        }
                    ]
                })
                res.series.push({
                    name: '',
                    type: 'pie',
                    silent: true,
                    z: 1,
                    clockWise: false, //顺时加载
                    hoverAnimation: false, //鼠标移入变大
                    radius: [70 - i * 15 + '%', 80 - i * 15 + '%'],
                    center: ['30%', '50%'],
                    label: {
                        show: false
                    },

                    data: [
                        {
                            value: 7.5,
                            itemStyle: {
                                color: '#E3F0FF',
                                borderWidth: 0
                            },
                            tooltip: {
                                show: false
                            },
                            hoverAnimation: false
                        }
                    ]
                })
            }
            return res
        }
    })
    // 稳态指标超标占比

    evaluationDetail(form).then(res => {
        monitorList.value = res.data[0]
        exceededCharts.value = {
            tooltip: {},
            yAxis: {
                name: '%',
                type: 'value',
                max: 100
            },
            legend: {
                data: ['超标监测点数', '超标天数']
            },
            xAxis: {
                type: 'category',
                data: res.data[0].list?.map((item: any) => {
                    return item.targetName.length > 4
                        ? item.targetName.slice(0, 4) + '\n ' + item.targetName.slice(4)
                        : item.targetName
                }),
                axisLabel: {
                    color: '#000',
                    fontSize: 12
                }
            },
            grid: {
                top: '30px',
                left: '10px',
                right: '20px'
            },

            options: {
                dataZoom: null,
                series: [
                    {
                        name: '超标监测点数',
                        type: 'bar',
                        data: res.data[0].list?.map((item: any) => item.overNum),
                        label: {
                            show: true,
                            position: 'top',
                            fontSize: 12,
                            formatter: function (params: any) {
                                return `${params.value == 3.14159 ? '' : params.value}`
                            }
                        }
                    },
                    {
                        name: '超标天数',
                        type: 'bar',
                        data: res.data[0].list?.map((item: any) => item.overDay),
                        label: {
                            show: true,
                            position: 'top',
                            fontSize: 12,
                            formatter: function (params: any) {
                                return `${params.value == 3.14159 ? '' : params.value}`
                            }
                        }
                    }
                ]
            }
        }
    })
    // 稳态电能质量指标水平评估

    handleClick(active.value)
    // 暂态电能质量水平评估
    getGeneralSituation({ ...form, monitorFlag: form.isUpToGrid == 0 ? 2 : 1 }).then(res => {
        statisticsCharts.value = {
            tooltip: {},
            yAxis: {
                type: 'value'
            },
            legend: {
                show: false
            },
            xAxis: {
                type: 'category',
                data: ['电压暂升', '电压暂降', '短时中断']
            },
            grid: {
                top: '30px',
                left: '10px',
                right: '20px'
            },

            options: {
                dataZoom: null,
                series: [
                    {
                        name: '暂态个数',
                        type: 'bar',
                        data: [res.data[0].upCount, res.data[0].sagsCount, res.data[0].breakCount],
                        label: {
                            show: true,
                            position: 'top',
                            fontSize: 12
                        }
                    }
                ]
            }
        }
    })
}

// 点击电压等级
const handleClick = (i: any) => {
    active.value = i
    getEvaluationData({
        ...formRow.value,
        voltageLevel: i
    }).then(res => {
        evaluationData.value = res.data
    })
}

defineExpose({ info })
</script>

<style lang="scss" scoped>
.boxLeft {
    background-color: #fff;
    width: 25%;
    padding: 10px 0px 10px 10px;
    font-size: 13px;
    overflow: hidden;
}

.info {
    font-weight: normal;
    display: flex;
    font-size: 12px;
    cursor: pointer;
    align-items: center;
}
.evaluate {
    height: 60px;
    border: 1px solid #ccc;
    margin: 10px 0;
    padding: 10px 0;
    display: flex;
    justify-content: space-around;
    text-align: center;
    overflow-x: auto;
    overflow-y: hidden;
}
.boxR {
    border: 1px solid #ccc;
    margin: 10px 0;
    padding: 5px;
    .num {
        color: #2478f2;
    }
    .top {
        display: flex;
        justify-content: space-between;
    }
    .bottom {
        margin: 0;
        border: 0px;
    }
}

:deep(.el-card) {
    .el-card__header {
        padding: 5px 20px;
        font-size: 1.2rem;
        &::before {
            width: 0.3rem;
            height: 1.2rem;
            margin-top: 0.3rem;
            position: absolute;
            content: '';
            background: var(--el-color-primary);
            border-radius: 0.02rem;
            left: 1.2rem;
        }
    }
    .el-card__body {
        padding: 10px;
    }
    margin-bottom: 10px;
    margin-right: 10px;
}
.monitoringPoints {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    text-align: center;
}
:deep(.boxSteps) {
    font-size: 1rem;
    .evaluationData {
        display: grid;
        height: calc(100% - 30px);
        grid-template-rows: repeat(5, auto);
    }
    img {
        width: 1.3rem;
        height: 1.3rem;
        margin-right: 10px;
    }
}
:deep(.el-segmented) {
    --el-border-radius-base: 16px;
}
</style>
