const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "￥", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();

import * as echarts from '../ec-canvas/echarts';
Component({
    properties: {

    },
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.setData({
                "content.dataid": wx.getStorageSync('userMsg').userid,
                "content.username": wx.getStorageSync('userMsg').name,
            })
        }
    },
    data: {
        "content": {
            unwriteoffamounttype: "订单",
            where: {}
        },
        pages: {
            pageNumber: 1,
            pageTotal: 1,
            pageSize: 20
        },
        list: []
    },
    methods: {
        async getList(init = false) {
            if (init.detail != undefined) init = init.detail;
            let content = this.data.content,
                pages = this.data.pages;
            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;
            if (init) {
                pages.pageNumber = 1;
                pages.pageTotal = 1;
            }
            if (pages.pageNumber <= pages.pageTotal) _Http.basic({
                "id": 20231016163904,
                content: Object.assign(content, pages)
            }).then(res => {
                console.log("出货未开票分析", res)
                this.selectComponent('#ListBox').automaticSetHei();
                this.selectComponent('#ListBox').RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                res.data = res.data.map(v => {
                    v.zerotothree = CNY(v.zerotothree)
                    v.threetosix = CNY(v.threetosix)
                    v.sixtotwelve = CNY(v.sixtotwelve)
                    v.twelveup = CNY(v.twelveup)
                    v.total = CNY(v.total)
                    return v
                })
                pages.pageNumber++;
                pages.pageTotal = res.pageTotal;
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    pages
                })
                if (init) this.initChart(res.data[0].Trend)
            })
        },
        toDetail(e) {
            let content = this.data.content;
            content.sys_enterpriseid = e.currentTarget.dataset.item.sys_enterpriseid;
            content.enterprisename = e.currentTarget.dataset.item.enterprisename;
            wx.navigateTo({
                url: '/salesPanel/AnalysisOfUninvoicedShipments/detail?content=' + JSON.stringify(content)
            })
        },
        initChart(data) {
            let dividend = wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000;
            let option = {
                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}\n`;
                        });
                        return tooltipText;
                    },
                    textStyle: {
                        fontSize: 10,
                    }
                },
                xAxis: {
                    max: 'dataMax'
                },
                yAxis: {
                    type: 'category',
                    data: data.map(v => getApp().globalData.Language.getMapText(v.key)),
                    inverse: true,
                    animationDuration: 300,
                    animationDurationUpdate: 300
                },
                series: [{
                    name: getApp().globalData.Language.getMapText('出货未开票金额') + '(' + getApp().globalData.Language.getMapText('万元') + ')',
                    type: 'bar',
                    data: data.map(v => (v.value / dividend).toFixed(2)),
                    label: {
                        show: true,
                        valueAnimation: true
                    }
                }],
                legend: {
                    show: true
                },
                grid: {
                    top: '10%',
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                animationDuration: 0,
                animationDurationUpdate: 3000,
                animationEasing: 'linear',
                animationEasingUpdate: 'linear'
            };
            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;
            });
        },

        showExplain(e) {
            const {
                name
            } = e.currentTarget.dataset;
            getApp().globalData.Language.modeBoxPrompts(getApp().globalData.Language.getMapText('统计到当前查询时间为止的') + name + "(" + getApp().globalData.Language.getMapText('审核状态') + ")")
        },
    }
})