const _Http = getApp().globalData.http;

Page({
    data: {
        list: [],
        content: {
            nocache: true,
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1,
            type: "应用",
            where: {}
        },
        total: 0,
    },
    //切换tab选项
    tabChange({
        detail
    }) {
        this.setData({
            "content.type": detail.name
        })
        this.getlist(true);
    },
    onReady() {
        this.selectComponent("#ListBox").setHeight("#tabs", this);
    },
    getlist(init = false) {
        if (init.detail != undefined) init = init.detail;
        if (init) this.setData({
            ['content.pageNumber']: 1
        })
        if (this.data.content.pageNumber > this.data.content.pageTotal) return;
        _Http.basic({
            "classname": "system.message.Message",
            "method": "queryMessage",
            content: this.data.content
        }).then(res => {
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            const authComparison = wx.getStorageSync('authComparison');
            res.data = res.data.map(v => {
                let data = authComparison.find(s => (s.objectname || "") == v.objectname);
                if (data) {
                    switch (data.objectname) {
                        case "sa_customers":
                            data.name = '客户'
                            //客户
                            break;
                        case "sat_orderclue":
                            data.name = '线索'
                            //线索
                            break;
                        case "sa_order":
                            data.name = '订单'
                            break;
                    }
                    return Object.assign(v, data)
                }
                return v
            })
            this.setData({
                total: res.total,
                list: (res.pageNumber == 1) ? res.data : this.data.list.concat(res.data),
                ['content.pageNumber']: res.pageNumber + 1,
                ['content.pageTotal']: res.pageTotal
            })
        })
    },
    toDetails(e) {
        const {
            item
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: './details?item=' + JSON.stringify(item),
        })
    },
    onShow() {
        this.getlist(true);
        this.getTabBar().init();
        this.getTabBar().unReadMessageCount(); //更新信息数量
    }
})