<template>
    <div :class="show ? 'show' : 'noshow'">
        <div class="boxLeft" :style="height">
            <!-- 指标合格率统计 -->
            <div :style="`height:calc(${boxHeight.height} - 40px)`">
                <div class="title">
                    <span>指标合格率统计</span>
                    <span class="info" @click="open(0)">
                        详情
                        <ArrowRight style="width: 12px" />
                    </span>
                </div>

                <div style="display: flex" class="mt2">
                    <img src="@/assets/img/FGX.png" />
                </div>
                <MyEChart :style="`height:calc(${EchHeight.height} - 40px)`" :options="passingCharts" />
            </div>

            <!-- 稳态指标超标占比 -->
            <div :style="`height:calc(${boxHeight.height} + 40px)`">
                <div class="title">
                    <span>稳态指标超标占比</span>
                    <span class="info" @click="open(1)">
                        详情
                        <ArrowRight style="width: 12px" />
                    </span>
                </div>

                <div style="display: flex" class="mt2">
                    <img src="@/assets/img/FGX.png" />
                </div>
                <div>
                    <div class="monitoringPoints">
                        <div>
                            <span class="line"></span>
                            在线监测点数：{{ monitorList?.onlineNum }}
                        </div>
                        <div>
                            <span class="line"></span>
                            超标监测点数：{{ monitorList?.overNum }}
                        </div>
                        <div>
                            <span class="line"></span>
                            超标监测点占比：{{ monitorList?.overRatio }}
                        </div>
                    </div>

                    <MyEChart :style="`height:calc(${EchHeight.height} + 15px)`" :options="exceededCharts" />
                </div>
            </div>

            <!-- 暂态事件统计 -->
            <div :style="`height:calc(${boxHeight.height} - 10px)`">
                <div class="title">
                    <span>暂态事件统计</span>
                </div>

                <div style="display: flex" class="mt2">
                    <img src="@/assets/img/FGX.png" />
                </div>
                <MyEChart :style="`height:calc(${EchHeight.height} - 10px)`" :options="statisticsCharts" />
            </div>

            <!-- 稳态电能质量指标水平评估 -->
            <!-- <div>
                <div class="title">
                    <span>技术监督统计</span>
        
                </div>
                <div style="display: flex" class="mt2">
                    <img src="@/assets/img/FGX.png" />
                </div>
                <div style="height: 150px" class="boxR">
                    <el-segmented v-model="active" :options="Voltage" block />
                    <el-row
                        style="height: calc(100% - 45px); display: flex; justify-content: space-around"
                        class="ml10 mr10 mt5"
                    >
                        <el-col :span="11" class="col pt10">
                            <div>
                                <span>异常问题总数：</span>
                                <span style="color: #2dcd28">{{ technology[active]?.abnormalNum || 0 }}个</span>
                            </div>
                            <div>
                                <span style="width: 120px">已关联工单数：</span>
                                <span style="color: #81b337">{{ technology[active]?.associatedNum || 0 }}个</span>
                            </div>
                            <div>
                                <span style="width: 120px">　工单转换率：</span>
                                <span style="color: #338dff">{{ technology[active]?.conversionNum || 0 }}%</span>
                            </div>
                        </el-col>
                        <el-col :span="11" class="col pt10" :offset="0.5">
                            <div>
                                <span>工单总数：</span>
                                <span style="color: #2dcd28">{{ technology[active]?.workNum || 0 }}个</span>
                            </div>
                            <div>
                                <span style="width: 120px">已处理工单数：</span>
                                <span style="color: #81b337">{{ technology[active]?.processedNum || 0 }}个</span>
                            </div>
                            <div>
                                <span style="width: 120px">　工单处置率：</span>
                                <span style="color: #338dff">{{ technology[active]?.disposalNum || 0 }}%</span>
                            </div>
                        </el-col>
                    </el-row>
                </div>
            </div> -->
            <!-- 指标合格率统计 -->
            <statistics ref="statisticsRef" />
            <!-- 稳态指标超标占比 -->
            <exceeded ref="exceededRef" />
        </div>
        <img
            class="imgR"
            :style="show ? 'transform: rotate(180deg);' : 'transform: rotate(0deg);'"
            @click="show = !show"
            src="@/assets/img/QH.png"
        />
    </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, provide } from 'vue'
import statistics from '../components/city/statistics.vue'
import exceeded from '../components/city/exceeded.vue'
import MyEChart from '@/components/echarts/MyEchart.vue'
import { useDictData } from '@/stores/dictData'
import { mainHeight } from '@/utils/layout'
import { getSupervisionCityDetailsData } from '@/api/device-boot/panorama'
import { ArrowRight } from '@element-plus/icons-vue'
import { getAssessDetail, evaluationDetail, getGeneralSituation } from '@/api/device-boot/panorama'

const dictData = useDictData()
const height = mainHeight(30)
const show = ref(false)
const boxHeight: any = mainHeight(30, 3)
const EchHeight: any = mainHeight(130, 3)
const statisticsRef = ref()
const exceededRef = ref()
const formRow: any = ref({})
const monitorList: any = ref({})
const statisticsCharts: any = ref({})
import { useConfig } from '@/stores/config'
const config = useConfig()
const passingCharts = ref()
const exceededCharts = ref()
const technology: any = ref({})

const Voltage: any = [
    {
        label: '技术监督计划',
        value: 'survey'
    },
    {
        label: '在线监测',
        value: 'onLine'
    },
    {
        label: '用户投诉',
        value: 'user'
    },
    {
        label: '试运行问题',
        value: 'trialRun'
    }
]

const active: any = ref(Voltage[0].value)

const open = (e: number) => {
    if (e == 0) {
        statisticsRef.value.open(formRow.value)
    } else if (e == 1) {
        exceededRef.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 == 0 ? null : row.isUpToGrid
    }
    formRow.value = form
    // 指标合格率统计
    getAssessDetail(form).then(res => {
        let data = [
            {
                name:
                    '电压偏差：' +
                    (res.data[0]?.vdevQualifyData == 3.14159 ? '暂无数据' : res.data[0]?.vdevQualifyData || 0 + '%'),
                value: res.data[0]?.vdevQualifyData == 3.14159 ? '暂无数据' : res.data[0]?.vdevQualifyData
            },
            {
                name:
                    '频率偏差：' +
                    (res.data[0]?.freqQualifyData == 3.14159 ? '暂无数据' : res.data[0]?.freqQualifyData || 0 + '%'),
                value: res.data[0]?.freqQualifyData == 3.14159 ? '暂无数据' : res.data[0]?.freqQualifyData
            },
            {
                name:
                    '电压总谐波畸变率：' +
                    (res.data[0]?.harmQualifyData == 3.14159 ? '暂无数据' : res.data[0]?.harmQualifyData || 0 + '%'),
                value: res.data[0]?.harmQualifyData == 3.14159 ? '暂无数据' : res.data[0]?.harmQualifyData
            },
            {
                name:
                    '闪变：' +
                    (res.data[0]?.flickerQualifyData == 3.14159
                        ? '暂无数据'
                        : res.data[0]?.flickerQualifyData || 0 + '%'),
                value: res.data[0]?.flickerQualifyData == 3.14159 ? '暂无数据' : res.data[0]?.flickerQualifyData
            },
            {
                name:
                    '三相电压不平衡度：' +
                    (res.data[0]?.unbalanceQualifyData == 3.14159
                        ? '暂无数据'
                        : res.data[0]?.unbalanceQualifyData || 0 + '%'),
                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: {
                toolbox: null,
                dataZoom: null,
                series: optionData.series
            }
        }

        function getData(data: any) {
            var res: any = {
                series: []
            }
            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 || 0,
                            name: data[i].name
                        },
                        {
                            value: 100 - (data[i].value || 0),
                            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: {},
            color: [config.layout.elementUiPrimary[0], '#FFBF00'],
            yAxis: {
                name: '',
                type: 'value'
                // max: 100
            },
            legend: {
                data: ['超标监测点数', '超标天数'],
                top: '5'
            },
            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: 10,
                    rotate: 30
                }
            },
            grid: {
                top: '35px',
                left: '10px',
                right: '20px'
            },

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

    getGeneralSituation({ ...form, monitorFlag: form.isUpToGrid == 0 ? 2 : 1 }).then(res => {
        let data = [
            {
                name: '电压暂升',
                value: res.data[0].upCount - 0
            },
            {
                name: '电压暂降',
                value: res.data[0].sagsCount - 0
            },
            {
                name: '短时中断',
                value: res.data[0].breakCount - 0
            }
        ]
        statisticsCharts.value = {
            title: {
                text: '总数：' + (data[0].value + data[1].value + data[2].value),
                left: '23%',
                top: '45%',
                textStyle: {
                    // fontWeight: 600,
                    fontSize: 12
                }
                // subtext: '总数',
                // subtextStyle: {
                //     fontWeight: 550,
                //     fontSize: 14
                // }
            },

            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                top: 'center',
                right: '10%',
                formatter: function (e: any) {
                    return e + '   ' + data.filter(item => item.name == e)[0].value + '条'
                }
            },
            xAxis: {
                show: false
            },
            yAxis: {
                show: false
            },
            color: [config.layout.elementUiPrimary[0], '#FFBF00', '#FF9100'],
            options: {
                toolbox: null,
                dataZoom: null,
                series: [
                    {
                        type: 'pie',
                        center: ['30%', '50%'],
                        radius: ['50%', '70%'],
                        label: {
                            show: false,
                            position: 'outside',
                            textStyle: {
                                //数值样式
                            }
                        },

                        data: data
                    }
                ]
            }
        }
    })
    // 技术监督
    getSupervisionCityDetailsData(form).then(res => {
        technology.value = res.data
    })
}

defineExpose({ info, show })
</script>

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

.title {
    // height: ;
    display: flex;
    justify-content: space-between;
    font-size: 15px;
    line-height: 23px;
    padding-left: 5px;
    width: 100%;
    font-weight: 550;

    .info {
        font-weight: normal;
        display: flex;
        font-size: 12px;
        cursor: pointer;
        color: #757575;
    }
}
.TJTop {
    display: flex;
    img {
        height: 1.2rem;
        width: 1.2rem;
        margin-right: 5px;
    }
}
.evaluate {
    margin: 10px 0;
    display: flex;
    justify-content: space-around;
    text-align: center;
    overflow-x: auto;
    overflow-y: hidden;
    .line {
        display: inline-block;
        width: 0.5rem;
        height: 0.5rem;
        border-radius: 0.25rem;
        background: var(--el-color-primary);
        margin-right: 2px;
        margin-bottom: 1px;
    }
}
.boxR {
    margin: 10px 0 0 0;

    .num {
        color: #2478f2;
    }
    .top {
        display: flex;
        justify-content: space-between;
    }
    .bottom {
        margin: 0;
        border: 0px;
    }

    .harmonic {
        display: grid;
        .progress {
            display: flex;
            align-items: center;
            .text {
                width: 50px;
                font-size: 12px;
            }
            .el-progress {
                flex: 1;
            }
        }
    }
}
:deep(.el-select) {
    min-width: 120px;
}
.col {
    display: grid;
    grid-template-columns: 1fr;
    border-radius: 10px;
    text-align: center;
    align-items: center;
    background-color: #edededc0;
}
.imgR {
    position: absolute;
    padding: 10px;
    top: calc(50% - 80px);
    left: -23px;

    height: 200px;
    cursor: pointer;
}
.show {
    width: 0px;
    transition: all 0.1s ease;
    .boxLeft {
        padding: 0;
    }
}
.noshow {
    width: 25%;
    transition: all 0.3s ease;
    .boxLeft {
        padding: 10px 10px 10px 10px;
    }
}
:deep(.el-progress__text) {
    font-size: 0.8rem !important    ;
}
.monitoringPoints {
    // display: grid;
    font-size: 12px;
    display: flex;
    justify-content: space-between;
    // grid-template-columns: 1fr 1fr 1fr;
    margin-top: 10px;
    margin-bottom: 3px;
    .line {
        display: inline-block;
        width: 0.5rem;
        height: 0.5rem;
        border-radius: 0.25rem;
        background: var(--el-color-primary);
        margin-right: 2px;
        margin-bottom: 1px;
    }
}
:deep(.el-dialog__body) {
    max-height: none !important;
}
</style>
