const _Http = getApp().globalData.http;
import * as echarts from '../../ec-canvas/echarts';
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        idname: {
            type: [String, Number]
        }
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.setData({
                "content.dataid": wx.getStorageSync('userMsg').userid,
                "content.username": wx.getStorageSync('userMsg').name,
            })
        }
    },
    data: {
        year: new Date().getFullYear().toString(),
        "content": {
            enddate: new Date().toISOString().split('T')[0],
            where: {}
        }
    },
    methods: {
        async getList(init = false) {
            let content = this.data.content
            const {
                dataid,
                type,
                username,
                isleave
            } = getCurrentPages()[getCurrentPages().length - 1].data;
            if (content.dataid != dataid || content.type != type || isleave != isleave) init = true
            content.dataid = dataid;
            content.type = type;
            content.username = username;
            content.where.isleave = isleave;

            _Http.basic({
                "id": this.data.idname,
                content
            }).then(res => {
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.initChart(res.data)
            })
        },
        initChart(data) {
            console.log("data", data)
            const colors = ['#6CD2A1', '#5F9DFC', '#ECB937', '#F69240'],
                getMapText = getApp().globalData.Language.getMapText;
            let legend = [getMapText('去年同期新增'), getMapText('本期新增'), `${getMapText('同比增长率')}`],
                series = [{
                        name: getMapText('去年同期新增'),
                        type: 'bar',
                        data: data.histogram.filter(v => v.type == '去年同期新增' || v.key == '去年同期新增').map(v => v.value),
                        smooth: true
                    },
                    {
                        name: getMapText('本期新增'),
                        type: 'bar',
                        yAxisIndex: 1,
                        data: data.histogram.filter(v => v.type == '本期新增' || v.key == '本期新增').map(v => v.value),
                        smooth: true
                    },
                    {
                        name: getMapText('同比增长率'),
                        type: 'line',
                        yAxisIndex: 2,
                        data: data.lineChart.map(v => this.data.idname == 20231018155804 ? (v.value).toFixed(2) : (v.value * 100).toFixed(2)),
                        smooth: true
                    }
                ]
            const option = {
                color: colors,
                tooltip: {
                    trigger: 'axis',
                    confine: true, // Ensure tooltip stays within the chart area
                    formatter: function (params) {
                        let tooltipText = '';
                        params.forEach((item, index) => {
                            tooltipText += `${index==0?item.axisValue+'\n':''}${item.marker}${item.seriesName}: ${item.value}${item.seriesName == getMapText('同比增长率')||item.seriesName == getMapText('准交率') ? '%' : ''}\n`;
                        });
                        return tooltipText;
                    },
                    textStyle: {
                        fontSize: 10, // Reduced font size
                        lineHeight: 14 // Adjusted line height for smaller tooltip height
                    }
                },
                legend: {
                    data: legend
                },
                xAxis: [{
                    type: 'category',
                    axisTick: {
                        alignWithLabel: true
                    },
                    data: data.lineChart.map(v => v.date),
                    axisLabel: {
                        interval: 0,
                        rotate: 45
                    }
                }],
                yAxis: [{
                        type: 'value',
                        name: '',
                        position: 'right',
                        offset: 80,
                        alignTicks: true,
                        axisLine: {
                            show: true,
                        },
                        axisLabel: {
                            formatter: '{value}'
                        }
                    },
                    {
                        type: 'value',
                        name: '',
                        position: 'left',
                        alignTicks: true,
                        axisLine: {
                            show: false,
                        },
                        axisLabel: {
                            formatter: '{value}',
                            rotate: 45
                        }
                    },
                    {
                        type: 'value',
                        name: '',
                        position: 'right',
                        alignTicks: true,
                        offset: 80,
                        axisLine: {
                            show: false,
                            lineStyle: {
                                color: colors[1]
                            }
                        },
                        axisLabel: {
                            formatter: '{value}'
                        }
                    },

                ],
                series
            };
            this.chartComponent = this.selectComponent('#mychart');
            this.chartComponent.init((canvas, width, height, dpr) => {
                const chart = echarts.init(canvas, null, {
                    width,
                    height,
                    devicePixelRatio: dpr
                });
                chart.setOption(option);
                return chart;
            });
        },
        changeDate({
            detail
        }) {
            this.setData({
                "content.enddate": detail
            })
            this.getList(true)
        },
    }
})