<script setup lang="ts">
import SmallTitle from '../smallTitle/smallTitle.vue'
import './BrandConsumptionLevel.less'
import * as echarts from 'echarts/core';
import { LineChart } from 'echarts/charts';
import {
    GridComponent,
    TitleComponent,
    TooltipComponent,
    LegendComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import { onBeforeUnmount, onMounted } from 'vue';
import { handleGetBusinessBrandLevel } from '../../service';
import moment from 'moment';

// 注册组件
echarts.use([
    LineChart,
    GridComponent,
    TitleComponent,
    TooltipComponent,
    LegendComponent,
    CanvasRenderer
]);

let myChart: echarts.ECharts;

onMounted(async () => {
    const res: any = await handleGetData()

    const chartDom = document.getElementById('BrandConsumptionLevel');
    if (!chartDom) return;

    myChart = echarts.init(chartDom);

    const option = {
        legend: {
            // selectedMode: false,
            right: 0,
            textStyle: {
                color: '#fff',
                fontSize: 12,
                padding: [0, 0, 0, 5]
            },
        },
        grid: {
            left: '0',
            right: '0',
            bottom: '0',
            top: '30',
            containLabel: true
        },
        yAxis: {
            name: '%',
            type: 'value',
            splitLine: { show: false },  // 隐藏刻度线
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#fff'
                }
            },
            axisLabel: {
                color: '#fff',
            }
        },
        xAxis: {
            type: 'category',
            data: res.category,
            axisLabel: {
                interval: 0  // 强制显示所有标签
            },
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#fff'
                }
            },
        },
        series: res.seriesData,
        tooltip: {
            trigger: 'item',
            formatter: function (params: any) {
                return `
                <div style="font-weight:bold">${params.seriesName}  ${params.value}%</div>
            `;
            }
        },
    };

    myChart.setOption(option);
    myChart.resize();
    window.addEventListener('resize', resizeChart);
})

// 拿到数据
const handleGetData = async () => {

    let category: any = []
    let seriesData: any = []

    const req: any = {
        ProvinceCode: '530000',
        StatisticsDate: moment().subtract(1, 'd').format('YYYY-MM-DD'),
        // ServerpartId: ''
    }

    let BrandConsumptionLevel = sessionStorage.getItem('BrandConsumptionLevel')
    let data: any = []
    if (BrandConsumptionLevel) {
        data = JSON.parse(BrandConsumptionLevel)
    } else {
        data = await handleGetBusinessBrandLevel(req)
        sessionStorage.setItem("BrandConsumptionLevel", JSON.stringify(data))
    }


    // const data = await handleGetBusinessBrandLevel(req)


    category = data.legend
    let list = data.ColumnList

    if (list && list.length > 0) {
        list.forEach((item: any) => {
            seriesData.push({
                name: item.name,
                type: 'bar',
                stack: 'Ad',
                barWidth: 6,
                emphasis: {
                    focus: 'series'
                },
                itemStyle: {
                    color: item.name === '低消费' ? '#7D4CD2' : item.name === '普通消费' ? '#00FFB7' : item.name === '高消费' ? '#FF307C' : '' // 直接指定颜色值
                },
                data: item.data
            })
        })
    }


    let res: any = {
        category: category,// x轴的内容
        seriesData: seriesData,
    }


    let BrandConsumptionLevelAI = sessionStorage.getItem('BrandConsumptionLevelAI')
    if (BrandConsumptionLevelAI) { } else {
        let aiObj: any = {}

        for (let i = 0; i < res.category.length; i++) {
            aiObj[res.category[i]] = `低消费占比${list[0].data[i]},普通消费占比${list[1].data[i]},高消费占比${list[2].data[i]},`
        }


        sessionStorage.setItem("BrandConsumptionLevelAI", JSON.stringify(aiObj))
    }

    return res
}


const resizeChart = () => {
    myChart?.resize();
};

onBeforeUnmount(() => {
    window.removeEventListener('resize', resizeChart);
    myChart?.dispose();
});



</script>

<template>
    <div class="BrandConsumptionLevelBox">
        <SmallTitle :title="'品牌消费水平'"></SmallTitle>

        <div class="BrandConsumptionLevel" id="BrandConsumptionLevel"></div>
    </div>
</template>
