// 102 按需加载路由
import Vue from 'vue'
import Router from 'vue-router'
 

Vue.use(Router)

const router =  new Router({
  mode:'hash',
  routes: [
    // 当路径为/,重定向路径到/login
    { path: '/', redirect: '/login' },
    // 当路径为/login,转到组件Login
    { path: '/login', component: () =>import('../views/Login.vue') },
    {
      path: '/home',
      component: () => import('../views/Home.vue'),
      // 当路径为/home,转到组件welcome
      redirect: '/welcome',
      //children里面放的是home组件下的子组件
      children:[
        { path: '/welcome', component: ()=> import('../views/Welcome.vue') },
        { path: '/FeedProcess', component: ()=> import('../views/dataStatistics/FeedProcess.vue') },
        { path: '/WeightTrend', component:() =>import('../views/dataStatistics/WeightTrend.vue') },
        { path: '/FormulaEffect', component: () => import('../views/dataStatistics/FormulaEffect.vue') },
        { path: '/GrowthRate', component: ()=> import('../views/dataStatistics/GrowthRate.vue') },
        { path: '/FeedCost', component: () => import('../views/dataStatistics/FeedCost.vue') },
        { path: '/DiseaseSituation', component: () => import('../views/dataStatistics/DiseaseSituation.vue') },
        { path: '/SurvivalRate', component: () => import('../views/dataStatistics/SurvivalRate.vue') },
        { path: '/WeightRecord', component: () => import('../views/cowManagement/WeightRecord.vue') },
        { path: '/CowInfo', component: () => import('../views/cowManagement/CowInfo.vue') },
        { path: '/EventRecord', component: () => import('../views/cowManagement/EventRecord.vue') },
        { path: '/CowCowShed', component: () => import('../views/cowShedManagement/CowCowShed.vue') },
        { path: '/CowShedGroup', component: () => import('../views/cowShedManagement/CowShedGroup.vue') },
        { path: '/FeedPlan', component: () => import('../views/formulaPlan/FeedPlan.vue') },
        { path: '/FormulaTemplate', component: () => import('../views/formulaPlan/FormulaTemplate.vue') },
        { path: '/WorkTaskAssign', component: () => import('../views/workManagement/WorkTaskAssign.vue') },
        { path: '/WorkTaskImplement', component: () => import('../views/workManagement/WorkTaskImplement.vue') },
        { path: '/CalfType', component: () => import('../views/basicSettings/CalfType.vue') },
        { path: '/WorkOrderType', component: () => import('../views/basicSettings/WorkOrderType.vue') },
        { path: '/FeedCarManagement', component: () => import('../views/basicSettings/FeedCarManagement.vue') },
        { path: '/DiseaseManagement', component: () => import('../views/basicSettings/DiseaseManagement.vue') },
        { path: '/InfoRemind', component: () => import('../views/basicSettings/InfoRemind.vue') },
        { path: '/DiseaseType', component: () => import('../views/basicSettings/DiseaseType.vue') },
        { path: '/FeedTemplate', component: () => import('../views/basicSettings/FeedTemplate.vue') },
        { path: '/DefaultPara', component: () => import('../views/basicSettings/DefaultPara.vue') },
        { path: '/FeedCostSet', component: () => import('../views/basicSettings/FeedCostSet.vue') },
        { path: '/User', component: () => import('../views/systemManagement/User.vue') },
        { path: '/Role', component: () => import('../views/systemManagement/Role.vue') },
      ]
    }
  ]
})



// 添加 beforeEach 导航守卫，如果用户没登录，直接访问其他页面，就跳转到登录页
router.beforeEach((to, form, next) => {
  // to 将要访问的路径
  // form 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行，next('/login')强制跳转
  //如果用户访问登录页，直接放行，
  if(to.path === '/login') return next();
  //从本地存储获取到token信息
  // const tokenStr  = window.sessionStorage.getItem('token')
  //没有token就强制跳转到登录页
  //if (!tokenStr) return next('/login')
  next()
})

export default router
