import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		defaultActive: '',//默认打开菜单的url
		tabList: sessionStorage.childrenMenus && sessionStorage.childrenMenus != 'undefined' ? JSON.parse(sessionStorage.childrenMenus).filter(item => { return item.systemflag == '2' }) : [],//tab组件数据源
		userInfo: {},
		token: {},
		menus: [],
		pages: [],
		collapse: false
	},
	mutations: {
		/**
		 * 设置默认或当前打开菜单的url值
		 * @param {Object} state
		 * @param {Object} value
		 */
		setDefaultActive(state, value) {
			state.defaultActive = value;
		},
		/**
		 * 设置当前打开的所有页面
		 * @param {Object} state
		 * @param {Object} value
		 */
		setTabList(state, value) {
			state.tabList = value;
		},
		setUserInfo(state, value) {
			state.userInfo = value;
		},
		setToken(state, value) {
			state.token = value;
		},
		setMenus(state, value) {
			state.menus = value
		},
		setPages(state, value) {
			if (state.pages.indexOf(value) == -1) {
				state.pages.push(value)
			}
		},
		removePagesByName(state, value) {
			let arr = [...state.pages]
			for (let i = 0; i < arr.length; i++) {
				if (value == arr[i]) {
					arr.splice(i, 1);
				}
			}
			state.pages = arr;
		},
		setCollapse(state) {
			state.collapse = !state.collapse
		}
	},
	actions: {
	},
	modules: {
	}
})
