<template>
    <div class="chart-box">
        <div class="title-top">
            <span>toC/toB年销售订单趋势</span>
            <el-select v-model="dateValue" size="mini" class="select" placeholder="请选择" @change="dateChange">
                <el-option v-for="item in options()" :key="item.value" :label="item.label"
                    :value="item.value"></el-option>
            </el-select>
        </div>
        <div class="chart" :id="id"></div>
    </div>

</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from 'vue-property-decorator'
import * as echarts from 'echarts';
type EChartsOption = echarts.EChartsOption;

@Component
export default class OrderSaleByYear extends Vue {
    id = this.randomString();
    dateValue = new Date().getFullYear()
    mounted() {
        this.getData()
    }
    options() {
        let arr: Array<any> = []
        //从2010年到当前年
        for (let i = 2010; i <= new Date().getFullYear(); i++) {
            arr.push({
                value: i,
                label: i + '年'
            })
        }
        return arr
    }
    randomString() {
        const str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        let result = '';
        for (let i = 10; i > 0; --i)
            result += str[Math.floor(Math.random() * str.length)];
        return result;
    }
    dateChange() {
        this.getData()
    }
    initChart(list: any) {
        if (!list) return
        let chartDom: any = document.getElementById(this.id);
        let myChart = echarts.init(chartDom);
        let option: EChartsOption;
        let dataX: Array<any> = this.getDaysList()
        let dataY: Array<any> = [];
        let obj1: any = {
            name: 'B2B',
            type: 'line',
            smooth: true,
            data: []
        }
        for (const arr of dataX) {
            let isAdd = false
            for (const item2 of list.businessOrderSaleList) {
                if (arr == item2.businessTime) {
                    obj1.data.push(item2.businessTotal)
                    isAdd = true
                    break
                }
            }
            if (!isAdd) {
                obj1.data.push(0)
            }
        }
        dataY.push(obj1)

        let obj2: any = {
            name: 'B2C',
            type: 'line',
            smooth: true,
            data: []
        }
        for (const arr of dataX) {
            let isAdd = false
            for (const item2 of list.consumerOrderSaleList) {
                if (arr == item2.consumerTime) {
                    obj2.data.push(item2.consumerTotal)
                    isAdd = true
                    break
                }
            }
            if (!isAdd) {
                obj2.data.push(0)
            }
        }
        dataY.push(obj2)
        option = {
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                show: true,
                top: 0
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    // saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: dataX
            },
            yAxis: {
                type: 'value'
            },
            series: dataY
        };

        option && myChart.setOption(option);
    }
    getDaysList() {
        //返回当前年的所有月份，如2025-01，2025-02，2025-03，2025-04，2025-05，2025-06，2025-07，2025-08，2025-09，2025-10，2025-11，2025-12
        let data: Array<any> = []
        for (let i = 1; i <= 12; i++) {
            data.push(this.dateValue + '-' + (i < 10 ? '0' + i : i))
        }
        return data
    }
    getData() {
        (this as any).$request({
            url: '/omsOrder/orderReport/orderSaleByYear',
            method: 'GET',
            params: {
                year: this.dateValue
            }
        }).then((res: any) => {
            if (res.data) {
                this.initChart(res.data)
            }
        }).catch(() => { })
    }
}
</script>

<style lang="scss" scoped>
.chart-box {
    height: 100%;
    width: 100%;

    .title-top {
        height: 40px;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
        width: 100%;

        >span {
            font-size: 16px;
            font-weight: 700;
        }
    }
}

.chart {
    height: calc(100% - 40px);
    width: 100%;
}
</style>
