<template>
    <div :id="chartId" style="width: 100%; height: 100%;"></div>
</template>

<script setup>
import * as echarts from "echarts";
import { ref, defineProps, onMounted, watch, nextTick } from "vue";
import { chartAdaptation } from "@/utils/tools";

const props = defineProps({
    barData: {
        type: Array,
        default: () => []
    },
    ids: {
        type: String,
        default: ''
    }
});

const chartId = ref(props.ids || `chart-${Math.random().toString(36).substr(2, 9)}`);
let myChart = null;

// 确保DOM渲染完成后再初始化图表
onMounted(() => {
    nextTick(() => {
        initializeChart();
    });
});

// 监听数据变化
watch(() => props.barData, (newData) => {
    if (myChart && newData && newData.length > 0) {
        updateChart();
    }
}, { deep: true, immediate: true });

const initializeChart = () => {
    const chartDom = document.getElementById(chartId.value);

    if (!chartDom) {
        console.error('图表容器未找到:', chartId.value);
        return;
    }

    try {
        myChart = echarts.init(chartDom);
        updateChart();

        // 自适应
        chartAdaptation(myChart);
        window.addEventListener('resize', () => {
            myChart && myChart.resize();
        });
    } catch (error) {
        console.error('图表初始化失败:', error);
    }
};

const updateChart = () => {
    if (!myChart || !props.barData || props.barData.length === 0) {
        console.log('图表或数据未准备好');
        return;
    }

    const option = {
        title: [
        ],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            // formatter: '{b}: {c}'
        },
        xAxis: {
            type: "category",
            data: props.barData[0],
        },
        yAxis: {
            type: "value",
            splitLine: {
                lineStyle: {
                    color: '#f0f0f0',
                    type: 'dashed'
                }
            }
        },
        legend: {
            show: true,
            top: "0",
        },
        series: [
            {
                name: "重大",
                data: props.barData[1],
                type: "bar",
                itemStyle: {
                    color: "#E80000",
                },
            },
            {
                name: "较大",
                data: props.barData[2],
                type: "bar",
                itemStyle: {
                    color: "#FF8040",
                },
            },
            {
                name: "一般",
                data: props.barData[3],
                type: "bar",
                itemStyle: {
                    color: "#E2D101",
                },
            },
            {
                name: "轻微",
                data: props.barData[4],
                type: "bar",
                itemStyle: {
                    color: "#0080FF",
                },
            },
        ],
    };
    myChart.setOption(option, true);
};
</script>

<style scoped>
/* 确保容器有基本样式 */
#chart-container {
    min-height: 200px;
}
</style>