let _Http = getApp().globalData.http,
    DataCarousel = null;
Page({
    data: {
        appid: "wxc1b6ae925ac1d06a",
        bannerList: [], //banner列表
        gridList: [],
        notice: "",
        msgCount: 1,
        msgList: [], //消息列表
    },
    onLoad(options) {
        this.setData({ //获取胶囊位置信息
            capsule: wx.getMenuButtonBoundingClientRect()
        })
        this.refreshData() //更新权限等信息
        this.getTabBar().unReadMessageCount(); //更新信息数量
    },
    /* 更新站点信息 */
    refreshData() {
        this.setData({
            user: wx.getStorageSync('userMsg')
        })
        if (wx.getStorageSync('userauth').length != 0) {
            let authList = {},
                entrance = [{
                    label: "E-订单",
                    appid: "wxc1b6ae925ac1d06a",
                    icon: "work-E-dingdan",
                    list: getedd()
                }];
            //E订单
            function getedd() {
                let paths = [{
                    name: "商城",
                    path: "/packageA/market/index",
                    icon: "work-shangcheng"
                }, {
                    name: "销售订单",
                    path: "/packageA/orderForm/index",
                    icon: "work-dingdan"
                }, {
                    name: "账户",
                    path: "/packageA/account/index",
                    icon: "work-zhanghu"
                }, {
                    name: "促销活动",
                    path: "/packageA/activity/index",
                    icon: "work-cuxiaohuodong"
                }, {
                    name: "购物车",
                    path: "/packageA/shopping/index",
                    icon: "work-gouwuche"
                },  {
                    name: "打款凭证",
                    path: "/packageA/remitVoucher/index",
                    icon: "work-dakuanpingzheng"
                },{
                    name: "退返申请",
                    path: "/packageA/returnOne/index",
                    icon: "work-shujuchaxun"
                },{
                    name: "发货单",
                    path: "/packageA/dispatchBill/index",
                    icon: "icon-shouhuo"
                }];
                let edd = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ["E-订单"], ["商品档案", "销售管理", "财务管理", "售后管理", "发货与库存", "业绩查询"]),
                    list = [];
                edd.forEach(v => {
                    v.apps.forEach(s => {
                        authList[s.name] = {
                            options: s.meta.auth.map(a => a.option),
                            optionnames: s.meta.auth.map(a => a.optionname),
                        }
                        if (authList[s.name].options.some(s => s == "read")) {
                            let i = paths.findIndex(k => k.name == s.meta.title);
                            if (i != -1) {
                                paths[i].index = i;
                                list.push(paths[i])
                            }
                        }
                    })
                });
                return dye(list.sort((a, b) => a.index - b.index))
            };
            //染色
            function dye(list) {
                let colorList = [{
                    color: "#3874F6",
                    bColor: "#F0F3FF",
                }, {
                    color: "#5AB73F",
                    bColor: "#F4FAEF",
                }, {
                    color: "#F29C37",
                    bColor: "#FCF6EF",
                }, {
                    color: "#EB4B5C",
                    bColor: "#FDF1ED",
                }, {
                    color: "#3874F6",
                    bColor: "#F0F3FF",
                }, {
                    color: "#F29C37",
                    bColor: "#FCF6EF",
                }, {
                    color: "#EB4B5C",
                    bColor: "#FDF1ED",
                }, {
                    color: "#3874F6",
                    bColor: "#F0F3FF",
                }, {
                    color: "#5AB73F",
                    bColor: "#F4FAEF",
                }, {
                    color: "#F29C37",
                    bColor: "#FCF6EF",
                }, {
                    color: "#5AB73F",
                    bColor: "#F4FAEF",
                }, {
                    color: "#3874F6",
                    bColor: "#F0F3FF",
                }, {
                    color: "#F29C37",
                    bColor: "#FCF6EF",
                }, {
                    color: "#EB4B5C",
                    bColor: "#FDF1ED",
                }, {
                    color: "#5AB73F",
                    bColor: "#F4FAEF",
                }];
                return list.map((v, i) => {
                    return {
                        ...v,
                        ...colorList[i > colorList.length - 1 ? i - colorList.length : i]
                    }
                })
            };
            wx.setStorageSync('auth', authList)
            this.setData({
                entrance,
                auth: JSON.stringify(authList),
                userMsg: JSON.stringify(wx.getStorageSync('userMsg')),
                site: JSON.stringify(wx.getStorageSync('siteP'))
            })
            /* 获取首页banner */
            let banner = wx.getStorageSync('banner_list').find(v => v.location == "index_top");
            if (banner) this.setData({
                bannerList: banner.ads
            })
        } else {
            setTimeout(this.refreshData, 10);
            return;
        }

    },
    /* 获取最新信息 */
    queryMessage(i = 0) {
        _Http.basic({
            "classname": "system.message.Message",
            "method": "queryMessage",
            content: {
                nocache: true,
                pageNumber: 1,
                pageSize: 5,
                pageTotal: 1,
                type: "",
                where: {}
            },
        }, false).then(res => {
            if (res.msg != '成功') return (i <= 5) ? this.queryMessage(i + 1) : wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                msgList: res.data,
                notice: res.data[0]
            })
            if (this.data.msgList.length > 2) this.startDataCarousel();
        })
    },
    /* 开启消息轮播 */
    startDataCarousel() {
        clearInterval(DataCarousel);
        DataCarousel = setInterval(() => {
            let count = this.data.msgCount < this.data.msgList.length ? this.data.msgCount : 0;
            this.setData({
                msgCount: count + 1,
                notice: this.data.msgList[count]
            })
        }, 5000)
    },
    /* 去消息详情 */
    toMsg(e) {
        const {
            item
        } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/tabbar/message/details?id=' + item.messageid,
        })
    },
    /* banner */
    bannerClick(e) {
        const {
            item
        } = e.currentTarget.dataset,
            hyperlink = item.hyperlink.split(":");
        if (hyperlink[0] == 'path') wx.navigateTo({
            url: hyperlink[1]
        })
    },
    onShow() {
        this.getTabBar().init();
        if (this.data.msgList.length > 2) this.startDataCarousel();
        this.queryMessage(0); //更新最新消息
    },
    onHide() {
        clearInterval(DataCarousel);
    },
    onReady() {
        // this.setListHeight();
    },
    /* 设置页面高度 */
    setListHeight() {
        this.selectComponent("#ListBox").setHeight(".grld-title", this);
    },
    onShareAppMessage() {}
})