import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import stock from '@/layout/stock.vue'
import login from '@/views/login.vue'
import systemCenter from'@/views/SystemCenter.vue'
import goodsInPda from '@/layout/goodsInForPDA.vue'

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: '/stock',
    children: [
      { path: '/stock', component: stock },// 库存
      { path: '/goodsIn', component: () => import('@/layout/goodsIn.vue') },// 入库
      { path: '/goodsOut', component: () => import('@/layout/goodsOut.vue') },// 出库
      { path: '/inTaskRecord', component: () => import('@/layout/inTaskRecord.vue') },// 入库记录
      { path: '/outTaskRecord', component: () => import('@/layout/outTaskRecord.vue') },// 出库记录
      { path: '/location', component: () => import('@/layout/location.vue') },// 库位
      { path: '/goods', component: () => import('@/layout/goods.vue') },// 物料
      { path: '/standSettings', component: () => import('@/layout/standSettings.vue') },// 站台（库口）设置
      { path: '/config', component: () => import('@/layout/config.vue') },// 系统配置
      { path: '/taskMonitor', component: () => import('@/layout/taskMonitor.vue') },// 任务监控
      { path: '/inventory', component: () => import('@/layout/inventory.vue') },// 盘点
      { path: '/inventoryRecord', component: () => import('@/layout/inventoryRecord.vue') },// 盘点
      { path: '/vehicle', component: () => import('@/layout/vehicle.vue') },// 料箱
    ]
  },
  {
    path: '/',
    name: 'login',
    component: login
  },
  {
    path: '/systemCenter',
    name: 'systemCenter',
    component: systemCenter
  },
  {
    path: '/goodsInPda',
    name: 'goodsInPda',
    component: goodsInPda
  },
]

const router = createRouter({
  base: '/',
  history: createWebHashHistory(),
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/') return next()
  // 获取token
  const user = sessionStorage.getItem('user')
  if (!user) return next('/')
  next()
})

export default router
