<template>
    <div class="_consume">
        <div class="_consume_mains">
            <div ref="chartRef" style="width: 100%; height: 100%;"></div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, nextTick } from 'vue';
import * as echarts from 'echarts';
import { el } from 'element-plus/es/locales.mjs';
const props = defineProps({
    resultData: {
        type: Array,
        default: []
    },
    curveType: {
        type: String,
        default: 'temperature'
    }
})
const chartRef = ref(null);
let chart = null;

const generateRandomData = (length, max) => {
    const randomData = [];
    for (let i = 0; i < length; i++) {
        randomData.push(Math.floor(Math.random() * max));
    }
    return randomData;
};

const handleResize = () => {
    if (chart) {
        chart.resize();
    }
};

onMounted(() => {
    initChart();
});
const initChart = (dates, EPE, typeName, unitType) => {
    if (chartRef.value) {
        chart = echarts.init(chartRef.value);
        // 生成 24 小时的时间数据
        // const hours = Array.from({ length: 24 }, (_, i) => `${i}时`);
        const hours = []
        const option = {
            xAxis: {
                type: 'category',
                // 使用 24 小时数据
                // data: hours,
                axisLabel: {
                    color: '#fff'
                },
                data: dates,
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                },
                textStyle: {
                    color: '#fafafa',
                },
                borderColor: 'transparent',
                backgroundColor: 'rgba(0, 0, 0, 0.5)',
                extraCssText: 'backdrop-filter: blur(6px);',
                formatter: function (params) {
                    let result = params[0].axisValue + '<br/>';
                    params.forEach(item => {
                        result += item.marker + item.seriesName + '：' + item.data + `${unitType}<br/>`;
                    });
                    return result;
                }
            },
            grid: {
                top: '5%',
                bottom: '5%',
                right: '5%',
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    color: '#fff'
                },
                splitLine: {
                    show: false,
                    lineStyle: {
                        color: '#44585e'
                    }
                },
                axisTick: {
                    show: false
                },
            },
            series: [
                {
                    name: typeName,
                    type: 'line',
                    // 生成 24 个随机数据
                    // data: generateRandomData(7, 50),
                    data: EPE,
                    lineStyle: {
                        color: 'rgb(49, 143, 247)',
                    },
                    showSymbol: false,
                    smooth: true,
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(
                            0,
                            0,
                            0,
                            1,
                            [{
                                offset: 0,
                                color: 'rgba(49, 143, 247, .6)',
                            },
                            {
                                offset: 0.8,
                                color: 'rgba(49, 143, 247, .2)',
                            },
                            ],
                            false
                        ),
                        shadowColor: 'rgba(0, 0, 0, 0.1)',
                        shadowBlur: 10,
                    }
                }
            ]
        };
        chart.setOption(option);
    }
}
const getChartData = (electricityCount) => {
    const dates = [];
    const EPE = [];
    const EPE_forecast = [];
    electricityCount.forEach((item) => {
        dates.push(item.collect_time);
        EPE.push(item[props.curveType]);
    });
    dates.reverse()
    EPE.reverse()
    return { dates, EPE, EPE_forecast };
};
watch(() => props.resultData, (newVal) => {
    let typeName = ''
    let unitType = ''
    if (props.curveType === 'temperature') {
        typeName = '温度'
        unitType = '℃'
    } else if (props.curveType === 'humidity') {
        typeName = '湿度'
        unitType = '%RH'
    } else if (props.curveType === 'pressure') {
        typeName = '压力'
        unitType = 'MPa'
    } else if (props.curveType === 'total_flow') {
        typeName = '流量'
        unitType = 'm³'
    }
    const { dates, EPE, EPE_forecast } = getChartData(newVal);
    if (newVal && chart == null) {
        nextTick(() => {
            initChart(dates, EPE, typeName, unitType);
        });
    }
    if (chart) {
        chart.setOption({
            xAxis: {
                data: dates,
            },
            tooltip: {
                formatter: function (params) {
                    let result = params[0].axisValue + '<br/>';
                    params.forEach(item => {
                        result += item.marker + item.seriesName + '：' + item.data + `${unitType} <br/>`;
                    });
                    return result;
                }
            },
            series: [{
                name: typeName,
                type: 'line',
                data: EPE,
            }],
        })
    }
}, { deep: true, immediate: true } // 开启深度监听
)
onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
    if (chart) {
        chart.dispose();
    }
});
</script>

<style lang="scss">
._divider {
    height: 1px;
    border: 1px dashed #168cdb;
    flex: 1;
    margin: 0 10px;
}

._consume {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;

    &_mains {
        margin: 10px 10px;
        flex: 1;
        display: flex;
        background-color: #061907;

        &_item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px;

            &_name {
                width: 30px;
                height: 30px;
                background: url("@/assets/images/content_circle_num.png");
                background-size: 100% 100%;
                background-position: center;
                background-repeat: no-repeat;
                // animation: scanning 4s linear infinite;
                border: 1px solid red;
            }

            &_flag {
                display: flex;
                align-items: center;
                gap: 3px;

                &_item {
                    width: 30px;
                    height: 30px;
                    border: 3px dashed #168cdb;
                    box-sizing: border-box;
                    background: #0e6ead;
                    color: #fff;
                    border-radius: 50%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-size: 14px;
                }
            }
        }

        &_item:hover {
            cursor: pointer;
            background-image: linear-gradient(to right, #168cdb, transparent);
        }
    }
}
</style>