let authList = {};
//通用
function getapps() {
	const paths = [{
		name: "联系人",
		path: "/packageA/contacts/index",
		icon: "work-xiaochengxutongxunlu"
	}, {
		name: "工作汇报",
		path: "/packageA/report/index",
		icon: "work-gongzuohuibao"
	}, {
		name: "360°画像",
		path: "/portrayal/index/index",
		icon: "work-kehuhuaxiang"
	}, {
		name: "销售数据",
		path: "/salesPanel/index/index",
		icon: "work-xiaoshoushuju"
	}, {
		name: "数据总览",
		path: "/salesPanel/dataOverview/index",
		icon: "work-shujutongji"
	}];
	let app = [...getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['通用'], ['通用', '工作汇报']),
			...getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['CRM'], ['业务管理', '数据统计'])
		],
		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),
				istask: s.istask == 1,
				isdatafollowup: s.isdatafollowup == 1,
				isdatatag: s.isdatatag == 1,
				isdatateam: s.isdatateam == 1,
			}
			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;
					paths[i].appName = s.name;
					list.push(paths[i])
				}
			}
		})
	});
	return dye(list.sort((a, b) => a.index - b.index))
};
//CRM
function getcrm() {
	const paths = [{
		name: "市场活动",
		path: "/packageA/activity/index",
		icon: "work-shichanghuodong",
		objectname: "sat_campaign"
	}, {
		name: "医院管理",
		path: "/prsx/hospital/index",
		icon: "work-kehu",
		objectname: "sa_customers"
	}, {
		name: "科室管理",
		path: "/prsx/department/index",
		icon: "work-kehu",
		objectname: "sa_hospitaldep"
	}, {
		name: "医生管理",
		path: "/prsx/doctors/index",
		icon: "work-kehu",
		objectname: "sa_doctor"
	}, {
		name: "合同管理",
		path: "/packageA/contract/index",
		icon: "work-hetong",
		objectname: "sa_contract"
	}, {
		name: "营销目标",
		path: "/packageA/target/index",
		icon: "work-xiaochengxu_xiaoshoumubiao",
		objectname: "sa_salestarget"
	}, {
		name: "销售订单",
		path: "/packageA/orderForm/index",
		icon: "work-dingdan",
		objectname: "sa_order"
	}];
	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),
				istask: s.istask == 1,
				isdatafollowup: s.isdatafollowup == 1,
				isdatatag: s.isdatatag == 1,
				isdatateam: s.isdatateam == 1,
			}
			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;
					paths[i].appName = s.name;
					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) => {
		if (getApp().globalData.http.appName && v.appName == getApp().globalData.http.appName) {
			wx.navigateTo({
				url: v.path
			})
			delete getApp().globalData.http.appName
		}
		return {
			...v,
			...colorList[i > colorList.length - 1 ? i - colorList.length : i]
		}
	})
};
module.exports = {
	getapps,
	getcrm,
	authList
};