import {createRouter, createWebHashHistory} from 'vue-router'
import Login from '@/pages/Login.vue'
import Layout from '@/pages/Layout.vue'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    { path: '/login', component: Login },
    {
      path: '/', component: Layout, redirect: '/dashboard',
      children: [
        { path: 'dashboard', component: () => import('@/pages/Dashboard.vue'), meta: { title: '概览' } },
        { path: 'users', component: () => import('@/pages/Users.vue'), meta: { title: '用户管理' } },
        { path: 'approvals', component: () => import('@/pages/Approvals.vue'), meta: { title: '认证审核' } },
        { path: 'departments', component: () => import('@/pages/Departments.vue'), meta: { title: '组织架构' } },
        { path: 'banners', component: () => import('@/pages/Banners.vue'), meta: { title: 'Banner管理' } },
        { path: 'notices', component: () => import('@/pages/Notices.vue'), meta: { title: '通知管理' } },
        { path: 'materials', component: () => import('@/pages/Materials.vue'), meta: { title: '素材上传' } },
        { path: 'config', component: () => import('@/pages/ApprovalConfig.vue'), meta: { title: '审批配置' } },
        { path: 'visit-types', component: () => import('@/pages/VisitTypes.vue'), meta: { title: '来访目的' } },
        { path: 'visitor-areas', component: () => import('@/pages/VisitorAreas.vue'), meta: { title: '到访区域' } },
        { path: 'appointments', component: () => import('@/pages/Appointments.vue'), meta: { title: '预约管理' } },
        { path: 'pending-employees', component: () => import('@/pages/PendingEmployees.vue'), meta: { title: '通讯录导入' } },
        { path: 'workflow-designer', component: () => import('@/pages/WorkflowDesigner.vue'), meta: { title: '流程设计器' } },
      ],
    },
  ],
})

router.beforeEach((to) => {
  const token = localStorage.getItem('admin_token')
  if (to.path !== '/login' && !token) return '/login'
})

export default router
