import { createRouter, createWebHashHistory } from 'vue-router';
import NProgress from 'nprogress'//进度条
import 'nprogress/nprogress.css'
import { getToken } from '../utils/auth'
NProgress.configure({ showSpinner: false })

const routes = [
  {
    path: '/login',
    name: 'login',
    component: ()=>import('@/views/login/index.vue'),
    meta: {
      hidden: true,
      title: '登录'
    }
  },
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/tunnel/index.vue'),
    meta: {
      title: '首页',
      breadcrumb: true
    }
  },
  {
    path: '/debug',
    name: 'debug',
    component: () => import('@/views/debug/index.vue'),
    meta: {
      title: 'debug',
      breadcrumb: true
    }
  },
  {
    path: '/site',
    name: 'site',
    component: () => import('@/views/site/index.vue'),
    meta: {
      title: 'site',
      breadcrumb: true
    }
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach( (to,form,next)=>{
    NProgress.start()
    if(!getToken()) {
      if (to.path === '/login') {
        next()
        NProgress.done()
      } else {
        next({path: '/login'})
      }
    }else {
      console.log('有token')
      if(to.path === '/login') {
        next('/')
        NProgress.done()
      }else {
        next()
      }
    }
})
router.afterEach(()=>{
  NProgress.done()
})

export default router;

