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

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: {
        active: 0,
        "id": 20231010133804,
        tabs: ['订单', '出货', '开票', '回款'],
        dateTypes: ["本年", "本季", "本月"],
        "content": {
            pageNumber: 1,
            pageTotal: 1,
            dateType: "本年",
            sequence: "订单",
            type: 0,
            where: {
                begdate: "",
                enddate: "",
                isleave: "1",
            }
        },
    },
    methods: {
        async getList(init = false) {
            if (init.detail != undefined) init = init.detail;
            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;
            const dividend = wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000,
                getMapText = getApp().globalData.Language.getMapText;
            if (init) {
                content.pageNumber = 1;
                content.pageTotal = 1;
            }
            if (content.pageNumber > content.pageTotal) return;
            _Http.basic({
                id: this.data.id,
                content
            }).then(res => {
                setTimeout(() => {
                    this.selectComponent('#ListBox').automaticSetHei();
                }, 200)
                this.selectComponent('#ListBox').RefreshToComplete();
                console.log(this.data.id, res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                res.data = res.data.map(v => {
                    const colors = ['#F56C6C', '#EF8E51', '#FFC148', '#999999'];
                    v.color = colors[v.rowindex - 1] || colors[3];
                    v.amount = CNY(v.amount)
                    v.outamount = CNY(v.outamount)
                    v.taxincludedamount = CNY(v.taxincludedamount)
                    v.writeoffamount = CNY(v.writeoffamount)
                    return v
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageSize": res.pageSize,
                })
                try {
                    this.selectComponent("#TimeRange").onCancel()
                } catch (error) {

                }
            })
        },
        changeDate({
            detail
        }) {
            this.setData({
                "content.dateType": detail.dateType,
                "content.where.begdate": detail.begdate || "",
                "content.where.enddate": detail.enddate || ""
            })
            this.getList(true)
        },
        changeSequence(e) {
            const {
                name
            } = e.currentTarget.dataset;
            if (this.data.content.sequence == name) return;
            this.setData({
                "content.sequence": name
            })
            this.getList(true)
        },
        tabsOnChange(e) {
            let name = e.detail.name
            if (name == this.data.id) return;
            this.setData({
                id: name,
                active: e.detail.index
            })
            this.getList(true);
        }
    }
})