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

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('../views/Layout.vue'),
    meta: { requiresAuth: true },
    redirect: '/dashboard',
    children: [
      { path: 'dashboard', name: 'Dashboard', component: () => import('../views/Dashboard.vue'), meta: { title: '仪表盘' } },
      { path: 'products', name: 'Products', component: () => import('../views/Products.vue'), meta: { title: '商品管理' } },
      { path: 'product-edit', name: 'ProductEdit', component: () => import('../views/ProductEdit.vue'), meta: { title: '商品发布/编辑' } },
      { path: 'orders', name: 'Orders', component: () => import('../views/Orders.vue'), meta: { title: '订单管理' } },
      { path: 'users', name: 'Users', component: () => import('../views/Users.vue'), meta: { title: '会员管理' } },
      { path: 'distribution', name: 'Distribution', component: () => import('../views/Distribution.vue'), meta: { title: '分销管理' } },
      { path: 'marketing', name: 'Marketing', component: () => import('../views/Marketing.vue'), meta: { title: '营销工具' } },
      { path: 'exchange', name: 'Exchange', component: () => import('../views/Exchange.vue'), meta: { title: '积分商城' } },
      { path: 'ai-service', name: 'AiService', component: () => import('../views/AiService.vue'), meta: { title: 'AI助手' } },
      { path: 'collect', name: 'Collect', component: () => import('../views/Collect.vue'), meta: { title: '商品采集' } },
      { path: 'app-mgr', name: 'AppMgr', component: () => import('../views/AppMgr.vue'), meta: { title: 'APP管理' } },
      { path: 'terminal-mgr', name: 'TerminalMgr', component: () => import('../views/TerminalMgr.vue'), meta: { title: '终端管理' } },
      { path: 'terminal', name: 'Terminal', component: () => import('../views/terminal/Index.vue'), meta: { title: '多终端管理' } },
      { path: 'stats', name: 'Stats', component: () => import('../views/Stats.vue'), meta: { title: '数据统计' } },
      { path: 'funnel', name: 'Funnel', component: () => import('../views/Funnel.vue'), meta: { title: '转化漏斗' } },
      { path: 'theme-editor', name: 'ThemeEditor', component: () => import('../views/ThemeEditor.vue'), meta: { title: '页面装修' } },
      { path: 'theme-pages', name: 'ThemePages', component: () => import('../views/ThemePages.vue'), meta: { title: '主题页面' } },
      { path: 'my-themes', name: 'MyThemes', component: () => import('../views/MyThemes.vue'), meta: { title: '我的主题' } },
      { path: 'theme-visual', name: 'ThemeVisual', component: () => import('../views/ThemeVisual.vue'), meta: { title: '主题配色' } },
      { path: 'assets-mgr', name: 'AssetsMgr', component: () => import('../views/AssetsMgr.vue'), meta: { title: '素材管理' } },
      { path: 'banner', name: 'Banner', component: () => import('../views/Banner.vue'), meta: { title: '轮播图管理' } },
      { path: 'link-mgr', name: 'LinkMgr', component: () => import('../views/LinkMgr.vue'), meta: { title: '链接管理' } },
      { path: 'design-mgr', name: 'DesignMgr', component: () => import('../views/DesignMgr.vue'), meta: { title: '模板管理' } },
      { path: 'extra', name: 'Extra', component: () => import('../views/Extra.vue'), meta: { title: '拓展管理' } },
      { path: 'tools', name: 'Tools', component: () => import('../views/Tools.vue'), meta: { title: '系统工具' } },
      { path: 'migration', name: 'Migration', component: () => import('../views/Migration.vue'), meta: { title: '一键搬家' } },
      { path: 'notification', name: 'Notification', component: () => import('../views/Notification.vue'), meta: { title: '通知管理' } },
      { path: 'categories', name: 'Categories', component: () => import('../views/Categories.vue'), meta: { title: '类目管理' } },
      { path: 'cms', name: 'Cms', component: () => import('../views/Cms.vue'), meta: { title: '内容管理' } },
      { path: 'license', name: 'License', component: () => import('../views/License.vue'), meta: { title: '授权管理' } },
      { path: 'settings', name: 'Settings', component: () => import('../views/Settings.vue'), meta: { title: '系统设置' } }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('admin_token')
  document.title = (to.meta.title || 'LiteMall') + ' - 管理后台'
  if (to.meta.requiresAuth !== false && !token) next({ path: '/login', query: { redirect: to.fullPath } })
  else if (to.path === '/login' && token) next({ path: '/dashboard' })
  else next()
})

export default router
