import { createRouter, createWebHistory } 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: '/edit/:tunnelId(\\d+)/:type/:userId(\\d+)',
    name: 'tunneledit',
    component: () => import('@/views/edit/edit.vue'),
    meta: {
      title: '编辑首页',
      breadcrumb: true
    },
  },
  {
    path: '/debug',
    name: 'debug',
    component: () => import('@/views/debug/index.vue'),
    meta: {
      title: 'debug',
      breadcrumb: true
    }
  },
  {
    path: '/site/:userId(\\d+)',
    name: 'site',
    component: () => import('@/views/site/index.vue'),
    meta: {
      title: 'site',
      breadcrumb: true
    }
  },
  {
    path: '/user/:siteId(\\d+)',
    name: 'user',
    component: () => import('@/views/user/index.vue'),
    meta: {
      title: 'user',
      breadcrumb: true
    }
  },
  {
    path: '/tunnel/:siteId(\\d+)/:type/:userId(\\d+)',
    name: 'tunnel',
    component: () => import('@/views/tunnel-manage/index.vue'),
    meta: {
      title: 'tunnel',
      breadcrumb: true
    }
  },
  {
    path: '/device/:tunnelId(\\d+)/:type/:userId(\\d+)',
    name: 'device',
    component: () => import('@/views/device-manage/index.vue'),
    meta: {
      title: 'device',
      breadcrumb: true
    }
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});
router.beforeEach((to, form, next) => {
  NProgress.start()
  console.log('token-----',getToken())
  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;

