<script setup lang="ts">
import SmallTitle from '../smallTitle/smallTitle.vue'
import './CoreCategory.less'
import { onMounted, onBeforeUnmount, ref } from 'vue';
import * as echarts from 'echarts/core';
import { PieChart } from 'echarts/charts';
import {
    GridComponent,
    TitleComponent,
    TooltipComponent,
    LegendComponent,
    GraphicComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import businessFormatCenterIcon from '../../../../assets/image/businessFormatCenterIcon.png'
import moment from 'moment';
import { handleGetBusinessTradeRevenue } from '../../service';

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

let myChart: echarts.ECharts;
// 自定义颜色列表
const colorList = ['#FF9500', '#00FFB7', '#0094FF', '#7D4CD2', '#69BCFF'];
// 图例数据
let lengedList = ref<any>([])

onMounted(async () => {

    const res: any = await handleGetData()

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

    myChart = echarts.init(chartDom);

    const option = {
        tooltip: {  // 新增 tooltip 配置
            trigger: 'item',  // 触发类型：坐标轴触发
            axisPointer: {    // 坐标轴指示器配置
                type: 'shadow' // 阴影指示器（适合柱状图）
            },
            formatter: function (params: any) {  // 自定义提示框内容
                return `
                    <div style="font-weight:bold">${params.data.name} ${params?.percent}%  ${res.realData[params.dataIndex]}</div>
                `;
            }
        },
        graphic: {  // 关键配置：在图表中心添加图片
            elements: [
                {
                    type: 'image',
                    style: {
                        image: businessFormatCenterIcon,
                        width: 83,
                        height: 83
                    },
                    left: 'center',
                    top: 'center',
                    z: 10
                }
            ]
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: ['85%', '100%'],
                center: ['50%', '50%'],  // 关键修改：强制居中 [水平位置, 垂直位置]
                avoidLabelOverlap: false,

                itemStyle: {
                    color: function (params: any) {
                        return colorList[params.dataIndex];
                    }
                },
                label: {
                    show: false
                },
                emphasis: false,
                labelLine: {
                    show: false
                },
                data: res.seriesData
            }
        ]
    };

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

// 业态结构占比
const handleGetData = async () => {

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

    const data = await handleGetBusinessTradeRevenue(req)

    console.log('业态结构占比', data)

    let list: any = data.BusinessTradeRank
    let seriesData: any = []
    let realData: any = []
    let category: any = []
    let lengedLists: any = []
    if (list && list.length > 0) {
        list.forEach((item: any) => {
            seriesData.push({ value: Number(item.value), name: item.name })
            realData.push(item.data)
            category.push(item.name)
            lengedLists.push({
                name: item.name,
                percent: Number(item.value),
                value: item.data
            })
        })
    }


    let res: any = {
        category: category,// x轴的内容
        seriesData: seriesData,// y轴的数据
        realData: realData// 真实数据
    }
    lengedList.value = lengedLists
    return res
}


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

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

</script>




<template>
    <div class="CoreCategoryBox">
        <SmallTitle :title="'核心品类占比'"></SmallTitle>

        <div class="CoreCategoryCharts">
            <!-- <div class="CoreCategoryUnit">万元</div> -->
            <div style="display: flex;margin-top: 30px;align-items: center;">
                <div class="CoreCategory" id="CoreCategory"></div>

                <div class="CoreCategoryDataList">
                    <div class="CoreCategoryDataBox" v-for="(item, index) in lengedList" :key="index">
                        <div class="CoreCategoryDataLeftBox">
                            <div class="CoreCategoryItem">
                                <div class="CoreCategoryItemLeged" :style="{ background: colorList[index] }"></div>
                                <div class="CoreCategoryItemLabel">{{ item.name }}</div>
                                <div class="CoreCategoryItemLabel" style="margin-left: 8px;">{{ item.percent }}%</div>
                                <!-- <div class="CoreCategoryItemLabel" style="margin-left: 8px;">{{ item.value }}</div> -->
                            </div>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>
</template>