import { createRouter, createWebHashHistory } from 'vue-router'

import Login from '@/views/Login/index.vue'
import Exam from '@/views/Exam/index.vue'
import ScaleList from '@/views/ScaleList/index.vue'
import ExamDetail from '@/views/ExamDetail/index.vue'

const router = createRouter({
  history: createWebHashHistory('/web'),
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: ScaleList
    },
    {
      path: '/exam',
      component: Exam
    },
    {
      path:'/examDetail',
      component: ExamDetail,
    }
  ],
  // 路由滚动行为定制
  scrollBehavior() {
    return {
      top: 0
    }
  }
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
}
)

export default router
