<template>
    <view class="consumption-period">
        <!-- 消费时段分析标题 -->
        <view class="section-header">
            <text class="section-title">消费时段分析</text>
        </view>

        <!-- 消费时段分析图表 -->
        <view class="chart-container">
            <QiunDataCharts type="line" :opts="chartOpts" :chartData="chartData" :canvas2d="true" :inScrollView="true"
                canvasId="consumptionPeriodChart" :animation="false" :ontap="true" :ontouch="true"
                tooltipFormat="ConsumptionPeriod" />
        </view>
    </view>
</template>

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

export default {
    components: {
        QiunDataCharts
    },

    data() {
        return {
            // 图表原始数据
            rawData: {
                category: [],
                seriesData: []
            }
        }
    },

    computed: {
        // 图表数据
        chartData() {
            return {
                categories: this.rawData.category,
                series: [
                    {
                        name: '客单占比',
                        data: this.rawData.seriesData
                    }
                ]
            }
        },

        // 图表配置
        chartOpts() {
            return {
                padding: [15, 15, 15, 15],
                dataLabel: false,
                enableScroll: true,
                dataPointShape: true,
                xAxis: {
                    disableGrid: true,
                    itemCount: 6,
                },
                yAxis: {
                    data: [{
                        min: 0,
                        title: '客单占比(%)',
                        titleFontSize: 12,
                        titleOffsetY: 0,
                        titleOffsetX: 0
                    }]
                },
                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'
                },
                extra: {
                    line: {
                        type: 'curve',
                        width: 2,
                        activeType: 'hollow'
                    },
                    tooltip: {
                        showBox: true,
                        bgColor: '#000000',
                        bgOpacity: 0.7,
                        borderColor: '#333333',
                        borderWidth: 1
                    }
                }
            }
        }
    },

    onReady() {
        this.handleGetConsumptionPeriodData()
    },

    methods: {
        // 获取消费时段分析数据
        async handleGetConsumptionPeriodData() {
            const req = {
                Province_Code: '530000',
                Statistics_Date: moment().subtract(1, 'd').subtract(1, 'M').format('YYYY-MM-DD'),
                Serverpart_ID: '',
                TimeSpan: 1
            }

            const data = await this.getTransactionTimeAnalysis(req);

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

        // 发起API请求获取消费时段分析数据
        async getTransactionTimeAnalysis(params) {
            const data = await request.$webGet(
                "CommercialApi/Revenue/GetTransactionTimeAnalysis",
                params
            );
            return data || []
        },

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

            // 处理数据：获取消费时段分析数据
            if (data && data.length > 0) {
                // 拿到笔数的合计
                let orderSum = 0
                data.forEach((item) => {
                    orderSum += Number(item.data)
                })

                // data 是笔数  key 是金额
                data.forEach((item, index) => {
                    if (index < 10) {
                        category.push(`0${index}:00`)
                    } else {
                        category.push(`${index}:00`)
                    }

                    seriesData.push(Number(((item.data / orderSum) * 100).toFixed(2)))
                })
            }

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

    }
}
</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);

.consumption-period {
    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>