<template>
    <view class="customer-age-group">
        <!-- 年龄标题 -->
        <view class="section-header">
            <text class="section-title">年龄</text>
        </view>

        <!-- 年龄图表 -->
        <view class="chart-container">
            <!-- 图表加载效果 -->
            <ChartLoading v-if="!hasChartData" text="数据加载中..." />
            <!-- 实际图表 -->
            <QiunDataCharts v-else type="column" :opts="chartOpts" :chartData="chartData" :canvas2d="true"
                :inScrollView="true" canvasId="customerAgeGroupChart" :animation="false" :ontap="true" :ontouch="true"
                tooltipFormat="CustomerAgeGroup" />
        </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 {
            // 图表原始数据
            rawData: {
                category: [],
                seriesDataMan: [],
                seriesDataWoman: []
            }
        }
    },
    props: {
        selectTime: {
            type: String,
            default: ""
        },
    },
    computed: {
        // 检查是否有图表数据
        hasChartData() {
            return this.rawData.category.length > 0 &&
                this.rawData.seriesDataMan.length > 0 &&
                this.rawData.seriesDataWoman.length > 0
        },

        // 图表数据
        chartData() {
            return {
                categories: this.rawData.category,
                series: [
                    {
                        name: '男性',
                        data: this.rawData.seriesDataMan
                    },
                    {
                        name: '女性',
                        data: this.rawData.seriesDataWoman
                    }
                ]
            }
        },

        // 图表配置
        chartOpts() {
            return {
                padding: [15, 15, 0, 5],
                dataLabel: false,
                enableScroll: true, // 开启滚动
                xAxis: {
                    disableGrid: true,
                    itemCount: 6, // 默认显示6个月的数据
                },
                yAxis: {
                    showTitle: true,
                    data: [{
                        min: 0,
                        title: '百分比(%)',
                        titleFontSize: 12,
                        titleOffsetY: -5,
                        titleOffsetX: 10,
                    }]
                },
                extra: {
                    column: {
                        type: 'group',
                        width: 12, // 柱子宽度调小一点
                        activeBgColor: '#000000',
                        activeBgOpacity: 0.08,
                        seriesGap: 0, // 同一组别的柱子紧贴在一起
                        barBorderRadius: [3, 3, 0, 0]
                    }
                }
            }
        }
    },

    onReady() {
        this.handleGetCustomerAgeData()
    },

    methods: {
        // 获取客群年龄数据
        async handleGetCustomerAgeData() {
            const req = {
                provinceCode: '530000',
                statisticsMonth: this.selectTime ? moment(moment(this.selectTime).subtract(1, 'M')).format('YYYYMM') : moment(moment().subtract(1, 'M')).format('YYYYMM'),
            }

            const data = await this.getCustomerAgeRatio(req);

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

        // 发起API请求获取客群年龄数据
        async getCustomerAgeRatio(params) {
            const data = await request.$webGet(
                "CommercialApi/Customer/GetCustomerAgeRatio",
                params
            );
            return data || []
        },

        // 处理图表数据
        processChartData(data) {
            let category = ["20岁", "30岁", "40岁", "50以上"]
            let seriesDataMan = []
            let seriesDataWoman = []

            // 处理数据：分别获取男性和女性数据
            if (data && data.length > 0) {
                data.forEach((item) => {
                    if (item.name === '男性') {
                        seriesDataMan = item.data || []
                    } else if (item.name === '女性') {
                        seriesDataWoman = item.data || []
                    }
                })
            }

            // 更新图表数据
            this.rawData = {
                category: category,
                seriesDataMan: seriesDataMan,
                seriesDataWoman: seriesDataWoman
            }
        },
    }
}
</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);

.customer-age-group {

    .section-header {
        display: flex;
        align-items: center;
        margin-bottom: 12rpx;

        .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>