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

// 引入全局的 Layout 布局组件
import Layout from '@/components/Layout/index.vue'

const routes = [
  // ==========================================
  // 1. 独立页面 (不需要左侧菜单和右侧购物车的页面)
  // ==========================================
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/Register/index.vue'),
    meta: { title: '注册' }
  },
 {
    path: '/setAccount',
    name: 'setAccount',
    component: () => import('@/views/setAccount/index.vue'),
    meta: { title: '填写账号' }
  },


  // ==========================================
  // 2. 核心业务页面 (包裹在 Layout 中)
  // ==========================================
  {
    path: '/',
    component: Layout,
    redirect: '/Home', // 默认重定向到首页
    children: [
      // 原 Tabbar 页面
      {
        path: 'Home',
        name: 'Home',
        component: () => import('@/views/Home/index.vue'),
        meta: { title: '首页', requiresAuth: false }
      },
      {
        path: 'WorldCup',
        name: 'WorldCup',
        component: () => import('@/views/WorldCup/index.vue'),
        meta: { title: '世界杯', requiresAuth: false }
      },
      {
        path: 'SportsBetting',
        name: 'SportsBetting',
        component: () => import('@/views/SportsBetting/index.vue'),
        meta: { title: '赛事列表', requiresAuth: false }
      },
      {
        path: 'TotalSoccerAdvance',
        name: 'TotalSoccerAdvance',
        component: () => import('@/views/TotalSoccerAdvance/index.vue'),
        meta: { title: '冠军足球列表', requiresAuth: false }
      },
      {
        path: 'SportDetail',
        name: 'SportDetail',
        component: () => import('@/views/SportDetail/index.vue'),
        meta: { title: '赛事详情', requiresAuth: false }
      },
      {
        path: 'Digital',
        name: 'Digital',
        component: () => import('@/views/Digital/index.vue'),
        meta: { title: '澳门六合彩', requiresAuth: false }
      },
      {
        path: 'NewDigital',
        name: 'NewDigital',
        component: () => import('@/views/NewDigital/index.vue'),
        meta: { title: '新澳门六合彩', requiresAuth: false }
      },
      {
        path: 'SpeedDigital',
        name: 'SpeedDigital',
        component: () => import('@/views/SpeedDigital/index.vue'),
        meta: { title: '极速六合彩', requiresAuth: false }
      },
      {
        path: 'HKDigital',
        name: 'HKDigital',
        component: () => import('@/views/HKDigital/index.vue'),
        meta: { title: '香港六合彩', requiresAuth: false }
      },
      // {
      //   path: 'rolling',
      //   name: 'RollingBall',
      //   component: () => import('@/views/Tabbar/RollingBall/index.vue'),
      //   meta: { title: '滚球', requiresAuth: false }
      // },

      {
        path: 'EntertainmentList',
        name: 'EntertainmentList',
        component: () => import('@/views/EntertainmentList/index.vue'),
        meta: { title: '彩票列表', requiresAuth: false }
      },
      {
        path: 'Entertainment',
        name: 'Entertainment',
        component: () => import('@/views/Entertainment/index.vue'),
        meta: { title: '彩票', requiresAuth: false }
      },
      {
        path: 'ExtremeSpeed28',
        name: 'ExtremeSpeed28',
        component: () => import('@/views/ExtremeSpeed28/index.vue'),
        meta: { title: '极速28', requiresAuth: false }
      },
      {
        path: 'JisuGame',
        name: 'JisuGame',
        component: () => import('@/views/JisuGame/index.vue'),
        meta: { title: '极速游戏', requiresAuth: false }
      },
      {
        path: 'JisuDetail',
        name: 'JisuDetail',
        component: () => import('@/views/AllHistory/JisuDetail/index.vue'),
        meta: { title: '极速游戏详情', requiresAuth: false }
      },
      {
        path: 'EntertainmentHistory',
        name: 'EntertainmentHistory',
        component: () => import('@/views/Entertainment/history/index.vue'),
        meta: { title: '往期历史', requiresAuth: true }
      },
      {
        path: 'DigitalOrderList',
        name: 'DigitalOrderList',
        component: () => import('@/views/Digital/Order/List/index.vue'),
        meta: { title: '购彩记录', requiresAuth: false }
      },
      {
        path: 'DigitalOrderDetail',
        name: 'DigitalOrderDetail',
        component: () => import('@/views/Digital/Order/Detail/index.vue'),
        meta: { title: '购彩详情', requiresAuth: false }
      },
      {
        path: 'ChampionOrderDetail',
        name: 'ChampionOrderDetail',
        component: () => import('@/views/AllHistory/ChampionDetail/index.vue'),
        meta: { title: '冠军详情', requiresAuth: false }
      },



      // ==========================================
      {
        path: 'my',
        name: 'My',
        component: () => import('@/views/My/index.vue'),
        meta: { title: '个人中心', requiresAuth: true }
      },
      {
        path: 'Promotion',
        name: 'Promotion',
        component: () => import('@/views/Promotion/index.vue'),
        meta: { title: '优惠活动', requiresAuth: false }
      },
       {
        path: 'PromotionDetail',
        name: 'PromotionDetail',
        component: () => import('@/views/Promotion/PromotionDetail/index.vue'),
        meta: { title: '优惠活动', requiresAuth: false }
      },
      {
        path: 'Safety',
        name: 'Safety',
        component: () => import('@/views/Safety/index.vue'),
        meta: { title: '账户安全', requiresAuth: true }
      },
      {
        path: 'SortsBettingOrderDetail',
        name: 'SortsBettingOrderDetail',
        component: () => import('@/views/AllHistory/SortsBettingOrderDetail/index.vue'),
        meta: { title: '赛事单详情', requiresAuth: true }
      },
      {
        path: 'EntertainmentDetail',
        name: 'EntertainmentDetail',
        component: () => import('@/views/AllHistory/EntertainmentDetail/index.vue'),
        meta: { title: '彩票单详情', requiresAuth: true }
      },
      {
        path: 'LotteryOrderDetail',
        name: 'LotteryOrderDetail',
        component: () => import('@/views/AllHistory/Lottery/LotteryOrderDetail.vue'),
        meta: { title: '彩票单详情', requiresAuth: true }
      },
      {
        path: 'AllHistory',
        name: 'AllHistory',
        component: () => import('@/views/AllHistory/index.vue'),
        meta: { title: '投注历史', requiresAuth: true }
      },
      {
        path: 'LotteryDraw',
        name: 'LotteryDraw',
        component: () => import('@/views/LotteryDraw/index.vue'),
        meta: { title: '开奖历史', requiresAuth: true }
      },
      {
        path: 'LotteryDrawHistory',
        name: 'LotteryDrawHistory',
        component: () => import('@/views/LotteryDraw/LotteryDrawHistory.vue'),
        meta: { title: '开奖历史', requiresAuth: true }
      },

      {
        path: 'withdrawalHistory',
        name: 'withdrawalHistory',
        component: () => import('@/views/WithdrawalHistory/index.vue'),
        meta: { title: '提现记录', requiresAuth: true }
      },
      {
        path: 'fundRecord',
        name: 'fundRecord',
        component: () => import('@/views/FundRecord/index.vue'),
        meta: { title: '资金明细', requiresAuth: true }
      },
      {
        path: 'rechargeRecord',
        name: 'rechargeRecord',
        component: () => import('@/views/RechargeRecord/index.vue'),
        meta: { title: '充值记录', requiresAuth: true }
      },
       {
        path: 'Yuebao',
        name: 'Yuebao',
        component: () => import('@/views/Yuebao/index.vue'),
        meta: { title: '余额宝', requiresAuth: true }
      },
      {
        path: 'YuebaoItemList',
        name: 'YuebaoItemList',
        component: () => import('@/views/Yuebao/itemList.vue'),
        meta: { title: '余额宝', requiresAuth: true }
      },
      {
        path: 'CustomerService',
        name: 'CustomerService',
        component: () => import('@/businessComponents/CustomerService/index.vue'),
        meta: { title: '客服中心' }
      }
      // 后续可以将其他的设置、修改密码等子页面继续添加在这里...
    ]
  },

  // 404 兜底路由
  {
    path: '/:pathMatch(.*)*',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior() {
    return { top: 0 }
  }
})

// 全局路由守卫 (原先 uni-router 和 permission 的逻辑迁移到这里)
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token') // 替换 uni.getStorageSync

  // 设置页面标题 (如果有 i18n，这里可以配合 i18n 动态设置)
  if (to.meta.title) {
    document.title = `${to.meta.title} - F1BET`
  }

  // 权限拦截
  if (to.meta.requiresAuth && !token) {
    next({ name: 'login', query: { redirect: to.fullPath } })
  } else {
    next()
  }
})

export default router
