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

Page({
    data: {
        classShow: false,
        admin: false,
        showFiltrate: false,
        content: {
            baseonproject: 0, //1项目预测，0出货开票预测
            pageNumber: 1,
            pageTotal: 1,
            pagesize: 20,
            sort: [],
            where: {
                condition: "",
                begindate: "",
                enddate: ""
            }
        },
        navList: [{
            label: "我负责的",
            icon: "icon-webxialaxuanxiangjiantou",
            color: "",
            width: "",
            id: "1"
        }, {
            label: "排序",
            icon: "icon-jiangxu1",
            color: "",
            width: "",
            id: "sort"
        }, {
            label: "筛选",
            icon: "icon-shaixuan",
            color: "",
            width: "",
            id: "2"
        }],
        filtrateList: [{
            label: "状态",
            index: null,
            showName: "name", //显示字段
            valueKey: "status", //返回Key
            selectKey: "name", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                    name: "进行中"
                },
                {
                    name: "已截止"
                },
            ]
        }]
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '出货开票预测');
        let classActions = wx.getStorageSync('templetList').filter(v => v.templetid != '5').map((v, i) => {
            return {
                name: v.templetname,
                index: v.templetid,
                color: i == 0 ? '#3874F6' : '',
                i: i,
            }
        });
        if (wx.getStorageSync('userMsg').usertype != 0) classActions = classActions.filter(v => v.index != '99');
        this.setData({
            classActions,
            'navList[0].label': classActions[0].name,
            "content.type": classActions[0].index,
        })
        this.getList();
    },
    /* 获取列表 */
    getList(init = false) {
        if (init.detail != undefined) init = init.detail;
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;

        _Http.basic({
            "id": 20230706092304,
            content
        }).then(res => {
            console.log("出货开票预测", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.code != '1') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            res.data = res.data.map(v => {
                v.orderoutamount = CNY(v.orderoutamount)
                v.orderinvoamount = CNY(v.orderinvoamount)
                return v
            })
            this.setData({
                'content.pageNumber': res.pageNumber + 1,
                'content.pageTotal': res.pageTotal,
                'content.total': res.total,
                'content.sort': res.sort,
                list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
            });
        })
    },
    /* 处理搜索 */
    onSearch({
        detail
    }) {
        this.setData({
            "content.where.condition": detail
        });
        this.getList(true);
    },
    /* 顶部条件导航回调 */
    navClick({
        detail
    }) {
        switch (detail.id) {
            case '1':
                this.setData({
                    classShow: true
                })
                break;
            case '2':
                this.setData({
                    showFiltrate: true
                })
                break;
        }
    },
    classClose() {
        this.setData({
            classShow: false
        })
    },
    classSelect({
        detail
    }) {
        if (this.data.content.type == detail.index) return this.classClose();
        this.setData({
            "content.type": detail.index,
            'navList[0].label': detail.name,
            classActions: this.data.classActions.map(v => {
                v.color = detail.i == v.i ? '#3874F6' : ''
                return v
            })
        })
        console.log("classActions", this.data.classActions)
        this.classClose();
        this.getList(true)
    },
    handleFilter({
        detail
    }) {
        console.log(detail)
        if (detail.startdate) {
            this.setData({
                'content.where.begindate': detail.startdate || "",
                'content.where.enddate': detail.enddate || "",
            });
        } else {
            this.setData({
                'content.where.begindate': "",
                'content.where.enddate': "",
            });
        };
        this.setData({
            'content.where.status': detail.status,
            'content.where.periodstart': detail.periodstart,
            'content.where.periodend': detail.periodend,
        });
        this.getList(true)
    }
})