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/index.vue"),
      meta: { index: 1 },
    },
    // 登录页面
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 微信登录跳板
    {
      path: "/wxLogin",
      name: "wxLogin",
      component: () => import("@/views/wxLogin.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 注册页面
    {
      path: "/register",
      name: "register",
      component: () => import("@/views/register.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 忘记密码页面
    {
      path: "/forgetpassword",
      name: "forgetPassword",
      component: () => import("@/views/forgetPassword"),
      meta: { index: 1, noLogin: true },
    },
    // 绑定微信
    {
      path: "/bindWechat",
      name: "bindWechat",
      component: () => import("@/views/bindWechat.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 修改密码页面
    {
      path: "/changepassword",
      name: "changePassword",
      component: () => import("@/views/changePassword"),
      meta: { index: 1, noLogin: true },
    },
    // 设备页面
    {
      path: "/device",
      name: "device",
      component: () => import("@/views/device/index"),
      meta: { index: 1, keepAlive: true },
    },
    // 设备详情
    {
      path: "/deviceSet",
      name: "deviceSet",
      component: () => import("@/views/device/deviceSet"),
      meta: { index: 1 },
    },
    // 编辑标签
    {
      path: "/editTag",
      name: "editTag",
      component: () => import("@/views/device/tagSet/index.vue"),
      meta: { index: 1 },
    },
    // { path: '/deviceOpr', name: 'deviceOpr', component: () => import('@/views/device/deviceOper2.vue'), meta: { index: 1 } },
    // 音量调节
    {
      path: "/modulation",
      name: "modulation",
      component: () => import("@/views/device/modulation.vue"),
      meta: { index: 1 },
    },
    // 远程开门
    {
      path: "/openDoor",
      name: "openDoor",
      component: () => import("@/views/device/openDoor.vue"),
      meta: { index: 1 },
    },
    // 远程做糖
    {
      path: "/doSugar",
      name: "doSugar",
      component: () => import("@/views/device/doSugar.vue"),
      meta: { index: 1 },
    },
    // 今日做糖列表
    {
      path: "/toDaySugarList",
      name: "toDaySugarList",
      component: () => import("@/views/device/toDaySugarList.vue"),
      meta: { index: 1 },
    },
    // 定时开关
    {
      path: "/alarmClock",
      name: "alarmClock",
      component: () => import("@/views/device/alarmClock.vue"),
      meta: { index: 1 },
    },
    // 设置闹钟
    {
      path: "/alarmClockAdd",
      name: "alarmClockAdd",
      component: () => import("@/views/device/alarmClockSet/index.vue"),
      meta: { index: 1 },
    },
    {
      path: "/alarmClockSet",
      name: "alarmClockSet",
      component: () => import("@/views/device/alarmClockSet/index.vue"),
      meta: { index: 1 },
    },
    // 参数设置
    {
      path: "/paramsSet",
      name: "paramsSet",
      component: () => import("@/views/device/paramsSet/index.vue"),
      meta: { index: 1 },
    },
    {
      path: "/paramsSetInfo",
      name: "paramsSetInfo",
      component: () => import("@/views/device/paramsSet/paramsSetInfo.vue"),
      meta: { index: 1 },
    },
    // 添加分销人
    {
      path: "/saveProportion",
      name: "saveProportion",
      component: () => import("@/views/device/saveProportion/index.vue"),
      meta: { index: 1 },
    },
    // 设备充值
    {
      path: "/recharge",
      name: "recharge",
      component: () => import("@/views/device/recharge.vue"),
      meta: { index: 1 },
    },
    // 机器排行
    {
      path: "/robotranking",
      name: "robotranking",
      component: () => import("@/views/robotRanking.vue"),
      meta: { index: 1 },
    },
    // 个人中心
    {
      path: "/user",
      name: "user",
      component: () => import("@/views/user.vue"),
      meta: { index: 1 },
    },
    // 结算账号
    {
      path: "/settlement",
      name: "settlement",
      component: () => import("@/views/settlement/index"),
      meta: { index: 1 },
    },
    // 自充值
    {
      path: "/uniPay",
      name: "uniPay",
      component: () => import("@/views/uniPay/index"),
      meta: { index: 1 },
    },
    // 优惠码
    {
      path: "/discountCode",
      name: "discountCode",
      component: () => import("@/views/discountCode/index"),
      meta: { index: 1 },
    },
    // Airwallex 钱包
    {
      path: "/airwallex",
      name: "airwallex",
      component: () => import("@/views/airwallex/index.vue"),
      meta: { index: 1 },
    },
    // Airwallex 创建付款人
    {
      path: "/airwallexBeneficiary",
      name: "airwallexBeneficiary",
      component: () => import("@/views/airwallex/beneficiary.vue"),
      meta: { index: 1 },
    },
    // Airwallex 付款单
    {
      path: "/airwallexPayout",
      name: "airwallexPayout",
      component: () => import("@/views/airwallex/payout.vue"),
      meta: { index: 1 },
    },
    // Airwallex 去付款
    {
      path: "/caPayment",
      name: "caPayment",
      component: () => import("@/views/airwallex/caPayment.vue"),
      meta: { index: 1 },
    },
    // Airwallex 付款结果详情
    {
      path: "/paymentDetail",
      name: "paymentDetail",
      component: () => import("@/views/airwallex/paymentDetail.vue"),
      meta: { index: 1 },
    },
    {
      path: "/payCode",
      name: "payCode",
      component: () => import("@/views/discountCode/payCode"),
      meta: { index: 1 },
    },
    // 分销设置
    {
      path: "/distributionSet",
      name: "distributionSet",
      component: () => import("@/views/distributionSet/index"),
      meta: { index: 1 },
    },
    {
      path: "/distributionDetail",
      name: "distributionDetail",
      component: () => import("@/views/distributionSet/detail"),
      meta: { index: 1 },
    },
    // 账号权限
    {
      path: "/accountPer",
      name: "accountPer",
      component: () => import("@/views/accountPer/index"),
      meta: { index: 1 },
    },
    // 添加账号
    {
      path: "/accountPerAdd",
      name: "accountPerAdd",
      component: () => import("@/views/accountPer/add"),
      meta: { index: 1 },
    },
    // 角色权限
    {
      path: "/role",
      name: "role",
      component: () => import("@/views/role/index"),
      meta: { index: 1 },
    },
    {
      path: "/roleSet",
      name: "roleSet",
      component: () => import("@/views/role/add"),
      meta: { index: 1 },
    },
    // 商户管理
    {
      path: "/merchantManage",
      name: "merchantManage",
      component: () => import("@/views/merchantManage/index"),
      meta: { index: 1 },
    },
    {
      path: "/merchantSet",
      name: "merchantSet",
      component: () => import("@/views/merchantManage/set"),
      meta: { index: 1 },
    },
    // 广告管理
    {
      path: "/advertManage",
      name: "advertManage",
      component: () => import("@/views/advertManage/index"),
      meta: { index: 1 },
    },
    {
      path: "/advertSet",
      name: "advertSet",
      component: () => import("@/views/advertManage/adSet.vue"),
      meta: { index: 1 },
    },
    // 广告规则
    {
      path: "/advertRule",
      name: "advertRule",
      component: () => import("@/views/advertManage/advertRule/index.vue"),
      meta: { index: 1 },
    },
    // 广告规则新增
    {
      path: "/advertRuleAdd",
      name: "advertRuleAdd",
      component: () => import("@/views/advertManage/advertRule/add.vue"),
      meta: { index: 1 },
    },
    {
      path: "/advertRuleAddScreen",
      name: "advertRuleAddScreen",
      component: () => import("@/views/advertManage/advertRule/screen.vue"),
      meta: { index: 1 },
    },
    // apk管理
    {
      path: "/apkManage",
      name: "apkManage",
      component: () => import("@/views/apkManage/index"),
      meta: { index: 1 },
    },
    // apk管理-新增
    {
      path: "/apkManageAdd",
      name: "apkManageAdd",
      component: () => import("@/views/apkManage/add"),
      meta: { index: 1 },
    },
    // 报警历史
    {
      path: "/alarmHistory",
      name: "alarmHistory",
      component: () => import("@/views/alarmHistory/index"),
      meta: { index: 1 },
    },
    // 订单导出
    {
      path: "/orderExport",
      name: "orderExport",
      component: () => import("@/views/orderExport/index"),
      meta: { index: 1 },
    },
    // 任务消息
    {
      path: "/taskMessage",
      name: "taskMessage",
      component: () => import("@/views/taskMessage/index"),
      meta: { index: 1 },
    },
    // 设备审批
    {
      path: "/taskEquipment",
      name: "taskEquipment",
      component: () => import("@/views/taskMessage/equipment"),
      meta: { index: 1 },
    },
    // 提现审批
    {
      path: "/taskJoinPayMchCheck",
      name: "taskJoinPayMchCheck",
      component: () => import("@/views/taskMessage/joinpayMchCheck"),
      meta: { index: 1 },
    },
    // 提现审批详情
    {
      path: "/taskJoinPayMchCheckInfo",
      name: "taskJoinPayMchCheckInfo",
      component: () => import("@/views/taskMessage/joinpayMchCheck/info"),
      meta: { index: 1 },
    },
    // 分销审批
    {
      path: "/taskProportion",
      name: "taskProportion",
      component: () => import("@/views/taskMessage/proportion"),
      meta: { index: 1 },
    },
    // 订单中心
    {
      path: "/orderCenter",
      name: "orderCenter",
      component: () => import("@/views/orderCenter/index"),
      meta: { index: 1 },
    },
    // 测试
    {
      path: "/test",
      name: "test",
      component: () => import("@/views/test"),
      meta: { index: 1 },
    },
    // 标签管理
    {
      path: "/labelMan",
      name: "labelMan",
      component: () => import("@/views/labelMan/index"),
      meta: { index: 1 },
    },
    // 标签管理-新增
    {
      path: "/labelManAdd",
      name: "labelManAdd",
      component: () => import("@/views/labelMan/add"),
      meta: { index: 1 },
    },
    // 修改价格
    {
      path: "/modifyPrice",
      name: "modifyPrice",
      component: () => import("@/views/device/modifyPrice/index"),
      meta: { index: 1 },
    },
    // 屏蔽/展示商品
    {
      path: "/showGoods",
      name: "showGoods",
      component: () => import("@/views/device/showGoods/index"),
      meta: { index: 1 },
    },
    // DIY花型
    {
      path: "/diyFlower",
      name: "diyFlower",
      component: () => import("@/views/device/diyFlower/index"),
      meta: { index: 1 },
    },
    // 修改机器密码
    {
      path: "/devicePassword",
      name: "devicePassword",
      component: () => import("@/views/device/devicePassword/index"),
      meta: { index: 1 },
    },
    // 查看定位
    {
      path: "/viewPosition",
      name: "viewPosition",
      component: () => import("@/views/device/viewPosition/index"),
      meta: { index: 1 },
    },
    // 设备编辑-编辑规则
    {
      path: "/editAdRule",
      name: "editAdRule",
      component: () => import("@/views/device/editAdRule/index"),
      meta: { index: 1 },
    },
    // 跳转空中云汇收单-消费者付款
    {
      path: "/hpp",
      name: "Hpp",
      component: () => import("@/views/Hpp.vue"),
      meta: { index: 1, noLogin: true },
    },
    // 定制logo
    {
      path: "/customLogo",
      name: "customLogo",
      component: () => import("@/views/device/customLogo.vue"),
      meta: { index: 1 },
    },
    // 查看日志
    {
      path: "/viewLogs",
      name: "viewLogs",
      component: () => import("@/views/device/viewLogs/index"),
      meta: { index: 1 },
    },
    // 账户操作
    {
      path: "/accountOperation",
      name: "accountOperation",
      component: () => import("@/views/accountOperation/index"),
      meta: { index: 1 },
    },
    // 汇付支付获取openid
    {
      path: "/weChatPay",
      name: "weChatPay",
      component: () => import("@/views/weChatPay"),
      meta: { index: 1, noLogin: true },
    },
    // 汇付支付获取openid
    {
      path: "/popPayment",
      name: "popPayment",
      component: () => import("@/views/popPayment"),
      meta: { index: 1, noLogin: true },
    },
    // apk管理，广告管理，订单导出 不是所有的帐号能看到
  ],
});
// 路由守卫处理
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);
      // 登录信息异常跳转登录页面
      if (!userObject) {
        router.push("/login");
      }
      if (typeof userObject.id !== "number") {
        router.push("/login");
      }
    }
    next();
  }
});

export default router;
