import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/components/Layout.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { public: true }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/customization/hsbc/equity-products',
    children: [
      {
        path: 'customization/hsbc/pages',
        redirect: 'customization/hsbc/pages/home',
        meta: { title: '页面管理' }
      },
      {
        path: 'customization/hsbc/pages/home',
        name: 'PageHomeConfig',
        component: () => import('@/views/PageHomeConfig.vue'),
        meta: { title: '权益介绍页' }
      },
      {
        path: 'customization/hsbc/pages/zone',
        name: 'PageZoneConfig',
        component: () => import('@/views/PageZoneConfig.vue'),
        meta: { title: '权益首页' }
      },
      {
        path: 'customization/hsbc/pages/assembly',
        name: 'PageAssembly',
        component: () => import('@/views/PageAssembly.vue'),
        meta: { title: '组装页' }
      },
      {
        path: 'customization/hsbc/equity-products',
        name: 'EquityProducts',
        component: () => import('@/views/EquityProducts.vue'),
        meta: { title: '借记卡权益' }
      },
      {
        path: 'customization/hsbc/credit-card-equity',
        name: 'CreditCardEquity',
        component: () => import('@/views/CreditCardEquity.vue'),
        meta: { title: '信用卡权益' }
      },
      {
        path: 'customization/hsbc/crowd-tasks',
        name: 'CrowdTaskList',
        component: () => import('@/views/CrowdTaskList.vue'),
        meta: { title: '客群任务管理' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/add',
        name: 'CrowdTaskAdd',
        component: () => import('@/views/CrowdTaskEdit.vue'),
        meta: { title: '新建客群任务' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/edit/:id',
        name: 'CrowdTaskEdit',
        component: () => import('@/views/CrowdTaskEdit.vue'),
        meta: { title: '编辑客群任务' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/copy/:id',
        name: 'CrowdTaskCopy',
        component: () => import('@/views/CrowdTaskEdit.vue'),
        meta: { title: '复制客群任务' }
      },
      {
        path: 'customization/hsbc/crowd-tasks/view/:id',
        name: 'CrowdTaskView',
        component: () => import('@/views/CrowdTaskView.vue'),
        meta: { title: '客群任务详情' }
      },
      {
        path: 'customization/hsbc/crowd-name-lists/add',
        name: 'CrowdNameListAdd',
        component: () => import('@/views/CrowdNameListEdit.vue'),
        meta: { title: '新增指定客户号客群' }
      },
      {
        path: 'customization/hsbc/crowd-name-lists/edit/:id',
        name: 'CrowdNameListEdit',
        component: () => import('@/views/CrowdNameListEdit.vue'),
        meta: { title: '编辑指定客户号客群' }
      },
      {
        path: 'customization/hsbc/rule-groups',
        name: 'RuleGroupList',
        component: () => import('@/views/RuleGroupList.vue'),
        meta: { title: '规则组管理' }
      },
      {
        path: 'customization/hsbc/rule-groups/add',
        name: 'RuleGroupAdd',
        component: () => import('@/views/RuleGroupEdit.vue'),
        meta: { title: '新增规则组' }
      },
      {
        path: 'customization/hsbc/rule-groups/edit/:id',
        name: 'RuleGroupEdit',
        component: () => import('@/views/RuleGroupEdit.vue'),
        meta: { title: '编辑规则组' }
      },
      {
        path: 'customization/hsbc/rule-groups/view/:id',
        name: 'RuleGroupView',
        component: () => import('@/views/RuleGroupView.vue'),
        meta: { title: '规则组详情' }
      },
      {
        path: 'customization/hsbc/rule-group-audit',
        name: 'RuleGroupAuditList',
        component: () => import('@/views/RuleGroupAuditList.vue'),
        meta: { title: '规则组审核' }
      },
      {
        path: 'customization/hsbc/rule-group-audit/:id',
        name: 'RuleGroupAuditDetail',
        component: () => import('@/views/RuleGroupAuditDetail.vue'),
        meta: { title: '规则组审核详情' }
      },
      {
        path: 'customization/hsbc/equity-products/add',
        name: 'AddEquityProduct',
        component: () => import('@/views/AddEquityProduct.vue'),
        meta: { title: '添加权益' }
      },
      {
        path: 'customization/hsbc/equity-products/view/:id',
        name: 'ViewEquityProduct',
        component: () => import('@/views/ViewEquityProduct.vue'),
        meta: { title: '查看权益' }
      },
      {
        path: 'customization/hsbc/equity-products/edit/:id',
        name: 'EditEquityProduct',
        component: () => import('@/views/EditEquityProduct.vue'),
        meta: { title: '编辑权益' }
      },
      {
        path: 'customization/hsbc/equity-products/audit/:id',
        name: 'AuditEquityProduct',
        component: () => import('@/views/AuditEquityProduct.vue'),
        meta: { title: '审核权益' }
      },
      {
        path: 'customization/hsbc/equity-products/coupons/:id',
        name: 'EquityProductCoupons',
        component: () => import('@/views/EquityProductCoupons.vue'),
        meta: { title: '券码管理' }
      },
      {
        path: 'customization/hsbc/tags',
        name: 'TagList',
        component: () => import('@/views/TagList.vue'),
        meta: { title: '标签管理' }
      },
      {
        path: 'customization/hsbc/tags/add',
        name: 'TagAdd',
        component: () => import('@/views/TagEdit.vue'),
        meta: { title: '新建标签' }
      },
      {
        path: 'customization/hsbc/tags/edit/:id',
        name: 'TagEdit',
        component: () => import('@/views/TagEdit.vue'),
        meta: { title: '编辑标签' }
      },
      {
        path: 'customization/hsbc/tags/view/:id',
        name: 'TagView',
        component: () => import('@/views/TagView.vue'),
        meta: { title: '查看标签' }
      },
      {
        path: 'customization/hsbc/sms-templates',
        name: 'SmsTemplateList',
        component: () => import('@/views/SmsTemplateList.vue'),
        meta: { title: '短信模板管理' }
      },
      {
        path: 'customization/hsbc/sms-send-records',
        name: 'SmsSendRecord',
        component: () => import('@/views/SmsSendRecord.vue'),
        meta: { title: '发送记录查询' }
      },
      {
        path: 'customization/hsbc/equity-grant',
        name: 'CustomerEquityGrant',
        component: () => import('@/views/CustomerEquityGrant.vue'),
        meta: { title: '客户权益发放' }
      },
      {
        path: 'customization/hsbc/cash-discount-grant',
        name: 'CashDiscountGrant',
        component: () => import('@/views/CashDiscountGrant.vue'),
        meta: { title: '立减金发放明细' }
      },
      {
        path: 'customization/hsbc/points-detail',
        name: 'PointsDetail',
        component: () => import('@/views/PointsDetail.vue'),
        meta: { title: '积分明细查询' }
      },
      {
        path: 'customization/hsbc/channel',
        name: 'ChannelManagement',
        component: () => import('@/views/ChannelManagement.vue'),
        meta: { title: 'Channel管理' }
      },
      {
        path: 'customization/hsbc/campaign',
        name: 'CampaignManagement',
        component: () => import('@/views/CampaignManagement.vue'),
        meta: { title: 'Campaign管理' }
      },
      {
        path: 'customization/hsbc/audit',
        name: 'AuditList',
        component: () => import('@/views/AuditList.vue'),
        meta: { title: '权益审核' }
      },
      {
        path: 'customization/hsbc/audit/:id',
        name: 'AuditDetail',
        component: () => import('@/views/AuditDetail.vue'),
        meta: { title: '审核详情' }
      },
      {
        path: 'customization/hsbc/equity-grant-campaign',
        name: 'EquityGrantCampaign',
        component: () => import('@/views/EquityGrantCampaign.vue'),
        meta: { title: '发放计划' }
      },
      {
        path: 'customization/hsbc/equity-grant-campaign/add',
        name: 'EquityGrantCampaignAdd',
        component: () => import('@/views/EquityGrantCampaignEdit.vue'),
        meta: { title: '新建发放计划' }
      },
      {
        path: 'customization/hsbc/equity-grant-campaign/edit/:id',
        name: 'EquityGrantCampaignEdit',
        component: () => import('@/views/EquityGrantCampaignEdit.vue'),
        meta: { title: '编辑发放计划' }
      },
      {
        path: 'customization/hsbc/equity-grant-campaign/copy/:id',
        name: 'EquityGrantCampaignCopy',
        component: () => import('@/views/EquityGrantCampaignEdit.vue'),
        meta: { title: '复制发放计划' }
      },
      {
        path: 'customization/hsbc/equity-grant-campaign/view/:id',
        name: 'EquityGrantCampaignView',
        component: () => import('@/views/EquityGrantCampaignView.vue'),
        meta: { title: '查看发放计划' }
      },
      {
        path: 'customization/hsbc/grant-task',
        name: 'GrantTaskList',
        component: () => import('@/views/GrantTaskList.vue'),
        meta: { title: '发放任务' }
      },
      {
        path: 'customization/hsbc/grant-task/view/:taskId',
        name: 'GrantTaskView',
        component: () => import('@/views/GrantTaskDetail.vue'),
        meta: { title: '查看发放任务' }
      },
      {
        path: 'customization/hsbc/grant-task/execute/:taskId',
        name: 'GrantTaskExecute',
        component: () => import('@/views/GrantTaskDetail.vue'),
        meta: { title: '执行发放任务' }
      },
      {
        path: 'customization/hsbc/campaign-audit',
        name: 'CampaignAuditList',
        component: () => import('@/views/CampaignAuditList.vue'),
        meta: { title: '发放计划审核' }
      },
      {
        path: 'customization/hsbc/campaign-audit/:id',
        name: 'CampaignAuditDetail',
        component: () => import('@/views/CampaignAuditDetail.vue'),
        meta: { title: '发放计划审核详情' }
      }
    ]
  },
  { path: '/:pathMatch(.*)*', redirect: '/' }
]

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

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta && to.meta.public) {
    if (token && to.path === '/login') return next('/')
    return next()
  }
  if (!token) {
    return next({ path: '/login', query: { redirect: to.fullPath } })
  }
  next()
})

export default router
