<template>
    <div class="content" style="display: block">
        <el-form :inline="true">
            <el-form-item label="统计指标:">
                <el-select v-model="formData.statisticalId" filterable clearable placeholder="请选择">
                    <el-option v-for="item in zblist" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="值类型:">
                <el-select v-model="formData.valueType" filterable clearable placeholder="请选择">
                    <el-option v-for="item in typelist" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="xiebojibianlvSearch">查询</el-button>
            </el-form-item>
        </el-form>
        <el-descriptions border direction="vertical" :column="4">
            <el-descriptions-item label="最新数据时间">{{ new Date().toLocaleString() }}</el-descriptions-item>
            <el-descriptions-item label="设备温度">10</el-descriptions-item>
            <el-descriptions-item label="环境温度">10</el-descriptions-item>
            <el-descriptions-item label="湿度">10</el-descriptions-item>
        </el-descriptions>
        <div style="height: 10px"></div>
        <el-table
   stripe         :cell-style="{ textAlign: 'center' }"
            :header-cell-style="{ textAlign: 'center' }"
            style="width: 100%"
            :data="tableData"
            border
            header-cell-class-name="table_header"
        >
            <!-- <el-table-column type="phase" :index="indexMethod"></el-table-column> -->
            <el-table-column prop="positionName" label="位置"> </el-table-column>
            <el-table-column prop="statisticalData" label="值"> </el-table-column>
            <!-- <el-table-column prop="address" label="输出侧"> </el-table-column> -->
        </el-table>
        <div style="height: 20px"></div>
        <div id="ssjbl"></div>
    </div>
</template>

<script>
import * as echarts from 'echarts'
import { queryStatisticalSelect, queryFisrtCommonStatistical } from '@/api/app/steady'

export default {
    data() {
        return {
            tableData: [],
            zblist: [],
            typelist: [
                {
                    label: '平均值',
                    value: 'avg',
                },
                {
                    label: '最大值',
                    value: 'max',
                },
                {
                    label: '最小值',
                    value: 'min',
                },
                {
                    label: 'CP95值',
                    value: 'cp95',
                },
            ],
            formData: {
                devId: '',
                date: [],
                endTime: '',
                frequency: '',
                startTime: '',
                statisticalId: '',
                valueType: '',
            },
            linePosition: [],
        }
    },
    props: {
        queryByCodeId: {
            type: String,
            default: '',
        },
        devId: {
            type: String,
            default: '',
        },
    },
    created() {
        const dictypeData = JSON.parse(localStorage.getItem('dictypeData'))
        if (dictypeData) {
            dictypeData.forEach((item) => {
                let arr = [
                    {
                        code: 'Line_Position',
                        key: 'linePosition',
                    },
                ]
                arr.forEach((item2) => {
                    if (item.code == item2.code) {
                        this[item2.key] = item.children.map((item3) => {
                            return {
                                label: item3.name,
                                value: item3.id,
                                code: item3.code,
                            }
                        })
                    }
                })
            })
        }
        queryStatisticalSelect(this.queryByCodeId).then((res) => {
            if (res.code == 'A0000') {
                this.zblist = res.data.map((item) => {
                    return {
                        label: item.phase == 'T' || item.phase == 'M' ? '' : item.phase + '相' + item.showName,
                        value: item.id,
                        ...item,
                    }
                })
                this.formData.statisticalId = this.zblist[0].value
                this.formData.valueType = this.typelist[0].value
                this.xiebojibianlvSearch()
            }
        })
    },
    methods: {
        xiebojibianlvSearch() {
            console.log('谐波畸变率查询')
            this.formData.devId = this.devId
            queryFisrtCommonStatistical(this.formData).then((res) => {
                this.tableData = res.data.map((item) => {
                    item.positionName = this.linePosition.find((item2) => {
                        return item2.value == item.position
                    }).label
                    return {
                        ...item,
                    }
                })
                console.log(this.tableData)
                this.ssjbl()
            })
        },
        ssjbl() {
            let chartDom = document.getElementById('ssjbl')
            let myChart = echarts.init(chartDom)
            let option = {
                title: {
                    // text: '准实时' + this.zblist.find((item) => item.value == this.formData.statisticalId).label,
                    // subtext: 'Feature Sample: Gradient Color, Shadow, Click Zoom'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow',
                    },
                },
                // legend: {
                //     data: this.tableData.map((item) => item.positionName),
                // },
                xAxis: {
                    type: 'category',
                    data: this.tableData.map((item) => item.positionName),
                },
                yAxis: {
                    type: 'value',
                },
                series: [
                    {
                        // name: '负载侧',
                        data: this.tableData.map((item) => item.statisticalData),
                        type: 'bar',
                    },
                    // {
                    //     name: '电网侧',
                    //     data: [110],
                    //     type: 'bar',
                    // },
                    // {
                    //     name: '输出侧',
                    //     data: [120],
                    //     type: 'bar',
                    // },
                ],
            }
            option && myChart.setOption(option)
window.echartsArr.push(myChart)
        },
    },
}
</script>

<style lang="scss">
#ssjbl,
#实时谐波含量 {
    width: 100%;
    height: 300px;
}
</style>
