<template>
    <div class="_consume">
        <HeadlineTag value="业主投诉量趋势"></HeadlineTag>
        <div class="_consume_mains">
            <div ref="chartRef" style="width: 100%; height: 100%;"></div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import * as echarts from 'echarts';
import HeadlineTag from '@/components/HeadlineTag';
const props = defineProps({
    resultData: {
        type: Object,
        default: {}
    }
})

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(() => {
    if (chartRef.value) {
        chart = echarts.init(chartRef.value);
        // 生成一周的日期数据
        const weekDays = [];
        const option = {
            xAxis: {
                type: 'category',
                data: weekDays,
                axisLabel: {
                    color: '#fff'
                },
            },
            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);',
            },
            grid: {
                top: '10%',
                bottom: '10%',
                right: '0%',
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    show: true,
                    color: '#fff'
                },
                splitLine: {
                    show: false,
                    lineStyle: {
                        color: '#44585e'
                    }
                },
                axisTick: {
                    show: false
                },
            },
            series: [
                // 柱状图表示运行正常
                {
                    name: '卫生问题',
                    type: 'bar',
                    // data: generateRandomData(7, 50),
                    data: [],
                    barWidth: '30%',
                    itemStyle: {
                        color: {
                            type: 'linear',
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            colorStops: [{
                                offset: 0, color: '#78befe' // 0% 处的颜色
                            }, {
                                offset: 1, color: '#0352fb' // 100% 处的颜色
                            }],
                            global: false // 缺省为 false
                        },
                        borderRadius: [10, 10, 10,10]
                    },
                },
                // 曲线表示运行异常
                {
                    name: '安全问题',
                    type: 'line',
                    // data: generateRandomData(7, 50),
                    data: [],
                    lineStyle: {
                        color: '#3b90d7',
                    },
                    showSymbol: false,
                    smooth: true,
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(
                            0,
                            0,
                            0,
                            1,
                            [{
                                offset: 0,
                                color: 'rgba(59,144,215, .6)',
                            },
                            {
                                offset: 0.8,
                                color: 'rgba(59,144,215, .2)',
                            },
                            ],
                            false
                        ),
                        shadowColor: 'rgba(0, 0, 0, 0.1)',
                        shadowBlur: 10,
                    },
                },
                {
                    name: '设施设备问题',
                    type: 'line',
                    // data: generateRandomData(7, 50),
                    data: [],
                    lineStyle: {
                        color: '#ffc107',
                    },
                    showSymbol: false,
                    smooth: true,
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(
                            0,
                            0,
                            0,
                            1,
                            [{
                                offset: 0,
                                color: 'rgba(255, 193, 7, .6)',
                            },
                            {
                                offset: 0.8,
                                color: 'rgba(255, 193, 7, .2)',
                            },
                            ],
                            false
                        ),
                        shadowColor: 'rgba(0, 0, 0, 0.1)',
                        shadowBlur: 10,
                    },
                }
            ]
        };

        chart.setOption(option);
    }
    window.addEventListener('resize', handleResize);
});

watch(() => props.resultData, (newVal) => {
    if (chart) {
        let arrData = Object.keys(newVal)
        let arr1 = []
        let arr2 = []
        let arr3 = []
        arrData.forEach((item)=>{
            let data = Object.entries(newVal[item])
            arr1.push(data[0][1])
            arr2.push(data[1][1])
            arr3.push(data[2][1])
        })
        chart.setOption({
            xAxis: {
                data: Object.keys(newVal),
            },
            series: [{
                name: '卫生问题',
                data: arr1,
            },{
                name: '安全问题',
                data: arr2,
            },{
                name: '设施设备问题',
                data: arr3,
            }],
        })
    }
}, { 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 {
    display: flex;
    flex-direction: column;

    &_mains {
        margin: 10px 30px;
        flex: 1;
        display: flex;

        &_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>