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": {
            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;
            let dividend = wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000,
                getMapText = getApp().globalData.Language.getMapText;

            _Http.basic({
                "id": 20231011201004,
                content
            }).then(res => {
                console.log("报价分析", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.setData({
                    list: [{
                        name: getMapText("报价总次数"),
                        value: res.data.totalqty
                    }, {
                        name: getMapText("客户报价次数"),
                        text: getMapText("客户报价总次数"),
                        value: res.data.cusqty
                    }, {
                        name: getMapText("项目报价次数"),
                        text: getMapText("客户报价总次数"),
                        value: res.data.proqty
                    }, {
                        name: getMapText("报价总金额"),
                        value: CNY(res.data.totalamount >= dividend ? (res.data.totalamount / dividend) : res.data.totalamount),
                        suffix: res.data.totalamount >= dividend ? getMapText("万") : ""
                    }, {
                        name: getMapText("客户报价金额"),
                        value: CNY(res.data.cusamount >= dividend ? (res.data.cusamount / dividend) : res.data.cusamount),
                        suffix: res.data.cusamount >= dividend ? getMapText("万") : ""
                    }, {
                        name: getMapText("项目报价金额"),
                        value: CNY(res.data.proamount >= dividend ? (res.data.proamount / dividend) : res.data.proamount),
                        suffix: res.data.proamount >= dividend ? getMapText("万") : ""
                    }]
                })
            })

            _Http.basic({
                "id": 20231011154704,
                content
            }).then(res => {
                console.log("报价分析图表", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.initChart(res.data)
            })
        },
        initChart(data) {
            console.log("data", data)
            const getMapText = getApp().globalData.Language.getMapText,
                list = [{
                    name: "报价总数量",
                    key: 'newtotalqty',
                    color: "#5B8FF9"
                }, {
                    name: "客户报价数量",
                    key: 'newcusqty',
                    color: "#5AD8A6"
                }, {
                    name: "项目报价数量",
                    key: 'newproqty',
                    color: "#5D7092"
                }, {
                    name: "去年同期报价总数量",
                    key: 'oldtotalqty',
                    color: "#ECB937"
                }, {
                    name: "去年同期客户报价数量",
                    key: 'oldcusqty',
                    color: "#6F5EF9"
                }, {
                    name: "去年同期项目报价数量",
                    key: 'oldproqty',
                    color: "#6DC8EC"
                }]
            const option = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: list.map(v => getMapText(v.name)),
                },
                grid: {
                    top: '30%', // Adjust grid top to leave space for the legend
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.map(v => v.date),
                    axisLabel: {
                        interval: 0, // Force display all labels
                        rotate: 45 // Rotate labels to prevent overlap
                    }
                },
                yAxis: {
                    type: 'value'
                },
                series: list.map(v => {
                    return {
                        name: getMapText(v.name),
                        type: 'line',
                        data: data.map(obj => obj[v.key]),
                        itemStyle: {
                            color: v.color
                        },
                        lineStyle: {
                            color: v.color
                        }
                    }
                })
            };
            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)
        },
        showExplain(e) {
            const {
                name
            } = e.currentTarget.dataset;
            getApp().globalData.Language.modeBoxPrompts(getApp().globalData.Language.getMapText('统计到当前查询时间为止的') + name + "(" + getApp().globalData.Language.getMapText('审核状态') + ")")
        },
    }
})