import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from './stores/user'

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('./views/Index.vue'),
    redirect: '/Home',
    children: [
      {
        path: '/Home',
        name: 'Home',
        component: () => import('./views/Tabbars/Home.vue'),
        meta: { title: '主页', cache: true }
      },
      {
        path: '/Gift',
        name: 'Gift',
        component: () => import('./views/Tabbars/Gift.vue'),
        meta: { title: '礼品区', cache: true }
      },
      {
        path: '/Agriculture',
        name: 'Agriculture',
        component: () => import('./views/Tabbars/Agriculture.vue'),
        meta: { title: '助农区', cache: true }
      },
      {
        path: '/Mall',
        name: 'Mall',
        component: () => import('./views/Tabbars/Mall.vue'),
        meta: { title: '商城', cache: true }
      },
      {
        path: '/My',
        name: 'My',
        component: () => import('./views/User/My.vue'),
        meta: { title: '我的' }
      },
    ],
    meta: { noLogin: true }
  },
  {
    path: '/Login',
    name: 'Login',
    component: () => import('./views/Account/Login.vue'),
    meta: {
      title: '登录',
      noLogin: true,
      noRefresh: true,
    }
  },
  {
    path: '/ResetPwd',
    name: 'ResetPwd',
    component: () => import('./views/Account/ResetPwd.vue'),
    meta: {
      title: '修改密码',
      noLogin: true,
    }
  },
  {
    path: '/Balance',
    name: 'Balance',
    component: () => import('./views/User/Balance.vue'),
    meta: {
      title: '余额',
    }
  },
  {
    path: '/Voucher',
    name: 'Voucher',
    component: () => import('./views/User/Voucher.vue'),
    meta: {
      title: '消费券',
    }
  },
  {
    path: '/CV',
    name: 'CV',
    component: () => import('./views/User/CV.vue'),
    meta: {
      title: '贡献值',
    }
  },
  {
    path: '/Transfer',
    name: 'Transfer',
    component: () => import('./views/User/Transfer.vue'),
    meta: {
      title: '互转',
    }
  },
  {
    path: '/CashoutRecord',
    name: 'CashoutRecord',
    component: () => import('./views/User/CashoutRecord.vue'),
    meta: {
      title: '提现记录',
    }
  },
  {
    path: '/Cashout',
    name: 'Cashout',
    component: () => import('./views/User/Cashout.vue'),
    meta: {
      title: '提现',
    }
  },
  {
    path: '/Service',
    name: 'Service',
    component: () => import('./views/User/Service.vue'),
    meta: { title: '客服中心' }
  },
  {
    path: '/Address',
    name: 'Address',
    component: () => import('./views/User/Address.vue'),
    meta: { title: '收货地址' }
  },
  {
    path: '/Invite',
    name: 'Invite',
    component: () => import('./views/User/Invite.vue'),
    meta: { title: '邀请码' }
  },
  {
    path: '/Team',
    name: 'Team',
    component: () => import('./views/User/Team.vue'),
    meta: { title: '我的好友' }
  },
  {
    path: '/AliPayAccount',
    name: 'AliPayAccount',
    component: () => import('./views/User/AliPayAccount.vue'),
    meta: { title: '提现账号' }
  },
  {
    path: '/TradeConfirm',
    name: 'TradeConfirm',
    component: () => import('./views/Trade/TradeConfirm.vue'),
    meta: { title: '确认订单' }
  },
  {
    path: '/TradeDetail',
    name: 'TradeDetail',
    component: () => import('./views/Trade/TradeDetail.vue'),
    meta: { title: '订单详情' }
  },
  {
    path: '/TradeList',
    name: 'TradeList',
    component: () => import('./views/Trade/Tradelist.vue'),
    meta: { title: '订单列表' }
  },
  {
    path: '/Pay',
    name: 'Pay',
    component: () => import('./views/Trade/Pay.vue'),
    meta: { title: '支付' }
  },
  {
    path: '/Business_School',
    name: 'Business_School',
    component: () => import('./views/Platform/Business_School.vue'),
    meta: { title: '商学院' }
  },
  {
    path: '/School_detail',
    name: 'School_detail',
    component: () => import('./views/Platform/School_Detail.vue'),
    meta: { title: '商学院详情' }
  },
  {
    path: '/School_Category',
    name: 'School_Category',
    component: () => import('./views/Platform/School_Category.vue'),
    meta: { title: '商学院分类' }
  },
  {
    path: '/Column',
    name: 'Column',
    component: () => import('./views/Platform/Column.vue'),
    meta: { title: '栏目列表' }
  },
  {
    path: '/ColumnDetail',
    name: 'ColumnDetail',
    component: () => import('./views/Platform/ColumnDetail.vue'),
    meta: { title: '栏目详情' }
  },
  {
    path: '/GoodsDetail',
    name: 'GoodsDetail',
    component: () => import('./views/Goods/GoodsDetail.vue'),
    meta: { title: '商品详情' }
  },
  {
    path: '/Category',
    name: 'Category',
    component: () => import('./views/Goods/Category.vue'),
    meta: { title: '全部分类' }
  },
  {
    path: '/Merchant',
    name: 'Merchant',
    component: () => import('./views/Merchant/Merchant.vue'),
    meta: { title: '商家中心' }
  },
  {
    path: '/MerchantIncome',
    name: 'MerchantIncome',
    component: () => import('./views/Merchant/MerchantIncome.vue'),
    meta: { title: '营业收入' }
  },
  {
    path: '/Statistics',
    name: 'Statistics',
    component: () => import('./views/Merchant/Statistics.vue'),
    meta: { title: '数据统计' }
  },
  {
    path: '/MerchantTrade',
    name: 'MerchantTrade',
    component: () => import('./views/Merchant/MerchantTrade.vue'),
    meta: { title: '商家订单' }
  },
  {
    path: '/MerchantTradeDetail',
    name: 'MerchantTradeDetail',
    component: () => import('./views/Merchant/MerchantTradeDetail.vue'),
    meta: { title: '订单详情' }
  },
  {
    path: '/MerchantProduct',
    name: 'MerchantProduct',
    component: () => import('./views/Merchant/MerchantProduct.vue'),
    meta: { title: '商家产品' }
  },
  {
    path: '/Refresh',
    name: 'Refresh',
    component: { render: () => null },
    meta: { noLogin: true },
    beforeRouteEnter(to, from, next) {
      next(vm => {
        setTimeout(() => {
          const redirect = to.query.redirect || '/Home'
          vm.$router.replace(decodeURIComponent(redirect))
        }, 50)
      })
    }
  },
  {
    path: '/404',
    name: '404',
    component: () => import('./views/404.vue'),
    meta: {
      title: '404',
      noLogin: true,
    }
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/404'
  }
]

export { routes }

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

export default router

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (to.meta.noLogin) {
    return next();
  }
  const isLogin = userStore.isLogin;
  if (!isLogin) {
    return next({ name: 'Login', query: { redirect: to.fullPath } });
  }
  next();
});
