import store from '@/utils/store';
import { deepTree, revisePath } from '@/utils/index';
import service from '@/service/index';
import router from '@/router/index';

export default {
	state: {
		routes: store.get('viewRoutes') || [],
		group: store.get('menuGroup') || [],
		menu: [],
		permission: [],
		collapse: false
	},
	actions: {
		permMenu({ commit }) {
			return new Promise((resolve, reject) => {
				// 	service.common
				// 		.permMenu()
				// 		.then(res => {
				// const routes = res.menus
				const routes = menu.menus
					.filter(e => e.type != 2)
					.map(e => {
						return {
							id: e.id,
							parentId: e.parentId,
							path: revisePath(e.router || e.id),
							viewPath: e.viewPath,
							type: e.type,
							name: e.name,
							icon: e.icon,
							orderNum: e.orderNum,
							meta: {
								keepAlive: e.keepAlive
							},
							children: []
						};
					});

				const menuGroup = deepTree(routes);

				commit('SET_PERMIESSION', menu.perms);
				commit('SET_MENU_GROUP', menuGroup);
				commit(
					'SET_VIEW_ROUTES',
					routes.filter(e => e.type == 1)
				);
				commit('SET_MENU_LIST', 0);

				resolve(menuGroup);
				// 		})
				// 		.catch(err => {
				// 			reject(err);
				// 		});
			});
		}
	},
	mutations: {
		SET_MENU_GROUP(state, list) {
			state.group = list;
			store.set('menuGroup', list);
		},

		SET_VIEW_ROUTES(state, list) {
			router.$plugin.addRoutes(list);

			state.routes = list;
			store.set('viewRoutes', list);
		},

		SET_MENU_LIST(state, index) {
			const item = state.group[index];
			state.menu = item ? item.children : [];
		},

		SET_PERMIESSION(state, list) {
			state.permission = list;
			store.set('permission', list);
		},

		COLLAPSE_MENU(state, val = false) {
			state.collapse = val;
		}
	}
};

let menu = {
	perms: [
		'sys:menu:add',
		'sys:menu:delete',
		'sys:menu:update',
		'sys:menu:page',
		'sys:menu:list',
		'sys:menu:info',
		'sys:user:add',
		'sys:user:delete',
		'sys:user:update',
		'sys:user:page',
		'sys:user:list',
		'sys:user:info',
		'sys:role:add',
		'sys:role:delete',
		'sys:role:update',
		'sys:role:page',
		'sys:role:list',
		'sys:role:info',
		'1',
		'sys:log:page',
		'sys:log:clear'
	],
	menus: [
		// {
		// 	id: '48',
		// 	parentId: '47',
		// 	name: '文档',
		// 	type: 1,
		// 	icon: 'icon-favorfill',
		// 	orderNum: 0,
		// 	router: '/tutorial/doc',
		// 	viewPath: 'https://docs.cool-admin.com/#/',
		// 	keepAlive: 1
		// },
		// 工作台
		// {
		// 	id: '1',
		// 	parentId: null,
		// 	name: '工作台',
		// 	type: 0,
		// 	icon: 'icongongzuotai',
		// 	orderNum: 1,
		// 	router: '/',
		// 	viewPath: null,
		// 	keepAlive: 1
		// },
		// 商家管理
		{
			id: '1',
			parentId: null,
			name: '商家管理',
			type: 0,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/shop-list',
			viewPath: null,
			keepAlive: 1
		},
		{
			id: '11',
			parentId: '1',
			name: '商家列表',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/merchant/merchant-list',
			viewPath: 'sass/shop/list',
			keepAlive: 1
		},
		{
			id: '12',
			parentId: '1',
			name: '申请记录',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/merchant/apply-list',
			viewPath: 'sass/shop/apply',
			keepAlive: 1
		},
		{
			id: '13',
			parentId: '1',
			name: '小程序升级',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/merchant/upgrade',
			viewPath: 'sass/shop/upgrade',
			keepAlive: 1
		},
		{
			id: '14',
			parentId: '1',
			name: '门店',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/merchant/shop-list',
			viewPath: 'sass/shop/shop',
			keepAlive: 1
		},
		{
			id: '14',
			parentId: '1',
			name: '购买记录',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/merchant/buy-list',
			viewPath: 'sass/shop/buy',
			keepAlive: 1
		},
		// 订单管理
		{
			id: '2',
			parentId: null,
			name: '订单管理',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/order/list',
			viewPath: 'sass/order/list',
			keepAlive: 1
		},
		// 商家客户
		{
			id: '3',
			parentId: null,
			name: '客户列表',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/member/list',
			viewPath: 'sass/member/list',
			keepAlive: 1
		},
		// 平台客户
		{
			id: '4',
			parentId: null,
			name: '平台客户',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/pt-member/list',
			viewPath: 'sass/pt-member/list',
			keepAlive: 1
		},
		// 商家商品
		{
			id: '5',
			parentId: null,
			name: '商品列表',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/goods/list',
			viewPath: 'sass/goods/list',
			keepAlive: 1
		},
		// 节日管理
		{
			id: '6',
			parentId: null,
			name: '节日列表',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/fest/list',
			viewPath: 'sass/fest/list',
			keepAlive: 1
		},
		// 套餐管理
		{
			id: '7',
			parentId: null,
			name: '套餐管理',
			type: 0,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/manage/list',
			viewPath: 'sass/manage/list',
			keepAlive: 1
		},
		{
			id: '701',
			parentId: '7',
			name: '套餐列表',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/manage/package',
			viewPath: 'sass/manage/package',
			keepAlive: 1
		},
		{
			id: '701',
			parentId: '7',
			name: '权限管理',
			type: 1,
			icon: 'icongongzuotai',
			orderNum: 1,
			router: '/sass/manage/auth',
			viewPath: 'sass/manage/auth',
			keepAlive: 1
		}
	]
};
