import Vue from "vue";
import VueRouter from "vue-router";
import Layouts from "@/pages/layouts/Layouts.vue";
import store from "@/store";
// import local from "@/utils/local";
// 解决重复点击同一个路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);
/**
 *  Route 对象配置:
 * hidden：控制路由是否显示在在菜单
 * hideChildrenInMenu:强制菜单显示为Item而不是SubItem
 * meta对象配置:
 * title:标题
 * keepAlive:缓存该路由
 * activeMenu:当前的激活路由
 * icon:图标
 * hidden：控制路由是否显示在在菜单
 */
const routes = [
  {
    path: "/",
    component: Layouts,
    redirect: "/jump",
    children: [
      {
        path: "/basicsset",
        // name: '样表基础设置',
        hideChildrenInMenu: true,
        component: () => import("@/pages/software/basics/BasicsSet"),
        meta: {
          icon: "el-icon-setting",
          title: "样表基础设置",
        },
      },
      {
        path: "/mattermanage",
        // name: "样表事项管理",
        hideChildrenInMenu: true,
        component: () => import("@/pages/software/matter/MatterManage"),
        meta: {
          icon: "el-icon-document",
          title: "样表事项管理",
        },
      },
      {
        path: "/materialsmanage",
        // name: '样表管理',
        hideChildrenInMenu: true,
        component: () => import("@/pages/software/materials/MaterialsManage"),
        meta: {
          icon: "el-icon-files",
          title: "样表管理",
        },
      },
      {
        path: "/librarymanage",
        // name: '公共库管理',
        hideChildrenInMenu: true,
        component: () => import("@/pages/software/librarymanage/LibraryManage"),
        meta: {
          icon: "el-icon-refrigerator",
          title: "公共库管理",
        },
      },
      {
        path: "/skinmanage",
        hideChildrenInMenu: true,
        component: () => import("@/pages/software/skinManage/SkinManage"),
        meta: {
          icon: "el-icon-orange",
          title: "皮肤管理",
        },
      },

      {
        path: "/numberwritedevice",
        // name: '设备管理',
        hideChildrenInMenu: true,
        component: () => import("@/pages/hardware/NumberWriteDevice"),
        meta: {
          icon: "el-icon-monitor",
          title: "设备管理",
        },
      },

      {
        path: "/system",
        hideChildrenInMenu: true,
        component: () => import("@/pages/system/System.vue"),
        redirect: "/system/parameter",
        meta: {
          icon: "el-icon-data-board",
          title: "系统设置",
        },
        children: [
          {
            path: "/system/parameter",
            component: () => import("@/pages/system/parameter/Parameter.vue"),
            meta: {
              activeMenu: "/system",
              title: "系统参数",
              icon: "el-icon-cpu",
            },
          },
          {
            path: "/system/task",
            component: () => import("@/pages/system/task/TaskSet.vue"),
            meta: {
              activeMenu: "/system",
              title: "任务信息",
              icon: "el-icon-bank-card",
            },
          },
          {
            path: "/system/systemlogs",
            component: () => import("@/pages/system/systemlogs/SystemLogs.vue"),
            meta: {
              activeMenu: "/system",
              title: "系统日志",
              icon: "el-icon-notebook-1",
            },
          },
        ],
      },
    ],
  },
  {
    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 = store.getters.token;
  if (islogin) {
    next();
  } else {
    if (to.path === "/jump") {
      next();
    } else {
      location.href = process.env.VUE_APP_API_portal_URL;
      // 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"),
//       },
//     ],
//   },
//   {
//     path: "/system",
//     component: Layouts,
//     meta: {
//       icon: "el-icon-data-board",
//       title: "系统设置",
//     },
//     children: [
//       {
//         path: "",
//         component: () => import("@/pages/system/System.vue"),
//         redirect: "/system/parameter",
//         children: [
//           {
//             path: "/system/parameter",
//             component: () => import("@/pages/system/parameter/Parameter.vue"),
//             meta: {
//               activeMenu: "/system",
//               hidden: true,
//             },
//           },
//           {
//             path: "/system/task",
//             component: () => import("@/pages/system/task/TaskSet.vue"),
//             meta: {
//               activeMenu: "/system",
//               hidden: true,
//             },
//           },
//           {
//             path: "/system/systemlogs",
//             component: () => import("@/pages/system/systemlogs/SystemLogs.vue"),
//             meta: {
//               activeMenu: "/system",
//               hidden: true,
//             },
//           },
//         ],
//       },
//     ],
//   },
// ];

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

export default router;
