import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/',
    redirect: '/lab-status'
  },
  {
    path: '/lab-status',
    name: 'LabStatus',
    component: () => import('@/views/LabStatus.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/video-monitor',
    name: 'VideoMonitor',
    component: () => import('@/views/VideoMonitor.vue'),
    meta: { requiresAuth: true }
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    if (to.path !== '/login') {
      next('/login')
    } else {
      next()
    }
  } else if (to.path === '/login' && token) {
    next('/lab-status')
  } else {
    next()
  }
})

export default router
