<template>
    <!-- 监测点详情 -->
    <el-dialog draggable title="监测点统计" v-model="dialogVisible" width="1400px" :before-close="handleClose">
        <el-row style="height: 300px" :gutter="20">
            <el-col :span="12">
                <div class="title">
                    <span>趋势分析</span>
                    <!-- <el-select v-model="time" style="width: 80px; margin-right: 80px" @change="analysis">
                        <el-option label="年" value="1" />
                        <el-option label="月" value="3" />
                    </el-select> -->
                    <el-radio-group v-model="time" style="margin-right: 50px" @change="analysis" size="small">
                        <el-radio-button label="1">年</el-radio-button>
                        <el-radio-button label="3">月</el-radio-button>
                    </el-radio-group>
                </div>
                <MyEChart style="height: 260px" :options="trendEChart" />
            </el-col>
            <el-col :span="12">
                <div class="title">
                    <span>分布统计</span>
                    <el-radio-group v-model="statisticalType" size="small">
                        <el-radio-button v-for="item in options" :label="item.code" @change="statiStics">
                            {{ item.name }}
                        </el-radio-button>
                    </el-radio-group>
                    <!-- <el-select
                        v-model="statisticalType"
                        value-key="id"
                        style="width: 120px; margin-right: 80px"
                        @change="statiStics"
                    >
                        <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item" />
                    </el-select> -->
                </div>
                <div class="pie">
                    <!-- <MyEChart style="height: 260px" :options="picEChart" /> -->
                    <MyEChart style="height: 260px; width: 50%" :options="picEChart" @echartClick="echartClick" />
                    <el-table size="small" height="260px" style="width: 50%" :data="picList">
                        <el-table-column prop="orgName" width="90px" align="center">
                            <template #default="scope">
                                {{ scope.row.orgName.split('(')[0].trim() }}
                            </template>
                        </el-table-column>
                        <el-table-column prop="integrityRate" label="完整性(%)" align="center"></el-table-column>
                        <el-table-column prop="onLineRate" label="在线率(%)" align="center" />
                        <el-table-column prop="outOfStandardRate" label="超标占比(%)" align="center" />
                    </el-table>
                </div>
            </el-col>
        </el-row>
        <div>
            <div class="title">
                <span>区域监测点统计</span>
            </div>
            <vxe-table v-bind="defaultAttribute" ref="vxeRef" height="325px" :data="tableData">
                <vxe-column field="orgName" title="区域" :formatter="formatter" />
                <vxe-column field="num" title="监测点个数" sortable :formatter="formatter" />
                <vxe-column field="integrityRate" sortable title="完整率(%)" />
                <vxe-column field="onLineRate" sortable title="在线率(%)" />
                <vxe-column field="outOfStandardRate" sortable title="超标监测点占比(%)" />
            </vxe-table>
        </div>
    </el-dialog>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import MyEChart from '@/components/echarts/MyEchart.vue'
import { defaultAttribute } from '@/components/table/defaultAttribute'
import { useDictData } from '@/stores/dictData'
import { getGridDiagramLineTendency, getGridDiagramLineData, getHalfReport } from '@/api/device-boot/panorama'
const dictData = useDictData()
const dialogVisible: any = ref(false)
const picList: any = ref([])
const Voltage = dictData.getBasicData('Dev_Voltage_Stand')
const tableData: any = ref([])
const options = dictData.getBasicData('Statistical_Type', ['Report_Type', 'Power_Network', 'JB_Power_Flag'])

const time = ref('1')
const statisticalType = ref('Load_Type')
const loadTypeArr = dictData.getBasicData('Interference_Source')
import { useRouter } from 'vue-router'
const router = useRouter()
const rowList: any = ref({})
const trendEChart: any = ref()

const picEChart = ref()
const open = async (row: any) => {
    statisticalType.value = 'Load_Type'
    time.value = '1'
    rowList.value = {
        deviceInfoParam: {
            ...row
        },
        pageNum: 1,
        pageSize: 1000,
        ...row
    }
    analysis(1)
    // 统计
    statiStics()
    // 监测点列表
    getGridDiagramLineData({
        ...rowList.value,
        statisticalType: rowList.value.deviceInfoParam.statisticalType
    }).then((res: any) => {
        tableData.value = res.data
    })

    dialogVisible.value = true
}
// 分析
const analysis = (e: any) => {
    let time = rowList.value.searchBeginTime.slice(0, 4) + `-01-01`
    // 分析
    getGridDiagramLineTendency({ ...rowList.value, searchBeginTime: time, type: e }).then(res => {
        let name = []
        let data = []
        let num = 0
        for (let k in res.data) {
            name.push(k)
            // num = num + res.data[k]
            data.push(res.data[k])
        }

        trendEChart.value = {
            title: {
                text: '在线监测点总数量'
            },
            xAxis: {
                name: '时间',
                data: name
            },
            legend: {
                show: false
            },
            yAxis: {
                name: '个'
            },

            options: {
                dataZoom: null,
                series: [
                    {
                        name: '接入',
                        type: 'line',
                        data: data,
                        smooth: true,

                        label: {
                            show: true,
                            position: 'top',
                            fontSize: 12
                        }
                    }
                ]
            }
        }
    })
}
// 统计
const statiStics = () => {
    getGridDiagramLineData({
        ...rowList.value,
        statisticalType: options.filter((item: any) => item.code == statisticalType.value)[0]
    }).then(res => {
        picList.value = res.data
        let picData: any = []
        const total = res.data.reduce((sum, item) => {
            const num = (item.orgName.match(/\((\d+)\)/) || [])[1] || 0
            return sum + Number(num)
        }, 0)

        res.data.forEach((item: any) => {
            let value = (item.orgName.match(/\((\d+)\)/) || [])[1] || 0
            picData.push({
                value: value,
                name: item.orgName,
                proportion: (value / total).toFixed(2)
            })
        })

        picEChart.value = {
            tooltip: {
                trigger: 'item',
                formatter: '{b}: 占比 {d}%  '
            },

            legend: {
                show: false
            },
            xAxis: {
                show: false
            },
            yAxis: {
                show: false
            },
            options: {
                dataZoom: null,
                series: [
                    {
                        type: 'pie',
                        startAngle: 360,
                        center: ['50%', '55%'],
                        radius: ['40%', '60%'],
                        endAngle: 0,
                        labelLine: {
                            length: 8,
                            length2: 30,
                            show: true
                        },
                        label: {
                            padding: [0, -30],
                            formatter: '{b}\n\n'
                        },
                        itemStyle: {
                            borderColor: '#fff',
                            borderWidth: 1
                        },

                        data: picData.sort((a, b) => b.value - a.value)
                    }
                ]
            }
        }
    })
}
const formatter = (row: any) => {
    if (row.column.field == 'orgName') {
        return row.cellValue.match(/[\u4e00-\u9fa5]+/g).join('')
    } else if (row.column.field == 'num') {
        return row.row.orgName.match(/\(([^]+)\)/)[1]
    } else {
        return row.cellValue
    }
}
const handleClose = () => {
    tableData.value = []
    dialogVisible.value = false
}
const echartClick = (params: any) => {
   // console.log('🚀 ~ echartClick ~ params:', params.name.split('(')[0])
    router.push({
        name: 'Supervision/Terminaldetection',
        query: {
            type: 'lodType',
            name: params.name.split('(')[0],
            statisticalType:statisticalType.value,
            t: Date.now()
        }
    })
}

defineExpose({ open })
</script>
<style lang="scss" scoped>
:deep(.el-select) {
    min-width: 80px;
}
.title {
    display: flex;
    justify-content: space-between;
    margin: 10px;
    span {
        font-weight: 550;
        font-size: 18px;
    }
}
.pie {
    display: flex;
    justify-content: space-around;
}
:deep(.el-table thead) {
    color: #000;
}
</style>
