<script setup lang="ts">
import SmallTitle from '../smallTitle/smallTitle.vue'
import './ConsumptionConversion.less'
import * as echarts from 'echarts/core';
import { LineChart } from 'echarts/charts';
import {
    GridComponent,
    TitleComponent,
    TooltipComponent,
    LegendComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import { onBeforeUnmount, onMounted } from 'vue';
import { handleGetTransactionConvert } from '../../service';
import moment from 'moment';

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

let myChart: echarts.ECharts;

onMounted(async () => {
    const res: any = await handleGetData()

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

    myChart = echarts.init(chartDom);

    const option = {
        legend: {
            data: ['客单', '车流'],  // 对应 series 中的 name
            top: 0,              // 距离容器底部距离
            right: 0,
            itemWidth: 20,          // 图例标记的图形宽度
            itemHeight: 10,         // 图例标记的图形高度
            textStyle: {
                color: '#333'       // 图例文字颜色
            }
        },
        xAxis: {
            type: 'category',
            data: res.category,
            boundaryGap: true,
        },
        yAxis: [
            {
                type: 'value',
                name: '车流量',
                nameTextStyle: {
                    color: '#00F6FF' // Y轴名称颜色
                },
                splitLine: { show: false },
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: '#00F6FF'
                    }
                },
                axisLabel: {
                    color: '#00F6FF',
                    formatter: '{value}' // 添加单位
                }
            },
            {
                type: 'value',
                name: '客单量',
                nameTextStyle: {
                    color: '#008CFF' // Y轴名称颜色
                },
                splitLine: { show: false },
                position: 'right',
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: '#008CFF'
                    }
                },
                axisLabel: {
                    color: '#008CFF',
                    formatter: '{value}' // 添加单位
                },
                offset: 0 // 确保Y轴紧贴图表
            }
        ],
        grid: {
            left: '10',    // 增加左侧空间
            right: '10',   // 增加右侧空间
            bottom: '10',
            top: '10',
            containLabel: true
        },
        series: [
            {
                name: '车流量',
                data: res.carResList.map((value: any, index: any) => ({
                    value: value,
                    symbol: index % 3 === 0 ? 'circle' : 'none',
                    symbolSize: 6
                })),
                type: 'line',
                smooth: true,
                lineStyle: {
                    width: 2,
                    color: '#00F6FF'
                },
                itemStyle: {
                    color: '#00F6FF'
                },
                yAxisIndex: 0 // 绑定左侧Y轴
            },
            {
                name: '客单量',
                data: res.orderResList.map((value: any, index: any) => ({
                    value: value,
                    symbol: index % 3 === 0 ? 'circle' : 'none',
                    symbolSize: 6
                })),
                type: 'line',
                smooth: true,
                lineStyle: {
                    width: 2,
                    color: '#008CFF' // 改为橙色
                },
                itemStyle: {
                    color: '#008CFF' // 改为橙色
                },
                yAxisIndex: 1 // 绑定右侧Y轴
            }
        ],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'line' // 改为线条指示器
            },
            formatter: function (params: any) {
                console.log('paramsparamsparams', params);
                console.log('resresresres', res);
                let toolStr: string[] = []
                if (params && params.length > 0) {
                    params.forEach((item: any) => {
                        toolStr.push(
                            `${item.seriesName}：${item.value}元`
                        )
                    })
                }

                return `
                    <div>
                        <div>${res.category[params[0].dataIndex]}时</div>
                        ${toolStr.map((item: string) => {
                    return `<div>${item}</div>`
                })}
                    </div>
                `
            }
        }
    };

    myChart.setOption(option);
    myChart.resize();
    window.addEventListener('resize', resizeChart);
})

// 拿到数据
const handleGetData = async () => {

    let category: any = []
    let carResList: any = []
    let carRealResList: any = []
    let orderResList: any = []
    let orderRealResList: any = []

    const req: any = {
        Province_Code: '530000',
        Statistics_Date: moment().subtract(1, 'd').format('YYYY-MM-DD'),
        Serverpart_ID: ''
    }
    const data = await handleGetTransactionConvert(req)

    let carList = data.BayonetList.data

    let orderList = data.TransactionList.data

    if (carList && carList.length > 0) {
        carList.forEach((item: any) => {
            carResList.push((item[1]).toLocaleString())
            carRealResList.push(item[1])
        })
    }

    if (orderList && orderList.length > 0) {
        orderList.forEach((item: any) => {
            category.push(item[0])
            orderResList.push((item[1]).toLocaleString())
            orderRealResList.push(item[1])
        })
    }



    let res: any = {
        category: category,// x轴的内容
        carResList: carResList,
        carRealResList: carRealResList,
        orderResList: orderResList,
        orderRealResList: orderRealResList
    }
    return res
}


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

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



</script>

<template>
    <div class="ConsumptionConversionBox">
        <SmallTitle :title="'消费转化对比图'"></SmallTitle>

        <div class="ConsumptionConversion" id="ConsumptionConversion"></div>
    </div>
</template>
