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

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/ai-chat',
    children: [
      {
        path: 'ai-chat',
        name: 'AIChat',
        component: () => import('../views/AIChat.vue'),
        meta: { title: 'AI对话', icon: 'chat' }
      },
      {
        path: 'solution',
        name: 'Solution',
        component: () => import('../views/Solution.vue'),
        meta: { title: '解决方案', icon: 'solution' }
      },
      {
        path: 'solution-chat',
        name: 'SolutionChat',
        component: () => import('../views/SolutionChat.vue'),
        meta: { title: '解决方案对话', icon: 'solution' }
      },
      {
        path: 'doc-search',
        name: 'DocSearch',
        component: () => import('../views/DocSearch.vue'),
        meta: { title: '文档搜索', icon: 'search' }
      },
      {
        path: 'favorites',
        name: 'Favorites',
        component: () => import('../views/Favorites.vue'),
        meta: { title: '收藏夹', icon: 'star' }
      },
      {
        path: 'history',
        name: 'History',
        component: () => import('../views/History.vue'),
        meta: { title: '历史记录', icon: 'history' }
      },
      {
        path: 'user-info',
        name: 'UserInfo',
        component: () => import('../views/UserInfo.vue'),
        meta: { title: '用户信息', icon: 'user' }
      },
      {
        path: 'knowledge-list',
        name: 'KnowledgeList',
        component: () => import('../views/knowledgeList.vue'),
        meta: { title: '知识库列表', icon: 'knowledge' }
      },
      {
        path: 'knowledge-file',
        name: 'KnowledgeFile',
        component: () => import('../views/knowledgeFile.vue'),
        meta: { title: '知识库文件', icon: 'knowledge' }
      },

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

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem('token')
  
  if (to.path === '/login') {
    next()
  } else {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router 