import store from '@/store'
import router from '@/router'
import { Message, MessageBox } from 'element-ui'
import cookie from '@/utils/cookie'
import NProgress from 'nprogress';//加载进度条
import methods from '@/utils/methodsTool';
import 'nprogress/nprogress.css';//加载进度条
// const TenantId = '867735392558919680'
NProgress.configure({
    showSpinner: false
});
NProgress.inc(0.2);
router.beforeEach((to, from, next) => {
    console.log(to, from, true)
    //保险起见 路由跳转时检查token是否存在
    // if (router.history.pending.query.TenantId !== undefined) {
    //     sessionStorage.TenantId = router.history.pending.query.TenantId
    // }
    // if(sessionStorage.TenantId == 'undefined'){
    //     sessionStorage.removeItem("TenantId");
    // }
    var getToken
    if (methods.getQueryVariable('TenantId')) {
        getToken = cookie.getCookie('token' + methods.getQueryVariable('TenantId'))
    } else {
        getToken = cookie.getCookie('token' + sessionStorage.TenantId)
    }
    // 加载进度条start
    if (to.path !== from.path) {
        NProgress.start();
    }
    if (getToken) {
        if (to.path === '/login') {
            next({ path: '/' })//重定向回主页
            // 加载进度条end
        } else {
            if (store.getters.userInfo.length === 0) {
                store.dispatch('GetUserInfo').then(() => {
                    store.dispatch('GenerateRoutes').then(res => {
                        router.addRoutes(res)
                        next({ ...to, replace: true })
                    })
                }).catch(err => {
                    store.dispatch('outLogin').then(() => {
                        Message.error(err)
                        next({ path: '/' })
                    })
                })
            } else {
                if (to.matched.length === 0) {
                    Message.error('未找到该页面')
                    next({ path: '/404', replace: true })
                } else {
                    store.state.navActive = to.path//导航栏状态
                    store.commit('uploadTabberLists', to)
                    if (!to.query.TenantId) {
                        to.query.TenantId = sessionStorage.TenantId
                        next(to)
                    } else {
                        next()
                    }
                }
            }
        }
    } else {
        if (to.path === '/login') {
            next()
        } else {
            localStorage.clear()
            store.state.tabberLists = []
            store.state.routes = []
            store.state.allRoutes = []
            if (methods.getQueryVariable('TenantId')) {
                next({
                    path: '/login', query: {
                        TenantId: methods.getQueryVariable('TenantId')
                    }
                })
            } else {
                next({ path: '/login' })
            }

            Message({
                type: 'info',
                message: '登录已过期，请重新登录'
            });
            // 加载进度条end
        }
    }
})
router.afterEach((to, from) => {
    NProgress.done();
})
