<template>
    <view class="gender-customer-group">
        <!-- 性别标题 -->
        <view class="section-header">
            <text class="section-title">性别</text>
        </view>

        <!-- 性别图表 -->
        <view class="chart-container">
            <!-- 图表加载效果 -->
            <ChartLoading v-if="isLoading" text="数据加载中..." />
            <!-- 实际图表 -->
            <QiunDataCharts v-else type="ring" :opts="chartOpts" :chartData="chartData" :canvas2d="true"
                :inScrollView="true" canvasId="genderCustomerGroupChart" :animation="false" :ontap="true"
                :ontouch="true" tooltipFormat="GenderCustomerGroup" />
        </view>
    </view>
</template>

<script>
import QiunDataCharts from './qiun-data-charts/components/qiun-data-charts/qiun-data-charts.vue'
import ChartLoading from './ChartLoading.vue'
import request from "@/util/index.js";
import moment from 'moment'

export default {
    components: {
        QiunDataCharts,
        ChartLoading
    },

    data() {
        return {
            isLoading: false,
            // 图表原始数据
            rawData: {
                seriesData: [],
                legendData: []
            }
        }
    },
    props: {
        selectTime: {
            type: String,
            default: ""
        },
    },
    computed: {
        // 检查是否有图表数据
        hasChartData() {
            return this.rawData.seriesData && this.rawData.seriesData.length > 0
        },

        // 图表数据
        chartData() {
            return {
                series: [{
                    data: this.rawData.seriesData
                }]
            }
        },

        // 图表配置
        chartOpts() {
            return {
                padding: [15, 15, 0, 15],
                dataLabel: false,
                title: {
                    name: '', // 清空标题文字
                    show: false // 隐藏标题
                },
                subtitle: {
                    name: '', // 清空副标题文字
                    show: false // 隐藏副标题
                },
                extra: {
                    ring: {
                        ringWidth: 15, // 减小圆环宽度，让圆环更细
                        activeOpacity: 0.5,
                        activeRadius: 10,
                        offsetAngle: 0,
                        labelWidth: 0,
                        border: false,
                        borderWidth: 2,
                        borderColor: '#FFFFFF',
                        linearType: 'custom'
                    }
                },
                legend: {
                    show: true,
                    position: 'bottom',
                    float: 'center',
                    backgroundColor: 'rgba(0,0,0,0)',
                    borderColor: 'rgba(0,0,0,0)',
                    fontSize: 12,
                    fontColor: '#333333',
                    margin: 0,
                    padding: 0,
                    itemGap: 10,
                    textAlign: 'left'
                },
            }
        }
    },

    watch: {
        selectTime: {
            handler(newVal, oldVal) {
                if (newVal !== oldVal) {
                    this.handleGetGenderData()
                }
            },
            immediate: false
        }
    },

    onReady() {
        this.handleGetGenderData()
    },

    methods: {
        // 获取性别数据
        async handleGetGenderData() {
            const req = {
                statisticsType: 1,
                provinceCode: '530000',
                statisticsMonth: this.selectTime ? moment(moment(this.selectTime).subtract(1, 'M')).format('YYYYMM') : moment(moment().subtract(1, 'M')).format('YYYYMM'),
            }
            this.isLoading = true

            const data = await this.getCustomerRatio(req);
            this.isLoading = false

            // 处理数据
            this.processChartData(data.Result_Data.List)
        },

        // 发起API请求获取性别数据
        async getCustomerRatio(params) {
            const data = await request.$webGet(
                "CommercialApi/Customer/GetCustomerRatio",
                params
            );
            return data || []
        },

        // 处理图表数据
        processChartData(data) {
            let seriesData = []
            let legendData = []

            // 处理数据：获取男性和女性的百分比数据
            if (data && data.length > 0) {
                data.forEach((item) => {
                    if (item.name === '男性' || item.name === '女性') {
                        seriesData.push({
                            name: `${item.name} ${item.data[0]}%`,
                            value: item.data[0]
                        })
                        legendData.push({
                            name: `${item.name} ${item.data[0]}%`
                        })
                    }
                })
            }

            // 更新图表数据
            this.rawData = {
                seriesData: seriesData,
                legendData: legendData
            }

        },

    }
}
</script>

<style scoped lang="less">
@primary-color: #46B8F3;
@secondary-color: #3CD495;
@danger-color: #FF5E5E;
@success-color: #52C41A;
@text-primary: #333;
@text-secondary: #666;
@text-light: #999;
@bg-white: #ffffff;
@border-radius: 16rpx;
@shadow: 0 4px 20px rgba(0, 0, 0, 0.08);

.gender-customer-group {
    margin-top: 24rpx;

    .section-header {
        display: flex;
        align-items: center;
        margin-bottom: 20rpx;
        padding: 0 8rpx;

        .section-title {
            font-size: 30rpx;
            font-weight: 600;
            color: @text-primary;
        }
    }

    .chart-container {
        background: @bg-white;
        border-radius: @border-radius;
        padding: 24rpx;
        box-shadow: @shadow;
        margin-bottom: 24rpx;
        width: 100%;
        box-sizing: border-box;
        height: 400rpx;
    }
}
</style>