const _Http = getApp().globalData.http;

Page({
    data: {
        list: [],
        content: {
            nocache: true,
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1,
            type: "应用",
            where: {}
        },
        total: 0,
        isread: false
    },
    onLoad() {
        getApp().globalData.Language.getLanguagePackage(this)
    },
    onSwitchChange({
        detail
    }) {
        this.data.isread = detail;
        this.getlist(true)
    },
    //切换tab选项
    tabChange({
        detail
    }) {
        this.setData({
            "content.type": detail.name
        })
        this.getlist(true);
    },
    onReady() {
        this.selectComponent("#ListBox").setHeight(".headline", this);
    },
    getlist(init = false) {
        if (init.detail != undefined) init = init.detail;
        let content = this.data.content;
        if (init) {
            content.pageNumber = 1
            content.pageTotal = 1
        }
        if (content.pageNumber > content.pageTotal) return;
        content.isread = this.data.isread ? 0 : ""
        _Http.basic({
            "classname": "system.message.Message",
            "method": "queryMessage",
            content
        }).then(res => {
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.code != '1') 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 = getApp().globalData.Language.getMapText('客户')
                            //客户
                            break;
                        case "sat_orderclue":
                            data.name = getApp().globalData.Language.getMapText('线索')
                            //线索
                            break;
                        case "sa_order":
                            data.name = getApp().globalData.Language.getMapText('订单')
                            break;
                    }
                    return Object.assign(v, data)
                }
                return v
            })
            content.pageNumber = res.pageNumber + 1;
            content.pageTotal = res.pageTotal;
            this.setData({
                total: res.total,
                list: (res.pageNumber == 1) ? res.data : this.data.list.concat(res.data),
                content
            })
        })
    },
    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(); //更新信息数量
    }
})