<template>
    <div>
        <SidePanelItem title="生态流量达标情况" :clickprompt="obj">
            <div class="body_topOne">
                <a-radio-group v-model:value="mode">
                    <a-radio-button value="top">按基地</a-radio-button>
                    <a-radio-button value="left">按调节性能</a-radio-button>
                </a-radio-group>
            </div>
            <a-spin :spinning="spinning">
                <div ref="chartRef" class="chart-container"></div>
            </a-spin>
        </SidePanelItem>
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue';
import SidePanelItem from '@/components/SidePanelItem/index.vue';
import * as echarts from 'echarts';


// 定义组件名（便于调试和递归）
defineOptions({
    name: 'shengtaidabiaoMod'
});
const obj =
{
    show: true,
    value: '1、统计电站范围：接入过生态流量数据的电站，2、当来水不足时，生态流量不小于入库流量判定为达标，3、"≥95% 座数"表示统计电站范围内时段达标率大于等于 95% 的电站数量',
}
const mode = ref('top');
const spinning = ref(false)
const chartRef = ref<HTMLElement | null>(null);
let chartInstance: echarts.ECharts | null = null;

// 模拟数据
const categoryData = [
    '其他', '闽浙赣', '澜沧江干流', '东北', '南盘江·红水河',
    '黄河中游干流', '黄河上游干流', '湘西', '长江上游干流',
    '乌江干流', '大渡河干流', '雅砻江干流', '金沙江干流'
];
const currentData = Array(13).fill(0).map(() => Math.random() * 5 + 86);
const lastYearData = Array(13).fill(0).map(() => Math.random() * 5 + 86);

// 初始化图表
const initChart = () => {
    if (!chartRef.value) return;
    
    // 如果实例存在，先销毁
    if (chartInstance) {
        chartInstance.dispose();
    }
    
    chartInstance = echarts.init(chartRef.value);
    
    const option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: function(params: any) {
                let result = params[0].name + '<br/>';
                params.forEach((param: any) => {
                    const percentage = param.value.toFixed(2) + '%';
                    result += param.marker + param.seriesName + '&nbsp;&nbsp;<b>' + percentage + '</b><br/>';
                });
                return result;
            }
        },
        legend: {
            data: ['当前', '去年同期'],
            top: 0,
            left: 'center',
            itemWidth: 20,
            itemHeight: 10,
            textStyle: {
                color: '#666'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            top: '50',
            containLabel: true
        },
        dataZoom: [
            {
                type: 'inside',
                xAxisIndex: 0,
                filterMode: 'empty',
                zoomOnMouseWheel: true,
                moveOnMouseMove: false,
                moveOnMouseWheel: true,
                start: 0,
                end: 100,
                minValueSpan: 0,
                maxValueSpan: 20
            }
        ],
        xAxis: {
            type: 'value',
            min: 80,
            max: 100,
            splitLine: {
                show: true,
                lineStyle: {
                    color: '#E8E8E8',
                    type: 'solid'
                }
            },
            axisLabel: {
                color: '#666',
                formatter: '{value}'
            }
        },
        yAxis: {
            type: 'category',
            data: categoryData,
            inverse: true,
            axisLabel: {
                color: '#666',
                fontSize: 12,
                interval: 0,
                rotate: 0,
                margin: 10
            },
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#666'
                }
            },
            axisTick: {
                show: true,
                lineStyle: {
                    color: '#666'
                }
            },
            splitLine: {
                show: false
            }
        },
        series: [
            {
                name: '当前',
                type: 'bar',
                data: currentData,
                itemStyle: {
                    color: '#5470C6'
                },
                barWidth: 12,
                barGap: '30%'
            },
            {
                name: '去年同期',
                type: 'bar',
                data: lastYearData,
                itemStyle: {
                    color: '#91CC75'
                },
                barWidth: 12,
                barGap: '30%'
            }
        ]
    };
    
    chartInstance.setOption(option);
};

// 页面加载时执行的逻辑
onMounted(() => {
    initChart();

    // 监听窗口大小变化
    window.addEventListener('resize', () => {
        chartInstance?.resize();
    });
});

// 监听 mode 变化
watch(mode, () => {
    // 这里可以根据 mode 的值重新加载数据
    initChart();
});
</script>

<style lang="scss" scoped>
.ant-radio-group {
    // border: 3px solid #2f6b98 !important;
    // border-radius: 10px !important;

    .ant-radio-button-wrapper-checked {
        border: 1px solid #2f6b98 !important;
        background-color: #2f6b98 !important;
        color: #fff !important;
    }

    .ant-radio-button-wrapper {
        border: 2px solid #2f6b98 !important;
    }

    .ant-radio-button-wrapper-checked :before {
        background-color: #2f6b98 !important;
    }
}

.chart-container {
    width: 100%;
    height: 500px;
    padding: 10px;
}
</style>
