import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import Layout from "@/layout/index.vue";
import store from "@/store";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { ElMessage, ElMessageBox } from "element-plus";
const routes = [
  // {
  //   path: "/login",
  //   component: () => import("@/views/login/index.vue"),
  // },
  // {
  //   path: "/",
  //   redirect: "/login",
  // },
  {
    path: "/process/error",
    name: "error",
    component: () => import("@/views/error/error.vue"),
    meta: { title: "无权限页面", icon: "error" },
  },
  {
    path: "/process",
    name: "process",
    component: Layout,
    children: [
      {
        path: "home",
        name: "home",
        component: () => import("@/views/homePage/home.vue"),
        meta: { title: "获取token页面", icon: "home" },
      },
      // {
      //   path: "homePage",
      //   name: "homePage",
      //   component: () => import("@/views/homePage/homePage.vue"),
      //   meta: { title: "首页", icon: "homePage" },
      // },
      // {
      //   path: "project",
      //   name: "project",
      //   component: () => import("@/views/project/project.vue"),
      //   meta: { title: "项目管理", icon: "project" },
      // },
      // {
      //   path: "role",
      //   name: "role",
      //   component: () => import("@/views/role/role.vue"),
      //   meta: { title: "角色管理", icon: "role" },
      // },
      // {
      //   path: "menu",
      //   name: "menu",
      //   component: () => import("@/views/menu/menu.vue"),
      //   meta: { title: "菜单管理", icon: "menu" },
      // },
      // {
      //   path: "user",
      //   name: "user",
      //   component: () => import("@/views/user/user.vue"),
      //   meta: { title: "用户管理", icon: "user" },
      // },
      // {
      //   path: "system",
      //   name: "system",
      //   component: () => import("@/views/system/system.vue"),
      //   meta: { title: "系统管理", icon: "system" },
      // },
    ],
  },
];

const router = createRouter({
  history: createWebHistory("/kpi"), // 发布地址
  // history: createWebHistory(""), // 发布地址
  routes: routes,
});

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

  // const token = sessionStorage.setItem(
  //   "tokenP",
  //   "VEdULTMxNzUxNC1saG13TGdUNmFsWXl1aWpFNGU0c0xKTlJheGRiRjdzRDFNa2hESzJiemxaZGRIM05XWi16ZnNvZnQuY29tIzEz"
  // );
  let hasRoutes = store.state.user.hasRoutes;
  // console.log(hasRoutes, "hasRoutes");

  if (to.path == "/process/home") {
    next();
  } else {
    if (sessionStorage.getItem("tokenP")) {
      if (!hasRoutes) {
        store.commit("SET_ROUTES_STATE", true);
        store.commit("ROLELIST", "");
        // console.log("当前有权限信息");
        next({ ...to, replace: true });
      } else {
        // console.log("当前无权限信息");
        next();
      }
    } else {
      if (to.path == "/process/error") {
        next();
      } else {
        next(`/process/error`);
      }
    }
  }
});

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

export default router;
