// src/router/index.ts
import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css"; // 引入 NProgress 的 CSS 样式文件
import { ElMessage } from "element-plus";
const routes = [
  {
    path: "/login",
    name: "login",
    meta: { title: "", icon: "", showHeader: false },
    component: () => import("@/views/login/login.vue"),
  },
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/log",
    component: () => import("@/views/log/log.vue"),
    meta: { title: "", icon: "", showHeader: true },
  },
  // {
  //   path: "/:pathMatch(.*)*",
  //   redirect: "/404",
  // },
  // {
  //   path: "/dataOverview",
  //   component: () => import("@/views/dataOverview/dataOverview.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/studentInfo",
  //   component: () => import("@/views/studentInfo/studentInfo.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/building",
  //   component: () => import("@/views/building/building.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/dormitory",
  //   component: () => import("@/views/dormitory/dormitory.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/bed",
  //   component: () => import("@/views/bed/bed.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/student",
  //   component: () => import("@/views/student/student.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/quarterage",
  //   component: () => import("@/views/quarterage/quarterage.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/caller",
  //   component: () => import("@/views/caller/caller.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/user",
  //   component: () => import("@/views/user/user.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/role",
  //   component: () => import("@/views/role/role.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
  // {
  //   path: "/system",
  //   component: () => import("@/views/system/system.vue"),
  //   meta: { title: "", icon: "", showHeader: true },
  // },
];
const router = createRouter({
  history: createWebHashHistory("/welcome/welcomeWeb/"),
  routes,
});

// 配置 NProgress
NProgress.configure({ showSpinner: false }); // 根据需要配置选项

// 在路由守卫中启动和停止 NProgress
router.beforeEach(async (to, from, next) => {
  NProgress.start(); // 开始加载进度条

  let token = localStorage.getItem("token");
  if (to.path == "/login") {
    next();
  } else {
    if (token) {
      next();
    } else {
      // ElMessage({
      //   type: "error",
      //   showClose: true,
      //   message: "无权限进入此页面",
      //   center: true,
      // });
      localStorage.removeItem("token");
      localStorage.removeItem("userName");
      sessionStorage.removeItem("accountId");
      sessionStorage.removeItem("sidevarItem");
      next("/login");
    }
  }
});
router.afterEach(() => {
  NProgress.done(); // 结束加载进度条
});
export default router;
