import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

import Layout from '@/components/Layout.vue'
import HLayout from '@/components/HLayout.vue'
import Cookies from 'js-cookie'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/tourist/home'
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/register.vue')
    },
    // 管理端路径配置
    {
      path: '/admin',
      name: 'admin',
      component: Layout,
      redirect: '/admin/statisticalAnalysis',
      children: [
        // 数据统计
        {
          path: 'statisticalAnalysis',
          name: 'statisticalAnalysis',
          component: () => import('@/views/admin/statisticalAnalysis/index.vue')
        },
        // 藏品管理
        {
          path: 'relic',
          name: 'relic',
          component: () => import('@/views/admin/relic/index.vue')
        },
        // 藏品分类管理
        {
          path: 'itemCategory',
          name: 'itemCategory',
          component: () => import('@/views/admin/itemCategory/index.vue')
        },
        // 预约时段管理
        {
          path: 'reservationTimeSlot',
          name: 'reservationTimeSlot',
          component: () => import('@/views/admin/reservationTimeSlot/index.vue')
        },
        // 预约明细管理
        {
          path: 'reservation',
          name: 'reservation',
          component: () => import('@/views/admin/reservation/index.vue')
        },
        // 公告管理
        {
          path: 'announcement',
          name: 'announcement',
          component: () => import('@/views/admin/announcement/index.vue')
        },
        // 轮播图管理
        {
          path: 'carousel',
          name: 'carousel',
          component: () => import('@/views/admin/carousel/index.vue')
        },
        // 用户管理
        {
          path: 'user',
          name: 'user',
          component: () => import('@/views/admin/user/user.vue')
        },
        // 博物馆信息管理
        {
          path: 'museumIntro',
          name: 'museumIntro',
          component: () => import('@/views/admin/museumIntro/index.vue')
        },
        // 用户详情
        {
          path: 'userInfo',
          name: 'userInfo',
          component: () => import('@/views/admin/user/userInfo.vue')
        }
      ]
    },

    // 游客端路径配置
    {
      path: '/tourist',
      name: 'tourist',
      component: HLayout,
      redirect: '/tourist/home',
      children: [
        // 首页 
        {
          path: 'home',
          name: 'tHome',
          component: () => import('@/views/tourist/home/index.vue')
        },
        // 藏品
        {
          path: 'relic',
          name: 'tRelic',
          component: () => import('@/views/tourist/relic/index.vue')
        },
        // 藏品详情
        {
          path: 'relic/detail/:relicId',
          name: 'tRelicDetail',
          component: () => import('@/views/tourist/relic/detail.vue')
        },
        // 预约
        {
          path: 'reservation',
          name: 'tReservation',
          component: () => import('@/views/tourist/reservation/reserve.vue')
        },
        // 我的预约
        {
          path: 'myReservation',
          name: 'myReservation',
          component: () => import('@/views/tourist/reservation/index.vue')
        },
        // 公告
        {
          path: 'announcement',
          name: 'tAnnouncement',
          component: () => import('@/views/tourist/announcement/index.vue')
        },
        // 我的收藏
        {
          path: 'myRelicCollection',
          name: 'tRelicCollection',
          component: () => import('@/views/tourist/itemCollection/index.vue')
        },
        // 用户详情
        {
          path: 'userInfo',
          name: 'tUserInfo',
          component: () => import('@/views/tourist/user/userInfo.vue')
        }
      ]
    }
  ],
})

router.beforeEach((to, from, next) => {
  const type = Cookies.get('role')
  const publicPaths = ['/login', '/register', '/tourist/home', '/tourist/relic', '/tourist/announcement']
  if (publicPaths.includes(to.path) || to.path.startsWith('/tourist/relic/detail')) {
    next()
    return
  }
  if (!type && to.path === '/tourist/reservation') {
    ElMessage && ElMessage.warning('请先登录')
    next(false)
    return
  }
  if (!type) {
    next('/login')
    return
  }
  next()
})

export default router
