<template>
    <div>
        <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item>
                <Area @click="handleNodeClick"></Area>
            </el-form-item>
            <el-form-item>
                <Timeinterval :interval="3" ref="interval" :timeOptions="timeOptions"></Timeinterval>
            </el-form-item>
            <el-form-item label="监测对象标签:">
                <el-select v-model="form.monitorSort" multiple collapse-tags clearable placeholder="请选择监测对象标签">
                    <el-option
                        v-for="item in monitorTagList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit" class="ml10" icon="el-icon-search">查询</el-button>
                <el-button type="primary" icon="el-icon-download" @click="exportEvent">导出</el-button>
            </el-form-item>
        </el-form>
        <vxe-table
            :data="totalData.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
            :height="vh"
            stripe
            border
            :row-config="{ isCurrent: true, isHover: true }"
            size="mini"
            ref="Regionalclassification"
            v-loading="isLoading1"
            style="width: 100%"
            header-cell-class-name="table_header"
            :cell-style="cellStyle"
        >
            <template v-for="(item, index) in tableHeaderTotal">
                <vxe-table-colgroup
                    align="center"
                    :field="item.prop"
                    :title="item.label"
                    :min-width="item.width"
                    :key="index"
                    :formatter="formFilter"
                >
                    <template v-for="(item, index) in item.children">
                        <vxe-table-colgroup
                            align="center"
                            :field="item.prop"
                            :title="item.label"
                            :min-width="item.width"
                            :key="index"
                        >
                            <template v-for="(item, index) in item.children">
                                <vxe-table-column
                                    align="center"
                                    :field="item.prop"
                                    :title="item.label"
                                    :min-width="item.width"
                                    :key="index"
                                ></vxe-table-column>
                            </template>
                        </vxe-table-colgroup>
                    </template>
                </vxe-table-colgroup>
            </template>
        </vxe-table>
        <el-pagination
            background
            align="right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[20, 30, 40, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            class="mt10"
        ></el-pagination>
    </div>
</template>

<script>
import { getRStatEventOrg } from '@/api/Statistical-analysis/transientIndicators/transientIndicators'
import Timeinterval from '@/views/components/Timeinterval.vue'
import Area from '@/views/components/Area/Area.vue'
import { dicData } from '@/assets/commjs/dictypeData'
export default {
    components: { Area, Timeinterval },
    data() {
        return {
            vh: undefined,
            date: '',
            input: '',
            height: null,
            item: ['', ''],
            form: {
                endTime: '',
                id: '',
                startTime: '',
                type: 0,
                monitorSort: []
            },
            monitorTagList: [],
            isLoading1: false,
            //暂态事件统计
            tableHeaderTotal: [
                { prop: 'orgName', label: '单位', width: 200 },
                { prop: 'monitorType', label: '监测对象标签', width: 150 },
                {
                    label: '有效接入监测点数',
                    children: [
                        // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                        {
                            prop: 'effectiveMeasurementAverage',
                            label: '日均',
                            width: 80
                        },
                        {
                            prop: 'effectiveMeasurementAccrued',
                            label: '累计',
                            width: 80
                        }
                    ]
                },
                {
                    label: '暂态指标监测点数',
                    children: [
                        // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                        {
                            prop: 'eventMeasurementAverage',
                            label: '日均',
                            width: 80
                        },
                        {
                            prop: 'eventMeasurementAccrued',
                            label: '累计',
                            width: 80
                        }
                    ]
                },
                {
                    label: '暂态指标监测点占比(%)',
                    children: [
                        // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                        {
                            prop: 'eventMeasurementRatioAverage',
                            label: '日均',
                            width: 100
                        },
                        {
                            prop: 'eventMeasurementRatioAccrued',
                            label: '累计',
                            width: 100
                        }
                    ]
                },
                {
                    label: '短时中断',
                    children: [
                        // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                        {
                            label: '监测点数',
                            children: [
                                // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                                {
                                    prop: 'dayShortInterruptions',
                                    label: '日均',
                                    width: 80
                                },
                                {
                                    prop: 'cumulativeShortInterruptions',
                                    label: '累计',
                                    width: 80
                                }
                            ]
                        },
                        {
                            label: '监测点占比(%)',
                            children: [
                                // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                                {
                                    prop: 'dayShortInterruptionsProportion',
                                    label: '日均',
                                    width: 80
                                },
                                {
                                    prop: 'cumulativeShortInterruptionsProportion',
                                    label: '累计',
                                    width: 80
                                }
                            ]
                        }
                        // { prop: "shortInterruptionsMonitor", label: "监测点数", },
                        // { prop: "shortInterruptionsProportion", label: "占比", },
                        // { prop: "shortInterruptionsCount", label: "次数", },
                        // { prop: "shortInterruptionsFrequency", label: "频次",}
                    ]
                },
                {
                    label: '电压暂升',
                    children: [
                        // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                        {
                            label: '监测点数',
                            children: [
                                // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                                {
                                    prop: 'dayVoltageRise',
                                    label: '日均',
                                    width: 80
                                },
                                {
                                    prop: 'cumulativeVoltageRise',
                                    label: '累计',
                                    width: 80
                                }
                            ]
                        },
                        {
                            label: '监测点占比(%)',
                            children: [
                                // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                                {
                                    prop: 'dayVoltageRiseProportion',
                                    label: '日均',
                                    width: 80
                                },
                                {
                                    prop: 'cumulativeVoltageRiseProportion',
                                    label: '累计',
                                    width: 80
                                }
                            ]
                        }
                        // { prop: "voltageRiseProportion", label: "占比", width: 120 },
                        // { prop: "voltageRiseCount", label: "次数", width: 120 },
                        // { prop: "voltageRiseFrequency", label: "频次", width: 120 }
                    ]
                },
                {
                    label: '电压暂降',
                    children: [
                        // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                        {
                            label: '监测点数',
                            children: [
                                // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                                {
                                    prop: 'dayVoltageDip',
                                    label: '日均',
                                    width: 80
                                },
                                {
                                    prop: 'cumulativeVoltageDip',
                                    label: '累计',
                                    width: 80
                                }
                            ]
                        },
                        {
                            label: '监测点占比(%)',
                            children: [
                                // { prop: "monthVoltage", label: "谐波电流超标监测点占比(%)" ,width:200},
                                {
                                    prop: 'dayVoltageDipProportion',
                                    label: '日均',
                                    width: 80
                                },
                                {
                                    prop: 'cumulativeVoltageDipProportion',
                                    label: '累计',
                                    width: 80
                                }
                            ]
                        }
                        // { prop: "voltageDipProportion", label: "占比", width: 120 },
                        // { prop: "voltageDipCount", label: "次数", width: 120 },
                        // { prop: "voltageDipFrequency", label: "频次", width: 120 }
                    ]
                }
            ],
            timeOptions: [
                { label: '年份', value: 1 },
                { label: '季度', value: 2 },
                { label: '月份', value: 3 }
            ],
            totalData: [],
            device: '',
            currentPage: 1, // 当前页码
            total: undefined, // 总条数
            pageSize: 20 // 每页的数据条数
        }
    },
    created() {
        this.form.id = JSON.parse(window.sessionStorage.getItem('Info')).deptId
    },
    mounted() {
        // this.item=this.$refs.interval.timeValue;
        this.device = window.devicePixelRatio
        this.monitorTagList = dicData('Monitoring_Labels', [])
        this.setHeight()
        window.addEventListener('resize', this.setHeight)
        this.onSubmit()
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.setHeight)
    },
    methods: {
        setHeight() {
            this.vh = window.sessionStorage.getItem('appheight') - 215
        },
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            // console.log(`每页 ${val} 条`);
            this.currentPage = 1
            this.pageSize = val
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            // console.log(`当前页: ${val}`);
            this.currentPage = val
        },
        transient() {},
        // 表格查询
        onSubmit() {
            this.isLoading1 = true
            this.form.type = this.$refs.interval.intervald
            this.item = this.$refs.interval.timeValue
            this.form.startTime = this.item[0]
            this.form.endTime = this.item[1]
            //方法调用获取暂态事件统计数据接口
            getRStatEventOrg(this.form).then(res => {
                if (res.data == []) {
                    this.total = 0
                } else {
                    this.isLoading1 = false
                    this.totalData = res.data
                    this.total = this.totalData.length
                }
            })
        },
        // 切换选项
        handleNodeClick(data) {
            // console.log(data);
            this.form.id = data.id
        },
        //判断数据
        cellStyle(row) {
            // console.log(row);
            // if(row.columnIndex==12){
            if (
                row.row.shortInterruptionsProportion == 3.14159 ||
                row.row.voltageRiseProportion == 3.14159 ||
                row.row.voltageDipProportion == 3.14159
            ) {
                row.row.shortInterruptionsProportion = '暂无数据'
                row.row.voltageRiseProportion = '暂无数据'
                row.row.voltageDipProportion = '暂无数据'
            }
            if (
                row.row.shortInterruptionsCount == null ||
                row.row.shortInterruptionsFrequency == null ||
                row.row.voltageRiseCount == null ||
                row.row.voltageRiseFrequency == null ||
                row.row.voltageDipCount == null ||
                row.row.voltageDipFrequency == null
            ) {
                row.row.shortInterruptionsCount = '/'
                row.row.shortInterruptionsFrequency = '/'
                row.row.voltageRiseCount = '/'
                row.row.voltageRiseFrequency = '/'
                row.row.voltageDipCount = '/'
                row.row.voltageDipFrequency = '/'
            }
            // }
        },
        //导出
        exportEvent() {
            this.$refs.Regionalclassification.exportData({
                filename: 'export', // 文件名字
                sheetName: 'Sheet1',
                type: 'xlsx', //导出文件类型 xlsx 和 csv
                useStyle: true,
                data: this.totalData // 数据源 // 过滤那个字段导出 // columnFilterMethod: function(column, $columnIndex) { //   return !(column.$columnIndex === 0 ) //   // 0是复选框 不导出 // }
            })
        },
        formFilter(row, column) {
            if (row.column.property == 'monitorType') {
                let title = ''
                this.monitorTagList.forEach(item => {
                    if (item.id == row.row.monitorType) {
                        title = item.name
                    }
                })
                return title
            } else {
                return row.row[row.column.property]
            }
        }
    }
}
</script>

<style lang="less" scoped>
@import url('../../../../styles/comStyle.less');
::v-deep .vxe-table .cell {
    text-align: center;
}
::v-deep .el-tabs--border-card > .el-tabs__content {
    padding: 10px;
}

::v-deep .el-row {
    width: 100%;
}
::v-deep .form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-form-item {
        display: flex;
        width: 48%;
        .el-form-item__content {
            flex: 1 !important;
            .el-select {
                width: 100%;
            }
            .el-input__inner {
                width: 100% !important;
            }
            .el-date-editor {
                width: 100%;
            }
        }
    }
}
</style>
