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


const homeTab = {
    title:'控制台',
    name:'home',
    canClose:false
}
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        leftMenuCollapsed:false,
        menuTabs:[
            homeTab
        ],
        userInfo:{

        }
    },
    getters:{
        leftMenuCollapsed:state=>state.leftMenuCollapsed,
        menuTabs:state=>state.menuTabs,
        userInfo:state=>state.userInfo,
    },
    mutations: {
        CHANGECOLL(state,payload){
            state.leftMenuCollapsed = !state.leftMenuCollapsed
        },
        //添加路由
        PUSH_MENU(state,payload){
            let bool = true
            state.menuTabs.some(target=>{
                if(target.name == payload.name){
                    bool = false
                    return true
                }
            })
            if(bool){
                state.menuTabs.push(payload)
            }
            if(state.menuTabs.length>12){
                state.menuTabs = state.menuTabs.filter((x,i)=>{
                    return i!=1
                })
            }
            localStorage.setItem('rx-flq-menuList',JSON.stringify(state.menuTabs))
        },
        //set路由
        CONFIG_MENU(state,payload){
            state.menuTabs = payload
        },
        SET_USERINFO(state,payload){
            state.userInfo = payload
        },
        //删除路由
        DELETE_MENU(state,payload){
            state.menuTabs = state.menuTabs.filter(target=>{
                return target.name != payload.name
            })
            localStorage.setItem('rx-flq-menuList',JSON.stringify(state.menuTabs))
        },
    },
    actions: {
        pushMenu({commit},payload){
            commit('PUSH_MENU',payload)
        },
        configMenu({commit},payload){
            commit('CONFIG_MENU',payload)
        },
        deleteMenu({commit},payload){
            commit('DELETE_MENU',payload)
        },
        setUserInfo({commit},payload){
            commit('SET_USERINFO',payload)
        },
    },
    modules: {
    }
})
