let pageInit = null,
	_Http = getApp().globalData.http,
	{
		vIndate
	} = require("../../utils/pay");

Page({
	data: {
		PageCur: 'Home',
		msgList: []
	},
	onLoad(options) {
		pageInit = {
			Home: false,
			Message: false,
			Market: false,
			Performance: false,
			UserCenter: false,
		};
		if (options.PageCur) this.setData({
			PageCur: options.PageCur
		})
		this.refreshData();
		this.updateMsgCount();
		this.getAnnunciate();
		if (getApp().globalData.remindchangepassword_str) wx.showModal({
			title: "提示",
			content: getApp().globalData.remindchangepassword_str,
			confirmText: '前往修改',
			cancelText: "下次再说",
			success: ({
				confirm
			}) => {
				getApp().globalData.remindchangepassword_str = null;
				if (confirm) wx.navigateTo({
					url: '/pages/index/userCenter/changePassword/index',
				})
			},
		})
		getApp().globalData.http.refreshData = this.refreshData.bind(this);
		vIndate();
	},
	getAnnunciate() {
		_Http.basic({
			"id": 20221111090904,
			"content": {
				"nocache": true,
				"pageSize": 1,
				"where": {
					"condition": ""
				}
			}
		}).then(res => {
			console.log("获取一条通告", res)
			if (res.msg == '成功' && res.data.length) this.selectComponent("#Home").setData({
				annunciate: res.data[0]
			})
		})
	},
	/**
	 * 更新站点信息
	 * 主要用来渲染首页的banner和宫格
	 */
	refreshData() {
		getApp().globalData.socketCallback = this.updateMsgCount.bind(this); //更新信息数量
		getApp().globalData.changeBar = this.cutBar.bind(this); //切换首页页面
		let page = this.selectComponent("#Home");
		if (page && wx.getStorageSync('userauth').length != 0) {
			let authList = {},
				entrance = [{
					label: "营销工具",
					icon: "work-yingxiaogongju",
					appid: "wx7505ddb0a1ec6146",
					list: gettool()
				}, {
					label: "E-订单",
					appid: "wx7505ddb0a1ec6146",
					icon: "work-E-dingdan",
					list: getedd()
				}];
			//营销工具
			function gettool() {
				const paths = [{
					name: "通告",
					path: "/marketing/annunciate/index",
					icon: "work-a-shouyejingangqutonggao",
					objectname: "sat_notice"
				}, {
					name: "营销物料",
					path: "/marketing/mediaLibrary/index",
					icon: "work-a-shouyejingangquyingxiaowuliao",
					objectname: "sys_attachment"
				}, {
					name: "推广素材",
					path: "/marketing/promotional/index",
					icon: "work-a-shouyejingangqutuiguangsucai",
					objectname: "sat_sharematerial"
				}, {
					name: "商学院",
					path: "/marketing/college/index",
					icon: "work-a-shangxueyuanxuexi",
					objectname: "sat_courseware"
				}, {
					name: "提报",
					path: "/marketing/submission/index",
					icon: "work-a-tibaoguanlitibao",
					objectname: "sat_submiteditmodel"
				}];
				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.sort((a, b) => a.index - b.index))
			};
			//E订单
			function getedd() {
				let paths = [{
					key: "ProductQuery",
					PageCur: "Market",
					icon: "work-shangcheng"
				}, {
					path: "/packageA/orderForm/index",
					icon: "work-dingdan"
				}, {
					key: "qOrderBlank",
					path: "/packageA/orderForm/index", //row
					icon: "work-dingdan"
				}, {
					name: "账户",
					path: "/packageA/account/index",
					icon: "work-zhanghu"
				}, {
					key: 'qDiscounts',
					path: "/packageA/activity/index",
					icon: "work-cuxiaohuodong"
				}, {
					name: "打款凭证",
					path: "/packageA/remitVoucher/index",
					icon: "work-dakuanpingzheng"
				}, {
					key: "qLogistics",
					path: "/packageA/shipment/index",
					icon: "icon-shouhuo"
				}, {
					name: "返退申请单",
					path: "/packageA/returnOne/index",
					icon: "work-shujuchaxun"
				}, {
					name: "发货单",
					path: "/packageA/dispatchBill/index",
					icon: "icon-shouhuo"
				}, {
					key: "qdeliverydetail",
					path: "/packageA/dispatchBill/rows",
					icon: "icon-shouhuo"
				}, {
					name: "工厂发退明细",
					path: "/packageA/dispatchBill/sentandback",
					icon: "icon-shouhuo"
				}, {
					key: "agentsmag",
					path: "/packageA/agentsmag/index",
					icon: "work-kehu"
				}];
				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.key == s.name);
							if (i != -1) {
								paths[i].index = i;
								paths[i].isneedpay = s.isneedpay;
								paths[i].name = s.meta.title;
								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);
			console.log("entrance", entrance)
			return entrance.find(v => v.label == "E-订单").list
		} else {
			setTimeout(this.refreshData, 10);
			return;
		}
	},
	/* 获取通告列表 */
	/**
	 * 更新购物车数量
	 */
	/* 更新消息数量 */
	updateMsgCount() {
		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
			})
		})
	},
	/** 
	 * 切换页面
	 */
	async NavChange(e) {
		let cur = e.currentTarget.dataset.cur;
		if (this.data.PageCur == cur) return;
		if (cur == 'Market' || cur == 'Performance') {
			let app = getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ["E-订单"], ["商品档案"])[0].apps
			let item = app.find(v => v.name == (cur == 'Market' ? "wmarket" : "wshoppingtrolley"))
			const res = await getApp().globalData.evidence(item);
			if (res) {
				this.cutBar({
					detail: e.currentTarget.dataset.cur
				});
				if (!getApp().globalData.socketEstablish) getApp().initSocket();
			}
		} else {
			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
		})
	},
})