Page({
    data: {
        appid: "wx197f219a82a89d7b",
    },
    onLoad(options) {
        this.refreshData() //更新权限等信息
        this.setData({ //最近使用记录
            recorded: wx.getStorageSync('recorded') || []
        });
    },
    /* 更新站点信息 */
    refreshData() {
        if (wx.getStorageSync('userauth').length != 0) {
            let authList = {},
                entrance = [{
                    label: "通用",
                    appid: "wx197f219a82a89d7b",
                    icon: "work-tongyong",
                    list: getapps()
                }, {
                    label: "营销工具",
                    appid: "wx197f219a82a89d7b",
                    icon: "work-yingxiaogongju",
                    list: gettool()
                }, {
                    label: "CRM",
                    appid: "wx197f219a82a89d7b",
                    icon: "work-CRM",
                    list: getcrm()
                }, {
                    label: "E-订单",
                    appid: "wxc1b6ae925ac1d06a",
                    icon: "work-E-dingdan",
                    list: getedd()
                }, {
                    label: "E-服务",
                    appid: "wxc1b6ae925ac1d06a",
                    icon: "work-E-fuwu",
                    list: geteservice()
                }, ];
            //通用
            function getapps() {
                const paths = [{
                    name: "签到",
                    path: "/packageA/clockIn/index",
                    icon: "work-a-wodemendianxinxidizhi"
                }, {
                    name: "任务",
                    path: "/packageA/work/index",
                    icon: "work-a-woderenwuzhongxin"
                }, {
                    name: "通讯录",
                    path: "/packageA/contacts/index",
                    icon: "work-xiaochengxutongxunlu"
                }];
                let app = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['通用'], ['通用']),
                    list = [];
                app.forEach((v, i) => {
                    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)
            }
            //营销工具
            function gettool() {
                const paths = [{
                    name: "通告",
                    path: "/pages/annunciate/index",
                    icon: "work-a-shouyejingangqutonggao"
                }, {
                    name: "营销物料",
                    path: "/pages/mediaLibrary/index",
                    icon: "work-a-shouyejingangquyingxiaowuliao",
                    openType: "switchTab"
                }, {
                    name: "推广素材",
                    path: "/pages/promotional/index",
                    icon: "work-a-shouyejingangqutuiguangsucai"
                }, {
                    name: "商学院",
                    path: "/pages/college/index",
                    icon: "work-a-shangxueyuanxuexi"
                }, {
                    name: "提报",
                    path: "/pages/submission/index",
                    icon: "work-a-tibaoguanlitibao"
                }];
                let tool = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['营销工具'], paths.map(v => v.name)),
                    list = [];
                tool.forEach(v => {
                    let auth = v.apps[0].meta.auth;
                    if (!auth.some(r => r.option == 'read')) return;
                    authList[v.apps[0].name] = {
                        options: auth.map(a => a.option),
                        optionnames: auth.map(a => a.optionname)
                    };
                    let item = paths.find(s => s.name == v.systemmodulename);
                    item && list.push(item)
                });

                return dye(list)
            };
            //CRM
            function getcrm() {
                const paths = [{
                    name: "市场活动",
                    path: "/packageA/activity/index",
                    icon: "work-shichanghuodong",
                }, {
                    name: "公海线索",
                    path: "/packageA/publicClue/index",
                    icon: "work-xiaochengxu_xiaoshoumubiao"
                }, {
                    name: "销售线索",
                    path: "/packageA/saleClue/index",
                    icon: "work-xiaoshouxiansuo"
                }, {
                    name: "公海客户",
                    path: "/packageA/publicCustomer/index",
                    icon: "work-gonghaikehu"
                }, {
                    name: "客户",
                    path: "/packageA/setclient/index",
                    icon: "work-kehu"
                }, {
                    name: "项目商机",
                    path: "/packageA/project/index",
                    icon: "work-xiangmushangji"
                }, {
                    name: "报价单",
                    path: "/packageA/offers/index",
                    icon: "work-xiangmubaojia"
                }, {
                    name: "竞争对手",
                    path: "/packageA/opponent/index",
                    icon: "work-jingzhengduishou"
                }, {
                    name: "合同",
                    path: "/packageA/contract/index",
                    icon: "work-hetong"
                }, {
                    name: "销售目标",
                    path: "/packageA/target/index",
                    icon: "work-xiaochengxu_xiaoshoumubiao"
                }, {
                    name: "销售预测",
                    path: "/packageA/salesForecasting/index",
                    icon: "work-xiaochengxu_xiaoshouyuce"
                }];
                let crm = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['CRM'], ['销售预测', '销售线索', '销售目标', '合同管理', '客户管理', '项目管理']),
                    list = [];
                crm.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))
            };
            //E订单
            function getedd() {
                let paths = [{
                    name: "商城",
                    path: "/packageA/market/index",
                    icon: "work-shangcheng"
                }, {
                    name: "销售订单",
                    path: "/packageA/orderForm/index",
                    icon: "work-dingdan"
                }, {
                    name: "收货",
                    path: "/packageA/shipment/index",
                    icon: "work-shouhuo"
                }, {
                    name: "业绩目标",
                    path: "/packageA/target/index",
                    icon: "work-yejimubiao"
                }, {
                    name: "账户",
                    path: "/packageA/account/index",
                    icon: "work-zhanghu"
                }, {
                    name: "促销活动",
                    path: "/packageA/activity/index",
                    icon: "work-cuxiaohuodong"
                }, {
                    name: "工具查询",
                    path: "/packageA/tool/index",
                    icon: "work-gongjuchaxun"
                }, {
                    name: "购物车",
                    path: "/packageA/shopping/index",
                    icon: "work-gouwuche"
                }, {
                    name: "开票",
                    path: "/packageA/invoice/index",
                    icon: "work-kaipiao"
                }, {
                    name: "打款凭证",
                    path: "/packageA/remitVoucher/index",
                    icon: "work-dakuanpingzheng"
                }, {
                    name: "工具借用",
                    path: "/packageA/borrow/index",
                    icon: "work-shujuchaxun"
                }, {
                    name: "工具归还",
                    path: "/packageA/toolBill/index",
                    icon: "work-shujuchaxun"
                }, {
                    name: "居间费结算",
                    path: "/packageA/betweenFee/index",
                    icon: "work-jujianfeijiesuan"
                }];
                /* , {
                    name: "数据查询",
                    path: "#",
                    icon: "work-shujuchaxun"
                } */
                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))
            };
            //E服务
            function geteservice() {
                let paths = [{
                    name: "工单",
                    path: "/Eservice/workOrder/index",
                    icon: "work-gongdan"
                }, {
                    name: "申请单",
                    path: "/Eservice/agent/serviceBillList/index",
                    icon: "work-shenqingdan"
                }, {
                    name: "申请单管理",
                    path: "/Eservice/workerLeader/serviceBillList/index",
                    icon: "work-shenqingdanguanli"
                }, {
                    name: "业务员服务申请单",
                    path: "/Eservice/saler/serviceBillList/index",
                    icon: "work-yewuyuanfuwushenqingdan"
                }];
                let eservice = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ["E-服务"], ["工单管理"]),
                    list = [];
                eservice.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'))
            })
        } else {
            setTimeout(this.refreshData, 10);
            return;
        }
    },
    /* 使用记录 */
    openRecord(e) {
        let {
            item,
            it
        } = e.currentTarget.dataset;
        if (!it) return;
        if (item.appid) it.appid = item.appid;
        this.data.recorded.unshift(it);
        let recorded = this.data.recorded.reduce((pre, cur) => {
            if (!pre.some(v => v.name == cur.name)) pre.push(cur)
            return pre
        }, []);
        this.setData({
            recorded: recorded.slice(0, 5)
        })
        wx.setStorageSync('recorded', recorded.slice(0, 5))
    },
    onShow() {
        this.getTabBar().init();
    },
})