let pageInit = null,
	_Http = getApp().globalData.http;


Page({
	data: {
		PageCur: 'Home',
		collectCount: "", //购物车商品数量
		msgList: []
	},
	onLoad(options) {
		pageInit = {
			Home: false,
			Message: false,
			Market: false,
			Collect: false,
			UserCenter: false,
		};
		if (options.PageCur) this.setData({
			PageCur: options.PageCur
		})
		this.refreshData();
		this.getCollectCount();
		this.updateMsgCount();
	},
	/**
	 * 更新站点信息
	 * 主要用来渲染首页的banner和宫格
	 */
	refreshData() {
		getApp().globalData.getCollectCount = this.getCollectCount.bind(this);
		getApp().globalData.socketCallback = this.updateMsgCount.bind(this);
		let page = this.selectComponent("#Home");
		if (page && wx.getStorageSync('userauth').length != 0) {
			let authList = {},
				entrance = [{
					label: "E-订单",
					appid: "wxc1b6ae925ac1d06a",
					icon: "work-E-dingdan",
					list: getedd()
				}];
			//E订单
			function getedd() {
				let paths = [{
					name: "商城",
					PageCur: "Market",
					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: "购物车",
					PageCur: "Collect",
					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)
			let banner = wx.getStorageSync('banner_list').find(v => v.location == "index_top");
			page.setData({
				entrance,
				bannerList: banner ? banner.ads : []
			});
			pageInit.Home = true;
			page.selectComponent("#ListBox").setHeight(".head", page);

		} else {
			setTimeout(this.refreshData, 10);
			return;
		}
	},
	/**
	 * 更新购物车数量
	 */
	getCollectCount() {
		return _Http.basic({
			"id": 20220927093202,
			"content": {}
		}).then(res => {
			console.log("购物车数量", res)
			getApp().globalData.collectCount = res.data.num;
			pageInit.Collect = false;
			this.setData({
				collectCount: res.data.num
			})
			return res.data.num;
		});
	},
	/* 更新消息数量 */
	updateMsgCount() {
		console.log("调用")
		pageInit.Message = false;
		_Http.basic({
			"classname": "system.message.Message",
			"method": "unReadMessageCount",
			"content": {
				nocache: true
			}
		}, false).then(res => {
			console.log('信息数量', res)
			if (res.msg != '成功') return;
			let fcount = res.data.fcount > 99 ? '99+' : res.data.fcount;
			if (res.data.fcount == 0) fcount = "";
			this.setData({
				fcount
			})
		});

		_Http.basic({
			"classname": "system.message.Message",
			"method": "queryMessage",
			content: {
				nocache: true,
				pageNumber: 1,
				pageSize: 1,
				type: "",
				where: {}
			},
		}).then(res => {
			if (res.msg != '成功') return;
			this.selectComponent("#Home").setData({
				msgList: res.data
			})
		})
	},
	/** 
	 * 切换页面
	 */
	NavChange(e) {
		this.cutBar({
			detail: e.currentTarget.dataset.cur
		});
		if (!getApp().globalData.socketEstablish) getApp().initSocket();
	},
	/** 
	 * 切换bar
	 */
	cutBar({
		detail
	}) {
		let page = this.selectComponent("#" + detail);
		if (!page) return;
		if (!pageInit[detail] && page.init()) pageInit[detail] = true;
		this.setData({
			PageCur: detail
		})
	},
})