<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-select
                        v-model="statisticalType"
                        value-key="id"
                        style="width: 120px; margin-right: 80px"
                        @change="statiStics"
                        filterable
                    >
                        <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" />
                </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 Voltage = dictData.getBasicData('Dev_Voltage_Stand')
const tableData: any = ref([])
const options = dictData.getBasicData('Statistical_Type', ['Report_Type', 'Power_Network'])
const time = ref('1')
const statisticalType = ref(options[0])
const loadTypeArr = dictData.getBasicData('Interference_Source')

const rowList: any = ref({})
const trendEChart: any = ref()

const picEChart = ref()
const open = async (row: any) => {
    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: statisticalType.value }).then(res => {
        picEChart.value = {
            title: {
                text: ''
            },
            xAxis: {
                data: res.data.map((item: any) => item.orgName)
            },
            yAxis: {
                name: '%',
                min: 0,
                max: 100
            },
            options: {
                dataZoom: null,
                series: [
                    {
                        name: '数据完整性',
                        type: 'bar',

                        data: res.data.map((item: any) => item.integrityRate),
                        label: {
                            show: true,
                            position: 'top',
                            fontSize: 12
                        }
                    },
                    {
                        name: '在线率',
                        type: 'bar',
                        data: res.data.map((item: any) => item.onLineRate),
                        label: {
                            show: true,
                            position: 'top',
                            fontSize: 12
                        }
                    },
                    {
                        name: '超标监测点占比',
                        type: 'bar',
                        data: res.data.map((item: any) => item.outOfStandardRate),
                        label: {
                            show: true,
                            position: 'top',
                            fontSize: 12
                        }
                    }
                ]
            }
        }
    })
}
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
}

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>
