import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
// import Nprogress from 'nprogress'
// import 'nprogress/nprogress.css'
import { GET_TOKEN } from '@/utils/token'
import { KeepAlive } from 'vue'

const constantRoutes = [
  /**
   * redirect 默认路由:进入项目 默认进入 /index 页面
   * hidden 是否在路由栏显示
   * meta : {
      noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
      title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
      icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
      breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
      activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
    }
   */
 

  {
    path: '/login',
    name: "Login",
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
    },
  },

  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layout/index.vue'),
    redirect: '/miniProgram',
    meta: {
      title: 'Layout',
    },
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '',
          KeepAlive: false
        },
      },{
        path: '/groups',
        name: 'groups',
        component: () => import('@/views/groups/index.vue'),
        meta: {
          title: '',
          KeepAlive: true
        },
      },{
        path: '/groupDetails',
        name: 'groupDetails',
        component: () => import('@/views/groups/groupDetails/index.vue'),
        meta: {
          title: '',
          KeepAlive: false
        },
      },
      
      
      {
        path: '/miniProgram',
        name: 'miniProgram',
        component: () => import('@/views/miniProgram/index.vue'),
        meta: {
          title: '',
          KeepAlive: false,
          noPadding: true
        },
      },{
        path: '/agents',
        name: 'agents',
        component: () => import('@/views/ai/index.vue'),
        meta: {
          title: '',
          KeepAlive: false,
          noPadding: true
        },
      },{
        path: '/user',
        name: 'user',
        component: () => import('@/views/user/index.vue'),
        meta: {
          title: '',
          KeepAlive: true
        },
      },{
        path: '/userDetails',
        name: 'userDetails',
        component: () => import('@/views/user/userDetails/index.vue'),
        meta: {
          title: '',
          KeepAlive: false
        },
      },{
        path: '/task',
        name: 'task',
        component: () => import('@/views/task/index.vue'),
        meta: {
          title: '',
          KeepAlive: false
        },
      },{
        path: '/taskDetails',
        name: 'taskDetails',
        component: () => import('@/views/task/taskDetails/index.vue'),
        meta: {
          title: '',
          KeepAlive: false
        },
      },{
        path: '/materialLibrary',
        name: 'materialLibrary',
        component: () => import('@/views/materialLibrary/index.vue'),
        meta: {
          title: '',
          KeepAlive: false
        },
      },{
        path: '/tags',
        name: 'tags',
        component: () => import('@/views/tags/index.vue'),
        meta: {
          title: '',
          KeepAlive: true
        },
      },
      {
        path: '/tagChildren',
        name: 'tagChildren',
        component: () => import('@/views/tags/tagChildren/index.vue'),
        meta: {
          title: '',
          KeepAlive: false
        },
      },  
      {
        path: '/chat',
        name: 'chat',
        component: () => import('@/views/chat/index.vue'),
        meta: {
          title: '',
          KeepAlive: false,
          noPadding: true
        },
      },
      {
        path: '/customerService',
        name: 'customerService',
        component: () => import('@/views/customerService/index.vue'),
        meta: {
          title: '',
          KeepAlive: false,
          noPadding: true
        },
      },

    ],
  },
  {
    path: '/:pathMatch(.*)',
    name: 'Any',
    component: () => import('@/views/404/index.vue'),
    meta: {
      title: '404',
      hidden: true,
      icon: '',
    },
  }
]



export const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  // 切换路由跳转到顶部
  scrollBehavior() {
    return {
      left: 0,
      top: 0
    }
  }
})

const whiteList = ['/login']

// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  // Nprogress.start()
  // document.title = `${setting.title} - ${to.meta.title}`
  const token = GET_TOKEN()
  


  if (token) {
    if (to.path === '/login') {
      next('/robot')
    } else {
      next()
    }
  } else {
    // 没有token的情况下，可以进入白名单
    if (whiteList.indexOf(to.path) > -1) {
      next()
    } else {
      next('/login')
    }
  }
})

// 全局后置守卫
router.afterEach(() => {
  // Nprogress.done()
})

