import Vue from "vue";
import VueRouter from "vue-router";
import Layouts from "@/pages/layouts/Layouts.vue";
import store from "@/store";
// 解决重复点击同一个路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);

const routes = [
  {
    path: "/jump",
    name: "跳转页",
    component: () => import("@/pages/jump/jump"),
  },
  {
    path: "/",
    redirect: "/jump",
  },
  {
    path: "/login",
    name: "登录页",
    component: () => import("@/pages/login/login"),
  },
];

const router = new VueRouter({
  routes,
});

// 前置路由卫士
router.beforeEach((to, from, next) => {
  let islogin = local.getLocal("sampleToken") ? true : false;
  if (islogin) {
    next();
  } else {
    location.href = process.env.VUE_APP_API_portal_URL + "/#/";
    // 再次判断防止死循环
    // if (to.path === "/login") {
    //   next();
    // } else {
    //   next({ path: "/login" });
    // }
  }
});

// 动态菜单
const dynamicRouter = [
  {
    path: "/basicsset",
    component: Layouts,
    meta: {
      icon: "el-icon-setting",
      title: "样表基础设置",
    },
    children: [
      {
        path: "",
        // name: '样表基础设置',
        component: () => import("@/pages/software/basics/BasicsSet"),
      },
    ],
  },
  {
    path: "/mattermanage",
    component: Layouts,
    meta: {
      icon: "el-icon-document",
      title: "样表事项管理",
    },
    children: [
      {
        path: "",
        name: "样表事项管理",
        component: () => import("@/pages/software/matter/MatterManage"),
      },
    ],
  },
  {
    path: "/materialsmanage",
    component: Layouts,
    meta: {
      icon: "el-icon-files",
      title: "样表管理",
    },
    children: [
      {
        path: "",
        // name: '材料管理',
        component: () => import("@/pages/software/materials/MaterialsManage"),
      },
    ],
  },
  {
    path: "/librarymanage",
    component: Layouts,
    meta: {
      icon: "el-icon-refrigerator",
      title: "公共库管理",
    },
    children: [
      {
        path: "",
        // name: '公共库管理',
        component: () => import("@/pages/software/librarymanage/LibraryManage"),
      },
    ],
  },
  {
    path: "/skinmanage",
    component: Layouts,
    meta: {
      icon: "el-icon-orange",
      title: "皮肤管理",
    },
    children: [
      {
        path: "",
        component: () => import("@/pages/software/skinManage/SkinManage"),
      },
    ],
  },
  {
    path: "/numberwritedevice",
    component: Layouts,
    meta: {
      icon: "el-icon-monitor",
      title: "数字样表设备",
    },
    children: [
      {
        path: "",
        // name: '数字样表设备',
        component: () => import("@/pages/hardware/NumberWriteDevice"),
      },
    ],
  },
];

// 过滤菜单
function menusFilter(arr) {
  let router = arr.filter((v) => {
    if (v.meta && v.meta.title) {
      if (v.children) {
        v.children = menusFilter(v.children);
      }
      return v;
    }
  });
  return router;
}
// 动态菜单
export function calcMenu() {
  dynamicRouter.forEach((v) => {
    router.addRoute(v);
  });
  let menus = menusFilter(dynamicRouter);
  store.commit("SET_MENUS", menus);
}
calcMenu();

export default router;