let authList = {};
//通用
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"
	}, {
		name: "工作汇报",
		path: "/packageA/report/index",
		icon: "work-gongzuohuibao"
	}, {
		name: "作业看板",
		path: "/packageA/achievement/index",
		icon: "work-zuoyekanban"
	}, {
		name: "销售漏斗",
		path: "/packageA/salesHourglass/index",
		icon: "work-xiaoshouloudou"
	}];
	let app = [...getApp().globalData.queryPer.query(wx.getStorageSync('userauth'), ['通用'], ['通用']), ...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),
				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;
					list.push(paths[i])
				}
			}
		})
	});
	return dye(list.sort((a, b) => a.index - b.index))
}
//营销工具
function gettool() {
	const paths = [{
		name: "通告",
		path: "/pages/annunciate/index",
		icon: "work-a-shouyejingangqutonggao",
		objectname: "sat_notice"
	}, {
		name: "营销物料",
		path: "/pages/mediaLibrary/index",
		icon: "work-a-shouyejingangquyingxiaowuliao",
		objectname: "sys_attachment"
	}, {
		name: "推广素材",
		path: "/pages/promotional/index",
		icon: "work-a-shouyejingangqutuiguangsucai",
		objectname: "sat_sharematerial"
	}, {
		name: "商学院",
		path: "/pages/college/index",
		icon: "work-a-shangxueyuanxuexi",
		objectname: "sat_courseware"
	}, {
		name: "提报",
		path: "/pages/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)
};
//CRM
function getcrm() {
	const paths = [{
		name: "市场活动",
		path: "/packageA/activity/index",
		icon: "work-shichanghuodong",
		objectname: "sat_campaign"
	}, {
		name: "公海线索",
		path: "/packageA/publicClue/index",
		icon: "work-xiaochengxu_xiaoshoumubiao",
		objectname: "sat_orderclue"
	}, {
		name: "销售线索",
		path: "/packageA/saleClue/index",
		icon: "work-xiaoshouxiansuo",
		objectname: "sat_orderclue"
	}, {
		name: "线索管理",
		path: "/packageA/saleAdmin/index",
		icon: "work-xiaoshouxiansuo",
		objectname: "sat_orderclue"
	}, {
		name: "公海客户",
		path: "/packageA/publicCustomer/index",
		icon: "work-gonghaikehu",
		objectname: "sa_customers"
	}, {
		name: "我的客户",
		path: "/packageA/setclient/index",
		icon: "work-kehu",
		objectname: "sa_customers"
	}, {
		name: "项目商机",
		path: "/packageA/project/index",
		icon: "work-xiangmushangji",
		objectname: "sa_project"
	}, {
		name: "报价单",
		path: "/packageA/offers/index",
		icon: "work-xiangmubaojia",
		objectname: "sa_quotedprice"
	}, {
		name: "竞争对手",
		path: "/packageA/opponent/index",
		icon: "work-jingzhengduishou",
		objectname: "sa_competitor"
	}, {
		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/salesForecasting/index",
		icon: "work-xiaochengxu_xiaoshouyuce",
		objectname: "sa_salesforecastbill"
	}, {
		name: "项目成交预测",
		path: "/packageA/finishforecast/index",
		icon: "work-xiangmuchengjiaoyuce"
	}, {
		name: "项目成交预测管理",
		path: "/packageA/finishforecast/admin",
		icon: "work-xiangmuchengjiaoyuceguanli"
	}, {
		name: "出货开票预测",
		path: "/packageA/invoiceforecast/index",
		icon: "work-chuhuokaipiaoyuce"
	}, {
		name: "出货开票预测管理",
		path: "/packageA/invoiceforecast/admin",
		icon: "work-chuhuokaipiaoyuceguanli"
	}];
	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;
					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",
		objectname: "sa_itemgroup"
	}, {
		name: "销售订单",
		path: "/packageA/orderForm/index",
		icon: "work-dingdan",
		objectname: "sa_order"
	}, {
		name: "收货",
		path: "/packageA/shipment/index",
		icon: "work-shouhuo",
		objectname: "sa_logistics"
	}, {
		name: "业绩目标",
		path: "/packageA/target/index",
		icon: "work-yejimubiao"
	}, {
		name: "账户",
		path: "/packageA/account/index",
		icon: "work-zhanghu",
		objectname: "sa_accountbalance"
	}, {
		name: "促销活动",
		path: "/packageA/activity/index",
		icon: "work-cuxiaohuodong",
		objectname: "sa_promotion"
	}, {
		name: "工具查询",
		path: "/packageA/tool/index",
		icon: "work-gongjuchaxun"
	}, {
		name: "购物车",
		path: "/packageA/shopping/index",
		icon: "work-gouwuche"
	}, {
		name: "开票",
		path: "/packageA/invoice/index",
		icon: "work-kaipiao",
		objectname: "sa_invoiceapplyid"
	}, {
		name: "打款凭证",
		path: "/packageA/remitVoucher/index",
		icon: "work-dakuanpingzheng"
	}, {
		name: "应收账款",
		path: "/packageA/receivables/index",
		icon: "work-kaipiao",
	}, {
		name: "工具借用",
		path: "/packageA/borrow/index",
		icon: "work-shujuchaxun",
		objectname: "sa_order"
	}, {
		name: "工具归还",
		path: "/packageA/toolBill/index",
		icon: "work-shujuchaxun",
		objectname: "sa_order"
	}, {
		name: "退返申请",
		path: "/packageA/returnOne/index",
		icon: "work-shujuchaxun",
		objectname: "sa_aftersalesmag"
	}, {
		name: "居间费结算",
		path: "/packageA/betweenFee/index",
		icon: "work-jujianfeijiesuan"
	}, ];
	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",
		objectname: "sa_workorder"
	}, {
		name: "申请单",
		path: "/Eservice/agent/serviceBillList/index",
		icon: "work-shenqingdan",
		// objectname: "sa_serviceorder"
	}, {
		name: "申请单管理",
		path: "/Eservice/workerLeader/serviceBillList/index",
		icon: "work-shenqingdanguanli",
		// objectname: "sa_serviceorder"
	}, {
		name: "业务员服务申请单",
		path: "/Eservice/saler/serviceBillList/index",
		icon: "work-yewuyuanfuwushenqingdan",
		// objectname: "sa_serviceorder"
	}];
	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]
		}
	})
};
module.exports = {
	getapps,
	gettool,
	getcrm,
	getedd,
	geteservice,
	authList
}