import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import Layout from "@/layout/index.vue";
import store from "@/store";
import { ElMessage, ElMessageBox } from "element-plus";

import NProgress from "nprogress";
import "nprogress/nprogress.css";
const routes = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/hotel",
    name: "hotel",
    component: Layout,
    children: [
      // {
      //   path: "blacklist",
      //   name: "blacklist",
      //   meta: { isAuth: true, title: "黑名单管理" },
      //   component: () => import("@/views/blacklist/blacklist.vue"),
      // },
      // {
      //   path: "roomList",
      //   name: "roomList",
      //   meta: { isAuth: true, title: "房间列表" },
      //   component: () => import("@/views/roomList/roomList.vue"),
      // },
      // {
      //   path: "staff",
      //   name: "staff",
      //   meta: { isAuth: true, title: "员工管理" },
      //   component: () => import("@/views/staff/staff.vue"),
      // },
      // {
      //   path: "order",
      //   name: "order",
      //   meta: { isAuth: true, title: "订单管理" },
      //   component: () => import("@/views/order/order.vue"),
      // },
      // {
      //   path: "class",
      //   name: "class",
      //   meta: { isAuth: true, title: "课表管理" },
      //   component: () => import("@/views/class/class.vue"),
      // },
      // {
      //   path: "process",
      //   name: "process",
      //   meta: { isAuth: true, title: "流程管理" },
      //   component: () => import("@/views/process/process.vue"),
      // },
      // {
      //   path: "statement",
      //   name: "statement",
      //   meta: { isAuth: true, title: "统计报表" },
      //   component: () => import("@/views/statement/statement.vue"),
      // },
      // {
      //   path: "user",
      //   name: "user",
      //   meta: { isAuth: true, title: "用户管理" },
      //   component: () => import("@/views/user/user.vue"),
      // },
      // {
      //   path: "userRoles",
      //   name: "userRoles",
      //   meta: { isAuth: true, title: "用户角色" },
      //   component: () => import("@/views/userRoles/userRoles.vue"),
      // },
      // {
      //   path: "roles",
      //   name: "roles",
      //   meta: { isAuth: true, title: "角色管理" },
      //   component: () => import("@/views/roles/roles.vue"),
      // },
      // {
      //   path: "doorLock",
      //   name: "doorLock",
      //   meta: { isAuth: true, title: "门锁管理" },
      //   component: () => import("@/views/doorLock/doorLock.vue"),
      // },
      // {
      //   path: "meter",
      //   name: "meter",
      //   meta: { isAuth: true, title: "水电表管理" },
      //   component: () => import("@/views/meter/meter.vue"),
      // },
      // {
      //   path: "account",
      //   name: "account",
      //   meta: { isAuth: true, title: "账号管理" },
      //   component: () => import("@/views/account/account.vue"),
      // },
      // {
      //   path: "system",
      //   name: "system",
      //   meta: { isAuth: true, title: "系统设置" },
      //   component: () => import("@/views/system/system.vue"),
      // },
      // {
      //   path: "icCard",
      //   name: "icCard",
      //   meta: { isAuth: true, title: "IC卡管理" },
      //   component: () => import("@/views/icCard/icCard.vue"),
      // },
      // {
      //   path: "fingerprint",
      //   name: "fingerprint",
      //   meta: { isAuth: true, title: "指纹管理" },
      //   component: () => import("@/views/fingerprint/fingerprint.vue"),
      // },
    ],
  },
];
const router = createRouter({
  // history: createWebHashHistory("/business"), // 线上地址
  history: createWebHashHistory("/hotel/iHotelStudentManage"), // 测试环境发布地址
  // history: createWebHashHistory("/hotelReservation/pc3/"), // 测试环境发布地址
  routes: routes,
});
export default router;
router.beforeEach(async (to, from, next) => {
  NProgress.start(); // 进度条开始
  let hasRoutes = store.state.user.hasRoutes;

  if (sessionStorage.getItem("token") && sessionStorage.getItem("user_head")) {
    if (!hasRoutes) {
      store.commit("ADDROUTE", "");
      store.commit("SET_ROUTES_STATE", true);
      next({ ...to, replace: true });
    } else {
      console.log("当前无权限信息");
    }
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next(`/login`);
    }
  }
});
router.afterEach(() => {
  NProgress.done() // 进度条结束
})
