import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/api'
import router from '@/router'
import cookie from '@/utils/cookie'
import path from '@/router/RoutingPath'
import { constantRouterMap } from '@/router'
const Layout = () => import("@/layout/index.vue")
//挂载Vuex
Vue.use(Vuex)
//创建VueX对象
const store = new Vuex.Store({
    state: {
        navActive: '',// 当前选择导航菜单
        tabberLists: [],//当前tab栏列表
        loginStatus: false,
        //存放的键值对就是所要管理的状态
        navStatus: false,
        routes: [],//新增路由
        allRoutes: [],// 总路由
        userInfo: [],//用户信息
    },
    getters: {
        addRouters(state) {
            return state.routes
        },
        navActive(state) {
            return state.navActive
        },
        tabberLists(state) {
            return state.tabberLists.length
        },
        userInfo(state) {
            return state.userInfo
        }
    },
    mutations: {
        // 获取最新路由情况
        getNewRoutes(state, routes) {
            state.routes = routes
            state.allRoutes = constantRouterMap.concat(routes)
            router.options.routes = state.allRoutes
            // }

        },
        userData(state, data) {
            state.userInfo.push(data)
        },
        //更新导航栏菜单状态
        uploadTabberLists(state, data) {
            if (localStorage['tabberLists'] !== undefined) {
                var tabberL = JSON.parse(localStorage['tabberLists'])
                new Promise((resolve, reject) => {
                    if (data.matched[0].name === 'error' || data.name === '/questionBank' || data.name === '/dictData') {
                        resolve()
                    } else {
                        tabberL.forEach((item, index) => {
                            if (item.path === data.path) {
                                resolve()
                            }
                        })
                        reject()
                    }
                }).then(res => {
                    state.tabberLists = tabberL
                    // console.log('找到相同');
                }).catch(err => {
                    var datas = {
                        title: data.meta.title,
                        path: data.path
                    }
                    tabberL.push(datas)
                    state.tabberLists = tabberL
                    localStorage['tabberLists'] = JSON.stringify(tabberL)
                })
            } else {
                var datas = {
                    title: data.meta.title,
                    path: data.path
                }
                state.tabberLists = [datas]
                localStorage['tabberLists'] = JSON.stringify([datas])
            }

        },
        delTabberLists(state, option) {
            var tabberL = JSON.parse(localStorage['tabberLists'])
            tabberL.splice(option.index, 1)
            localStorage['tabberLists'] = JSON.stringify(tabberL)
            state.tabberLists = tabberL
            if (option.path === option.item.path) {
                if (state.tabberLists.length === 1) {
                    router.replace({
                        path: '/home'
                    })
                } else {
                    if (option.index === tabberL.length) {
                        router.push({
                            path: tabberL[option.index - 1].path
                        })
                    } else {
                        router.push({
                            path: tabberL[option.index].path
                        })
                    }
                }
            }
        },
        delCookie(name) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = cookie.getCookie(name);
            if (cval != null)
                document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
        },

    },
    actions: {
        outLogin({ commit, state }) {
            return new Promise((resolve, reject) => {
                commit('delCookie', 'token')
                resolve()
            })
        },
        GetUserInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                api.getInfo().then(res => {
                    commit('userData', res.data)
                    resolve()
                }).catch(err => {
                    console.log(err);
                    reject()
                })
            })
        },
        GenerateRoutes({ commit, state }) {
            return new Promise((resolve, reject) => {
                const loadView = (view) => { // 路由懒加载
                    return (resolve) => require([`@/view/${view}/index.vue`], resolve)
                }
                // 调用接口 获取菜单列表
                api.getRouters().then(res => {
                    res.data.forEach((item, index) => {
                        if (item.component === 'Layout') {
                            item.component = Layout
                        }
                        if (item.children != null && item.children && item.children.length) {
                            item.children.forEach((items, index) => {
                                if (items.component) {
                                    // items.component = () => import(`@/view/${items.component}/index.vue`)
                                    items.component = loadView(items.component)
                                }
                            })
                        }
                    })
                    commit('getNewRoutes', res.data)
                    resolve(res.data)
                }).catch(err => {
                    console.log('路由获取失败');
                    console.log(err);
                })
                // 模拟数据👇
                let newRouters = [
                    {
                        name: "examManagement",
                        path: "/examManagement",
                        hidden: false,
                        redirect: "noRedirect",
                        component: Layout,
                        alwaysShow: true,
                        meta: { title: "考试安排", icon: "el-icon-notebook-1", noCache: false },
                        children: [
                            {
                                name: "collegePlan",
                                path: "/collegePlan",
                                hidden: false,
                                component: path.collegePlan,
                                meta: { title: "学员考试计划", icon: "user", noCache: false },
                            },
                            {
                                name: "examArrangement",
                                path: "/examArrangement",
                                hidden: false,
                                component: path.examArrangement,
                                meta: { title: "考试安排", icon: "user", noCache: false },
                            },
                            {
                                name: "examGuide",
                                path: "/examGuide",
                                hidden: false,
                                component: path.examGuide,
                                meta: { title: "报名指引", icon: "user", noCache: false },
                            },
                        ],
                    },
                    {
                        name: "systemSettings",
                        path: "/systemSettings",
                        hidden: false,
                        redirect: "noRedirect",
                        component: Layout,
                        alwaysShow: true,
                        meta: { title: "系统管理", icon: "el-icon-setting", noCache: false },
                        children: [
                            {
                                name: "modelPage",
                                path: "/modelPage",
                                hidden: false,
                                component: path.modelPage,
                                meta: { title: "模板页", icon: "user", noCache: false },
                            },
                            {
                                name: "personalCenter",
                                path: "/personalCenter",
                                hidden: false,
                                component: path.personalCenter,
                                meta: { title: "个人中心", icon: "user", noCache: false },
                            },
                            {
                                name: "operationLog",
                                path: "/operationLog",
                                hidden: false,
                                component: path.operationLog,
                                meta: { title: "操作日志", icon: "user", noCache: false },
                            },
                            {
                                name: "menu",
                                path: "/menu",
                                hidden: false,
                                component: path.menu,
                                meta: { title: "菜单管理", icon: "user", noCache: false },
                            }, {
                                name: "dict",
                                path: "/dict",
                                hidden: false,
                                component: path.dict,
                                meta: { title: "字典管理", icon: "user", noCache: false },
                            },
                            {
                                name: "dictData",
                                path: "/dictData",
                                hidden: true,
                                component: path.dictData,
                                meta: { title: "字典数据", icon: "user", noCache: false },
                            },
                            {
                                name: "department",
                                path: "/department",
                                hidden: false,
                                component: path.department,
                                meta: { title: "部门管理", icon: "user", noCache: false },
                            },
                            {
                                name: "jobManagement",
                                path: "/jobManagement",
                                hidden: false,
                                component: path.jobManagement,
                                meta: { title: "岗位管理", icon: "user", noCache: false },
                            },
                            {
                                name: "roleInfoManagement",
                                path: "/roleInfoManagement",
                                hidden: false,
                                component: path.roleInfoManagement,
                                meta: { title: "角色管理", icon: "user", noCache: false },
                            },
                            {
                                name: "usermanagement",
                                path: "/usermanagement",
                                hidden: false,
                                component: path.usermanagement,
                                meta: { title: "用户管理", icon: "user", noCache: false },
                            },
                        ],
                    },
                    {
                        name: "userManagement",
                        path: "/userManagement",
                        hidden: false,
                        redirect: "noRedirect",
                        component: Layout,
                        alwaysShow: true,
                        meta: { title: "用户管理", icon: "el-icon-user", noCache: false },
                        children: [
                            {
                                name: "personalManagement",
                                path: "/personalManagement",
                                hidden: false,
                                component: path.personalManagement,
                                meta: { title: "个人管理", icon: "user", noCache: false },
                            }
                        ],
                    },
                    {
                        name: "orderManagement",
                        path: "/orderManagement",
                        hidden: false,
                        redirect: "noRedirect",
                        component: Layout,
                        alwaysShow: true,
                        meta: { title: "订单管理", icon: "el-icon-document", noCache: false },
                        children: [
                            {
                                name: "orderList",
                                path: "/orderList",
                                hidden: false,
                                component: path.orderList,
                                meta: { title: "订单列表", icon: "user", noCache: false },
                            }
                        ],
                    }, {
                        name: "jobSearchManagement",
                        path: "/jobSearchManagement",
                        hidden: false,
                        redirect: "noRedirect",
                        component: Layout,
                        alwaysShow: true,
                        meta: { title: "求职管理", icon: "el-icon-suitcase", noCache: false },
                        children: [
                            {
                                name: "resumeManagement",
                                path: "/resumeManagement",
                                hidden: false,
                                component: path.resumeManagement,
                                meta: { title: "简历管理", icon: "user", noCache: false },
                            },
                            {
                                name: "deliveryRecord",
                                path: "/deliveryRecord",
                                hidden: false,
                                component: path.deliveryRecord,
                                meta: { title: "投递记录", icon: "user", noCache: false },
                            },
                            {
                                name: "employmentDemand",
                                path: "/employmentDemand",
                                hidden: true,
                                component: path.employmentDemand,
                                meta: { title: "用人需求", icon: "user", noCache: false },
                            }
                        ],
                    }, {
                        name: "CourseQuestionBank",
                        path: "/courseQuestionBank",
                        hidden: false,
                        redirect: "noRedirect",
                        component: Layout,
                        alwaysShow: true,
                        meta: { title: "课程题库", icon: "el-icon-collection", noCache: false },
                        children: [
                            {
                                name: "/courseManagement",
                                path: "/courseManagement",
                                hidden: false,
                                component: path.courseManagement,
                                meta: { title: "课程管理", icon: "user", noCache: false },
                            },
                            {
                                name: "keyTestSites",
                                path: "/keyTestSites",
                                hidden: false,
                                component: path.keyTestSites,
                                meta: { title: "重点考点", icon: "user", noCache: false },
                            },
                            {
                                name: "famousTeacherManagement",
                                path: "/famousTeacherManagement",
                                hidden: false,
                                component: path.famousTeacherManagement,
                                meta: { title: "名师管理", icon: "user", noCache: false },
                            },
                            {
                                name: "professionalManagement",
                                path: "/professionalManagement",
                                hidden: false,
                                component: path.professionalManagement,
                                meta: { title: "专业管理", icon: "user", noCache: false },
                            },
                            {
                                name: "questionBankManagement",
                                path: "/questionBankManagement",
                                hidden: false,
                                component: path.questionBankManagement,
                                meta: { title: "题库管理", icon: "user", noCache: false },
                            },
                            {
                                name: "examQuestionConfiguration",
                                path: "/examQuestionConfiguration",
                                hidden: false,
                                component: path.examQuestionConfiguration,
                                meta: { title: "考题配置", icon: "user", noCache: false },
                            },
                            {
                                name: "questionBank",
                                path: "/questionBank",
                                hidden: true,
                                component: path.questionBank,
                                meta: { title: "试卷题目", icon: "user", noCache: false },
                            }
                        ],
                    },

                ]
                let bbb = [
                    // {
                    //     name: "userManagement",
                    //     path: "/userManagement",
                    //     hidden: false,
                    //     redirect: "noRedirect",
                    //     component: Layout,
                    //     alwaysShow: true,
                    //     meta: { title: "用户管理", icon: "el-icon-user", noCache: true },
                    //     children: [
                    //         {
                    //             name: "personalManagement",
                    //             path: "/personalManagement",
                    //             hidden: false,
                    //             component: () => import('@/view/userManagement/personalManagement/index.vue'),
                    //             meta: { title: "个人管理", icon: "user", noCache: false },
                    //         }
                    //     ],
                    // },
                    {
                        name: "userManagement",
                        path: "/userManagement",
                        meta: { title: "用户管理", icon: "el-icon-user", noCache: false },
                        alwaysShow: true,
                        hidden: false,
                        redirect: "noRedirect",
                        component: Layout,
                        children: [
                            {
                                name: "personalManagement",
                                path: "/personalManagement",
                                hidden: false,
                                meta: { title: "个人管理", icon: "#", noCache: false },
                                // component: () => import('@/view/userManagement/personalManagement/index.vue'),
                                component: (resolve) => require(['@/view/userManagement/personalManagement/index.vue'], resolve)
                            }
                        ],
                    }
                ]
                // commit('getNewRoutes', newRouters)
                // resolve(newRouters)
            })
        },
    }
})

export default store