import { createRouter, createWebHashHistory } from "vue-router";
import { getLocal } from "@/common/js/utils";

const router = createRouter({
  // hash模式：createWebHashHistory，history模式：createWebHistory
  history: createWebHashHistory(),
  routes: [
    // 首页
    { path: "/", redirect: "/home" },
    // Home 页面
    {
      path: "/home",
      name: "home",
      component: () => import("@/views/home/HomeIndex.vue"),
      meta: { index: 1 },
    },
    // 登录页面
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login/LoginIndex.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 注册页面
    {
      path: "/register",
      name: "register",
      component: () => import("@/views/login/Register.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 忘记密码页面
    {
      path: "/forgetPassword",
      name: "forgetPassword",
      component: () => import("@/views/login/ForgetPassword.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 修改密码页面
    {
      path: "/changepassword",
      name: "changePassword",
      component: () => import("@/views/user/ChangePassword.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 交易中心
    {
      path: "/trading",
      name: "trading",
      component: () => import("@/views/trading/TradingIndex.vue"),
      meta: { index: 1 },
    },
    // 买入股票
    {
      path: "/tradingBuy",
      name: "tradingBuy",
      component: () => import("@/views/trading/TradingBuy.vue"),
      meta: { index: 1 },
    },
    // 卖出股票
    {
      path: "/tradingSell",
      name: "tradingSell",
      component: () => import("@/views/trading/TradingSell.vue"),
      meta: { index: 1 },
    },
    // 交易历史
    {
      path: "/transactionHistory",
      name: "transactionHistory",
      component: () => import("@/views/trading/TransactionHistory.vue"),
      meta: { index: 1 },
    },
    // 买家付款确认列表
    {
      path: "/buyConfList",
      name: "buyConfList",
      component: () => import("@/views/buyOrSell/BuyConfList.vue"),
      meta: { index: 1 },
    },
    // 买家付款
    {
      path: "/buyConf",
      name: "buyConf",
      component: () => import("@/views/buyOrSell/BuyConf.vue"),
      meta: { index: 1 },
    },
    // 卖家收款确认列表
    {
      path: "/sellConfList",
      name: "sellConfList",
      component: () => import("@/views/buyOrSell/SellConfList.vue"),
      meta: { index: 1 },
    },
    // 卖家确认收款
    {
      path: "/sellConf",
      name: "sellConf",
      component: () => import("@/views/buyOrSell/SellConf.vue"),
      meta: { index: 1 },
    },
    // 持仓
    {
      path: "/position",
      name: "position",
      component: () => import("@/views/position/PositionIndex.vue"),
      meta: { index: 1 },
    },
    // 个人中心
    {
      path: "/user",
      name: "user",
      component: () => import("@/views/user/UserIndex.vue"),
      meta: { index: 1 },
    },
    // 公告编辑
    {
      path: "/announcement",
      name: "announcement",
      component: () => import("@/views/announcement/index"),
      meta: { index: 1 },
    },
    // 异议反馈
    {
      path: "/objectionFeedback",
      name: "feedback",
      component: () => import("@/views/feedback/FeedbackIndex.vue"),
      meta: { index: 1 },
    },
    // 账号权限
    {
      path: "/accountPer",
      name: "accountPer",
      component: () => import("@/views/accountPer/index"),
      meta: { index: 1 },
    },
    // 任务消息
    {
      path: "/taskMessage",
      name: "taskMessage",
      component: () => import("@/views/taskMessage/index"),
      meta: { index: 1 },
    },
    // 账号审核列表
    {
      path: "/taskAccountList",
      name: "taskAccountList",
      component: () => import("@/views/taskMessage/TaskAccountList.vue"),
      meta: { index: 1 },
    },
    // 账号审核
    {
      path: "/taskAccount",
      name: "taskAccount",
      component: () => import("@/views/taskMessage/TaskAccount.vue"),
      meta: { index: 1 },
    },
    // 用户管理列表
    {
      path: "/userManageList",
      name: "userManageList",
      component: () => import("@/views/taskMessage/UserManageList.vue"),
      meta: { index: 1 },
    },
    {
      path: "/userManage",
      name: "userManage",
      component: () => import("@/views/taskMessage/UserManage.vue"),
      meta: { index: 1 },
    },
    // 挂单匹配列表页
    {
      path: "/pendingOrderList",
      name: "pendingOrderList",
      component: () => import("@/views/taskMessage/PendingOrderList.vue"),
      meta: { index: 1 },
    },
    // 挂单匹配页
    {
      path: "/pendingOrder",
      name: "pendingOrder",
      component: () => import("@/views/taskMessage/PendingOrder.vue"),
      meta: { index: 1 },
    },
    // 成交列表页
    {
      path: "/transactionList",
      name: "transactionList",
      component: () => import("@/views/taskMessage/TransactionList.vue"),
      meta: { index: 1 },
    },
    // 成交页
    {
      path: "/transaction",
      name: "transaction",
      component: () => import("@/views/taskMessage/Transaction.vue"),
      meta: { index: 1 },
    },
    // 账户操作
    {
      path: "/accountOperation",
      name: "accountOperation",
      component: () => import("@/views/accountOperation/index"),
      meta: { index: 1 },
    },
  ],
});
// 路由守卫处理
router.beforeEach((to, from, next) => {
  // 从账户操作页面回到home页面
  // if (from.name === "accountOperation" && to.name === "home") {
  //   // router.push("/home");
  //   location.reload();
  //   // 阻止重定向回 账户操作页
  //   if (to.path === '/accountOperation') {
  //     next(false);
  //   } else {
  //     next();
  //   }
  // }

  // 页面带有不需要识别登录状态的跳过登录验证
  if (to.meta.noLogin) {
    next();
  } else {
    const user = getLocal("loginUser");
    if (!user || user === "") {
      // 没有登录信息跳转登录页面
      router.push("/login");
    } else {
      const userObject = JSON.parse(user);
      console.log("userObject >>>", userObject);
      // 登录信息异常跳转登录页面
      if (!userObject) {
        router.push("/login");
      }
    }
    next();
  }
});

export default router;
