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: "/wanzai",
    name: "wanzai",
    component: Layout,
    children: [
      {
        path: "home",
        name: "home",
        meta: { isAuth: true, title: "系统首页" },
        component: () => import("@/views/home/home.vue"),
      },
      // {
      //   path: "user",
      //   name: "user",
      //   meta: { isAuth: true, title: "用户管理" },
      //   component: () => import("@/views/user/user.vue"),
      // },
      // {
      //   path: "identity",
      //   name: "identity",
      //   meta: { isAuth: true, title: "身份管理" },
      //   component: () => import("@/views/identity/identity.vue"),
      // },
      // {
      //   path: "class",
      //   name: "class",
      //   meta: { isAuth: true, title: "班级管理" },
      //   component: () => import("@/views/class/class.vue"),
      // },
      // {
      //   path: "grades",
      //   name: "grades",
      //   meta: { isAuth: true, title: "年级管理" },
      //   component: () => import("@/views/grades/grades.vue"),
      // },
      // {
      //   path: "post",
      //   name: "post",
      //   meta: { isAuth: true, title: "职务管理" },
      //   component: () => import("@/views/post/post.vue"),
      // },
      // {
      //   path: "subject",
      //   name: "subject",
      //   meta: { isAuth: true, title: "学科管理" },
      //   component: () => import("@/views/subject/subject.vue"),
      // },
      // {
      //   path: "semester",
      //   name: "semester",
      //   meta: { isAuth: true, title: "学期管理" },
      //   component: () => import("@/views/semester/semester.vue"),
      // },
      // {
      //   path: "exam",
      //   name: "exam",
      //   meta: { isAuth: true, title: "考试类型管理" },
      //   component: () => import("@/views/exam/exam.vue"),
      // },
      // {
      //   path: "apply",
      //   name: "apply",
      //   meta: { isAuth: true, title: "应用管理" },
      //   component: () => import("@/views/apply/apply.vue"),
      // },
      // {
      //   path: "energy",
      //   name: "energy",
      //   meta: { isAuth: true, title: "能耗管理" },
      //   component: () => import("@/views/energy/energy.vue"),
      // },
      // {
      //   path: "safety",
      //   name: "safety",
      //   meta: { isAuth: true, title: "校园安全" },
      //   component: () => import("@/views/safety/safety.vue"),
      // },
      // {
      //   path: "caller",
      //   name: "caller",
      //   meta: { isAuth: true, title: "访客预约" },
      //   component: () => import("@/views/caller/caller.vue"),
      // },
      // {
      //   path: "grade",
      //   name: "grade",
      //   meta: { isAuth: true, title: "成绩管理" },
      //   component: () => import("@/views/grade/grade.vue"),
      // },
      // {
      //   path: "leave",
      //   name: "leave",
      //   meta: { isAuth: true, title: "请假管理" },
      //   component: () => import("@/views/leave/leave.vue"),
      // },
      // {
      //   path: "teacher",
      //   name: "teacher",
      //   meta: { isAuth: true, title: "教师" },
      //   component: () => import("@/views/teacher/teacher.vue"),
      // },
      // {
      //   path: "student",
      //   name: "student",
      //   meta: { isAuth: true, title: "学生" },
      //   component: () => import("@/views/student/student.vue"),
      // },
      // {
      //   path: "info",
      //   name: "info",
      //   meta: { isAuth: true, title: "基本信息" },
      //   component: () => import("@/views/info/info.vue"),
      // },
      // {
      //   path: "screenShot",
      //   name: "screenShot",
      //   meta: { isAuth: true, title: "截屏风险" },
      //   component: () => import("@/views/screenShot/screenShot.vue"),
      // },
      // {
      //   path: "roles",
      //   name: "roles",
      //   meta: { isAuth: true, title: "权限管理" },
      //   component: () => import("@/views/roles/roles.vue"),
      // },
      // {
      //   path: "source",
      //   name: "source",
      //   meta: { isAuth: true, title: "我的数据源" },
      //   component: () => import("@/views/source/source.vue"),
      // },
      // {
      //   path: "task",
      //   name: "task",
      //   meta: { isAuth: true, title: "任务管理" },
      //   component: () => import("@/views/task/task.vue"),
      // },
      // {
      //   path: "log",
      //   name: "log",
      //   meta: { isAuth: true, title: "业务日志" },
      //   component: () => import("@/views/log/log.vue"),
      // },
    ],
  },
  
];
const router = createRouter({
  // history: createWebHistory("/smartManage"), // 线上地址
  history: createWebHashHistory("/smartManage"), // 测试环境发布地址
  routes: routes,
});

router.beforeEach(async (to, from, next) => {
  NProgress.start(); // 进度条开始

  // let userhead = "1B0430E15102ADC65C43E475AC697D9A";
  // let token =
  //   "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTMyMzQ2NDQsImNhcmRObyI6IjAwMDAwMSIsImlhdCI6MTcxMzE0ODI0NH0.jxuGCqDkqzRa3poVNQq5aoSrKGDCZauXFLVKrB4M87s";
  // sessionStorage.setItem("token", token);
  // sessionStorage.setItem("userhead", userhead);

  let hasRoutes = store.state.user.hasRoutes;
  if(to.path == "/wanzai/home"){
    next()
  }else{
    if (
      sessionStorage.getItem("token") &&
      sessionStorage.getItem("userhead") 
    ) {
      if (!hasRoutes) {
        store.commit("ROLELIST", "");
        store.commit("SET_ROUTES_STATE", true);
        next({ ...to, replace: true });
      } else {
        console.log("当前无权限信息");
      }
      next();
    } else {
      if (to.path == "/login") {
        next();
      } else {
        next(`/login`);
      }
    }
  }
  // next();
});

router.afterEach(() => {
  NProgress.done(); // 进度条结束
});
export default router;
