import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/dashboard',
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { title: '登录' },
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/Dashboard.vue'),
      meta: { title: '仪表盘', requiresAuth: true },
    },
    {
      path: '/group-course',
      name: 'GroupCourse',
      component: () => import('@/views/GroupCourse.vue'),
      meta: { title: '团课管理', requiresAuth: true },
    },
    {
      path: '/group-course-recommend',
      name: 'GroupCourseRecommend',
      component: () => import('@/views/GroupCourseRecommend.vue'),
      meta: { title: '推荐团课管理', requiresAuth: true },
    },
    {
      path: '/group-course-cancel',
      name: 'GroupCourseCancel',
      component: () => import('@/views/GroupCourseCancel.vue'),
      meta: { title: '取消团课', requiresAuth: true },
    },
    {
      path: '/group-course-types',
      name: 'GroupCourseType',
      component: () => import('@/views/GroupCourseType.vue'),
      meta: { title: '类型管理', requiresAuth: true },
    },
    {
      path: '/coach-manage',
      name: 'CoachManage',
      component: () => import('@/views/CoachManage.vue'),
      meta: { title: '教练管理', requiresAuth: true },
    },
    {
      path: '/member-manage',
      name: 'MemberManage',
      component: () => import('@/views/MemberManage.vue'),
      meta: { title: '会员管理', requiresAuth: true },
    },
    {
      path: '/member-card-manage',
      name: 'MemberCardManage',
      component: () => import('@/views/MemberCardManage.vue'),
      meta: { title: '会员卡管理', requiresAuth: true },
    },
    {
      path: '/member-signin',
      name: 'MemberSignIn',
      component: () => import('@/views/MemberSignIn.vue'),
      meta: { title: '签到明细', requiresAuth: true },
    },
    {
      path: '/data-report',
      name: 'DataReport',
      component: () => import('@/views/DataReport.vue'),
      meta: { title: '数据报表', requiresAuth: true },
    },
    {
      path: '/operation-log',
      name: 'OperationLog',
      component: () => import('@/views/OperationLog.vue'),
      meta: { title: '操作日志', requiresAuth: true },
    },
    {
      path: '/employee-manage',
      name: 'EmployeeManage',
      component: () => import('@/views/EmployeeManage.vue'),
      meta: { title: '账号管理', requiresAuth: true },
    },
    {
      path: '/role-manage',
      name: 'RoleManage',
      component: () => import('@/views/RoleManage.vue'),
      meta: { title: '角色权限', requiresAuth: true },
    },
    {
      path: '/notice-manage',
      name: 'NoticeManage',
      component: () => import('@/views/NoticeManage.vue'),
      meta: { title: '系统公告', requiresAuth: true },
    },
    {
      path: '/dict-manage',
      name: 'DictManage',
      component: () => import('@/views/DictManage.vue'),
      meta: { title: '字典管理', requiresAuth: true },
    },
  ],
})

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

export default router