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

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

let myChart: echarts.ECharts;

onMounted(async () => {

    const res: any = await handleGetSectionFlowCount()

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


    myChart = echarts.init(chartDom);

    const option = {
        tooltip: {
            trigger: 'item',
            backgroundColor: '#fff',  // 白色背景
            borderColor: '#fff',     // 白色边框
            textStyle: {
                color: '#333',       // 文字颜色
                fontSize: 14
            },
            formatter: function (params: any) {
                return `
                <div style="font-weight:bold">${params.name}  ${params.value}%</div>
            `;
            }
        },
        graphic: {  // 关键配置：在图表中心添加图片
            elements: [
                {
                    type: 'image',
                    style: {
                        image: GenderCenter,
                        width: 43,
                        height: 43
                    },
                    left: 'center',
                    top: 'center',
                    z: 10
                }
            ]
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: ['80%', '100%'],
                center: ['50%', '50%'],  // 关键修改：强制居中 [水平位置, 垂直位置]
                avoidLabelOverlap: false,

                itemStyle: {
                    color: function (params: any) {
                        // 自定义颜色列表
                        const colorList = ['#0094FF', '#FF5E5E'];
                        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 handleGetSectionFlowCount = async () => {
    const req: any = {
        statisticsType: 1,
        provinceCode: '530000',
        statisticsMonth: moment(moment().subtract(1, 'M')).format('YYYYMM'),
    }

    const data = await handleGetCustomerRatio(req)

    let seriesData: any = []
    let realData: any = []

    if (data && data.length > 0) {
        data.forEach((item: any) => {
            if (item.name === '男性' || item.name === '女性') {
                seriesData.push({ name: item.name, value: item.data[0] })
            }
        })
    }

    let res: any = {
        seriesData: seriesData,// y轴的数据
        realData: realData// 真实数据
    }
    return res
}


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

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

</script>




<template>
    <div class="GenderCustomerGroupBox">
        <SmallTitle :title="'性别'" :pageType="'center'"></SmallTitle>

        <div class="GenderCustomerGroupCharts">
            <!-- <div class="GenderCustomerGroupUnit">万元</div> -->
            <div class="GenderCustomerGroupContent">
                <div class="GenderCustomerGroup" id="GenderCustomerGroup"></div>


                <div class="legendBox">
                    <div class="legendBoxContent">
                        <div class="legendItem">
                            <div class="legendIcon" style="background-color: #0094FF;"></div>
                            <div class="legendName">男性</div>
                        </div>
                        <div class="legendItem">
                            <div class="legendIcon" style="background-color: #FF5E5E;"></div>
                            <div class="legendName">女性</div>
                        </div>
                    </div>
                </div>
            </div>

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