import { createRouter, createWebHistory } from 'vue-router'
import Home from '../pages/Home.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/index',
    meta: { title: 'route.home' },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: Home,
        meta: { title: 'route.home' }
      }
    ]
  },
  {
    path: '/search-platform',
    name: 'SearchPlatformHome',
    component:() => import('@/pages/SearchPlatform.vue'),
    meta: { title: 'SearchPlatformHome' },
    children: [
      // {
      //   path: 'search-platform',
      //   name: 'SearchPlatform',
      //   component: () => import('@/pages/SearchPlatform.vue'),
      //   meta: { title: '' }
      // },
      {
        path: 'workflow-detail',
        name: 'WorkflowDetail',
        component: () => import('@/pages/WorkflowDetail.vue'),
        meta: { title: '' }
      },
      {
        path: 'workflow-add',
        name: 'WorkflowAdd',
        component: () => import('@/pages/WorkflowAdd.vue'),
        meta: { title: 'common.chuangjiangongzuoliu' }
      }
    ]
  },
  {
    path: '/workflow-add',
    name: 'WorkflowAddHome',
    component: () => import('@/pages/WorkflowAdd.vue'),
    meta: { title: 'common.chuangjiangongzuoliu' }
  },
  {
    path: '/workflow-detail',
    name: 'WorkflowDetailHome',
    component: () => import('@/pages/WorkflowDetail.vue'),
    meta: { title: '' }
  },
  {
    path: '/workflow-trade',
    name: 'WorkflowTradeHome',
    component: () => import('@/pages/workflowTrade/workflowTrade.vue'),
    meta: { title: 'common.gongzuoliu_trade' },
    children: [
      {
        path: 'workflow-trade-detail',
        name: 'WorkflowTradeDetail',
        component: () => import('@/pages/workflowTrade/workflowTradeDetail.vue'),
        meta: { title: '' }
      },
      {
        path: 'workflow-trade-add',
        name: 'WorkflowTradeAdd',
        component: () => import('@/pages/workflowTrade/workflowTradeAdd.vue'),
        meta: { title: 'route.gongzuoliu_trade_add' }
      }
    ]
  },
  {
    path: '/learning-system',
    name: 'LearningSystemHome',
    component: () => import('@/pages/LearningSystem/LearningSystem.vue'),
    meta: { title: 'route.learning_system' },
    children: [
      {
        path: 'detail/:courseId',
        name: 'LearningSystemDetail',
        component: () => import('@/pages/LearningSystem/LearningSystemDetail.vue'),
        meta: { title: '' },
        children: [
          {
            path: 'course/:courseId',
            name: 'CourseDetail',
            component: () => import('@/pages/LearningSystem/CourseDetail.vue'),
            meta: { title: '' }
          }
        ]
      },
    ]
  },
  {
    path: '/order-confirm',
    name: 'OrderConfirmHome',
    component: () => import('@/pages/order/orderConfirm.vue'),
    meta: { title: 'route.recharge' }
  },
  {
    path: '/learn-note',
    name: 'LearnNoteHome',
    component: () => import('@/pages/LearnNote/LearnNote.vue'),
    meta: { title: 'common.xuxibiji' }
  },
  {
    path: '/mibi-shop',
    name: 'MibiShopHome',
    component: () => import('@/pages/mibiShop/mibiShop.vue'),
    meta: { title: 'route.mibiShop' }
  },
  {
    path: '/personal-center',
    name: 'PersonalCenterHome',
    component: () => import('@/pages/PersonalCenter.vue'),
    meta: { title: 'personalCenter.personalCenter' },
    children:[
      {
        path: 'wallet',
        name: 'Wallet',
        component: () => import('@/pages/Personal/Wallet.vue')
      },
      {
        path: 'collection',
        name: 'Collection',
        component: () => import('@/pages/Personal/Collection.vue')
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('@/pages/Personal/Orders.vue')
      },
      {
        path: 'member-details',
        name: 'MemberDetails',
        component: () => import('@/pages/Personal/MemberDetails.vue')
      },
      {
        path: 'demand',
        name: 'Demand',
        component: () => import('@/pages/Personal/Demand.vue')
      },
      {
        path: 'invoice',
        name: 'Invoice',
        component: () => import('@/pages/Personal/Invoice.vue')
      },
      {
        path: 'workflow',
        name: 'Workflow',
        component: () => import('@/pages/Personal/Workflow.vue')
      },
      {
        path: 'business-management',
        name: 'BusinessManagement',
        component: () => import('@/pages/Personal/BusinessManagement.vue')
      }
    ]
  },
  {
    path:'/member',
    name:'Member',
    component: () => import('@/pages/Member.vue')
  },
  {
    path: '/agreement',
    name: 'Agreement',
    component: () => import('@/pages/Agreement.vue'),
    meta: { title: 'common.agreement' }
  },
  // 404 路由
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/pages/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  // 添加scrollBehavior配置项，实现页面切换时滚轴置顶
  scrollBehavior() {
    return { top: 0 }
  }
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  // document.title = to.meta.title || 'Boom Ai'
 
  // 动态设置路由的meta.title
  const lastRoute = to.matched?.[to.matched.length - 1]
  if (lastRoute && to.query.metaTitle) {
    to.meta.title =  to.query.metaTitle;
    lastRoute.meta.title = to.query.metaTitle;
  }
  next()
})

export default router