import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

export const routes = [
  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/BackgroundcolorManage',
  //   children: [
  //     {
  //       path: 'BackgroundcolorManage',
  //       name: 'BackgroundcolorManage',
  //       component: () => import('@/views/BackgroundcolorManage/index.vue'),
  //       meta: { title: '底色管理' },
  //     }
  //   ],
  // },
  {
    path: '/',
    redirect: '/menu',
  },
  {
    path: '/menu',
    name: 'menu',
    component: () => import('@/views/Menu/index.vue'),
    meta: { title: '智能中控装置' },
  },
  {
    path: '/monitoring',
    name: 'Monitoring',
    component: () => import('@/views/Monitoring/index.vue'),
    meta: { title: '实时监控' },
  },
  {
    path: '/handle-control',
    name: 'HandleControl',
    component: () => import('@/views/HandleControl/index.vue'),
    meta: { title: '手动控制' },
  },
  {
    path: '/params-config',
    name: 'ParamsConfig',
    component: () => import('@/views/ParamsConfig/index.vue'),
    meta: { title: '参数设置' },
  },
  {
    path: '/automatic-mode',
    name: 'AutomaticMode',
    component: () => import('@/views/AutomaticMode/index.vue'),
    meta: { title: '自动模式' },
  },
  {
    path: '/alarm-information',
    name: 'AlarmInformation',
    component: () => import('@/views/AlarmInformation/index.vue'),
    meta: { title: '报警预览' },
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

// 白名单
const wihteList = ['/menu']

// 全局前置导航守卫
router.beforeEach((to) => {
  // 开启进度条
  NProgress.start()
  // 如果没有 token ，并且不在白名单中，则从定向到登陆
  // if (!localStorage.getItem('token') && !wihteList.includes(to.path)) return '/menu'
})

// 全局后置导航守卫
router.afterEach((to) => {
  document.title = `${to.meta.title || ''}-智能中控系统`
  // 关闭进度条
  NProgress.done()
})

export default router
