<template>
    <view class="consumption-conversion">
        <!-- 消费转化对比图标题 -->
        <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="consumptionConversionChart" :animation="false" :ontap="true" :ontouch="true"
                tooltipFormat="ConsumptionConversion" />
        </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: [],
                carResList: [],
                carRealResList: [],
                orderResList: [],
                orderRealResList: []
            }
        }
    },

    computed: {
        // 图表数据
        chartData() {
            return {
                categories: this.rawData.category,
                series: [
                    {
                        name: '车流量',
                        data: this.rawData.carResList
                    },
                    {
                        name: '客单量',
                        data: this.rawData.orderResList
                    }
                ]
            }
        },

        // 图表配置
        chartOpts() {
            return {
                padding: [15, 15, 15, 15],
                dataLabel: false,
                enableScroll: true,
                dataPointShape: true,
                xAxis: {
                    disableGrid: true,
                    itemCount: 8, // 增加显示数量，让刻度更紧凑
                },
                yAxis: {
                    data: [
                        {
                            title: '车流量(辆)',
                            titleFontSize: 12,
                            titleOffsetY: 0,
                            titleOffsetX: 0,
                            position: 'left'
                        },
                        {
                            title: '客单量(笔)',
                            titleFontSize: 12,
                            titleOffsetY: 0,
                            titleOffsetX: 0,
                            position: 'right'
                        }
                    ]
                },
                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.handleGetConsumptionConversionData()
    },

    methods: {
        // 获取消费转化对比数据
        async handleGetConsumptionConversionData() {
            const req = {
                Province_Code: '530000',
                Statistics_Date: moment().subtract(1, 'd').subtract(1, 'M').format('YYYY-MM-DD'),
                Serverpart_ID: "" // 暂时为空，如果需要传入服务区ID可以在这里添加
            }

            const data = await this.getTransactionConvert(req);

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

        // 发起API请求获取消费转化对比分析数据
        async getTransactionConvert(params) {
            const data = await request.$webGet(
                "CommercialApi/Revenue/GetTransactionConvert",
                params
            );
            return data || {}
        },

        // 处理图表数据
        processChartData(data) {
            let category = []
            let carResList = []
            let carRealResList = []
            let orderResList = []
            let orderRealResList = []

            // 处理车流数据
            if (data.BayonetList && data.BayonetList.data && data.BayonetList.data.length > 0) {
                let carList = data.BayonetList.data
                carList.forEach((item) => {
                    carResList.push(Number(item[1]))
                    carRealResList.push(item[1])
                })
            }

            // 处理客单量数据
            if (data.TransactionList && data.TransactionList.data && data.TransactionList.data.length > 0) {
                let orderList = data.TransactionList.data
                orderList.forEach((item) => {
                    category.push(`${item[0]}时`)
                    orderResList.push(Number(item[1]))
                    orderRealResList.push(item[1])
                })
            }

            // 更新图表数据
            this.rawData = {
                category: category,
                carResList: carResList,
                carRealResList: carRealResList,
                orderResList: orderResList,
                orderRealResList: orderRealResList
            }

        },


    }
}
</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-conversion {
    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>