import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'UserLogin',
    component: () => import('@/components/layouts/UserLogin.vue'),
    meta: { title: "员工登录" }
  },
  {
    path: '/chat',
    component: () => import(/* webpackChunkName: "user" */ '@/views/chat'),
    hidden: true,
    children: [
      {
        path: 'chat',
        name: 'chat',
        meta: { title: "知识库问询" },
        component: () => import(/* webpackChunkName: "user" */ '@/views/chat')
      }
    ]
  },
  {
    path: '/',
    name: 'index',
    component: () => import('@/components/layouts/BasicLayout'),
    meta: { title: '知识库' },
    redirect: '/list',
    children: [
      {
        path: 'list',
        name: 'list',
        meta: { title: "知识库" },
        component: () => import(/* webpackChunkName: "user" */ '@/views/list/list')
      },
      {
        path: 'upload',
        name: 'upload',
        meta: { title: "上传文档" },
        component: () => import(/* webpackChunkName: "user" */ '@/views/upload')
      },
      {
        path: 'detail',
        name: 'detail',
        meta: { title: "知识库详情" },
        component: () => import(/* webpackChunkName: "user" */ '@/views/detail')
      },
      {
        path: 'history',
        name: 'history',
        meta: { title: "对话记录" },
        component: () => import(/* webpackChunkName: "user" */ '@/views/history')
      },
      {
        path: 'historyDetail',
        name: 'historyDetail',
        meta: { title: "对话记录详情" },
        component: () => import(/* webpackChunkName: "user" */ '@/views/history/detail')
      },
      {
        path: 'setting',
        name: 'setting',
        meta: { title: "对话记录详情" },
        component: () => import(/* webpackChunkName: "user" */ '@/views/setting')
      },

    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const publicPages = ['/login'];
  const authRequired = !publicPages.includes(to.path);
  const loggedIn = localStorage.getItem('userToken');

  if (authRequired && !loggedIn) {
    return next('/login');
  }

  next();
});

export default router

