import Vue from "vue";
import VueRouter from "vue-router";
import Layouts from "@/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: "/sso",
    children: [
      {
        path: "/home",
        hideChildrenInMenu: true,
        component: () => import("@/pages/home/Home"),
        meta: {
          title: "数仓工作台",
        },
      },
      {
        path: "/engine",
        hideChildrenInMenu: true,
        component: () => import("@/pages/engine/Engine"),
        meta: {
          title: "数据引擎",
        },
        redirect: "/engine/queueupsystem",
        children: [
          {
            path: "/engine/queueupsystem",
            hideChildrenInMenu: true,
            hidden: true,
            component: () => import("@/pages/engine/QueueUpSystem"),
            meta: {
              title: "排队取号系统",
              activeMenu: "/engine",
            },
          },
          {
            path: "/engine/evaluatesystem",
            hideChildrenInMenu: true,
            hidden: true,
            component: () => import("@/pages/engine/EvaluateSystem"),
            meta: {
              title: "评价系统",
              activeMenu: "/engine",
            },
          },
        ],
      },
      // 数据搜索引擎
      {
        path: "/enginesearch",
        component: () => import("@/pages/engineSearch/EngineSearch"),
        hidden: true,
        redirect: "/enginesearch/queueupdata",
        children: [
          {
            path: "/enginesearch/queueupdata",
            hideChildrenInMenu: true,
            hidden: true,
            component: () => import("@/pages/engineSearch/QueueUpData"),
            meta: {
              title: "排队数据",
              activeMenu: "/engine",
              parentPath: "/enginesearch",
            },
          },
          {
            path: "/enginesearch/evaluatedata",
            hideChildrenInMenu: true,
            hidden: true,
            component: () => import("@/pages/engineSearch/EvaluateData"),
            meta: {
              title: "评价数据",
              activeMenu: "/engine",
              parentPath: "/enginesearch",
            },
          },
        ],
      },

      {
        path: "/market",
        name: "market",
        hideChildrenInMenu: true,
        component: () => import("@/pages/market/Market"),
        meta: {
          title: "数据集市",
        },
        redirect: "/market/queueupreport",
        children: [
          {
            path: "/market/queueupreport",
            name: "queueupreport",
            hideChildrenInMenu: true,
            component: () => import("@/pages/market/QueueUpReport"),
            meta: {
              title: "排队数据报表",
              activeMenu: "/market",
              icon: "el-icon-guide",
            },
          },
          {
            path: "/market/evaluatereport",
            name: "evaluatereport",
            hideChildrenInMenu: true,
            component: () => import("@/pages/market/EvaluateReport"),
            meta: {
              title: "评价数据报表",
              activeMenu: "/market",
              icon: "el-icon-collection",
            },
          },
        ],
      },
      {
        path: "/system",
        hideChildrenInMenu: true,
        component: () => import("@/pages/system/System"),
        meta: {
          title: "系统设置",
        },
        redirect: "/system/access",
        children: [
          {
            path: "/system/access",
            component: () => import("@/pages/system/access/Access.vue"),
            meta: {
              activeMenu: "/system",
              title: "区县接入",
              icon: "el-icon-set-up",
            },
          },
          {
            path: "/system/user",
            component: () => import("@/pages/system/user/User.vue"),
            meta: {
              activeMenu: "/system",
              title: "用户信息",
              icon: "el-icon-user",
            },
          },
          {
            path: "/system/role",
            component: () => import("@/pages/system/role/Role.vue"),
            meta: {
              activeMenu: "/system",
              title: "角色信息",
              icon: "el-icon-postcard",
            },
          },
          {
            path: "/system/resourcemanage",
            component: () =>
              import("@/pages/system/resourceManage/ResourceManage.vue"),
            meta: {
              activeMenu: "/system",
              title: "资源信息",
              icon: "el-icon-box",
            },
          },
          {
            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: "/sso",
    name: "跳转页",
    hidden: true,
    component: () => import("@/pages/sso/Sso"),
  },
  {
    path: "/login",
    name: "登录页",
    hidden: true,
    component: () => import("@/pages/login/Login"),
  },
];

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

// 前置路由卫士
// router.beforeEach((to, from, next) => {
//   let islogin = store.getters["user/token"];
//   if (islogin) {
//     next();
//   } else {
//     if (to.path === "/sso") {
//       next();
//     } else {
//       location.href = process.env.VUE_APP_API_portal_URL;
//     }
//   }
// });

// 过滤菜单
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("user/SET_menus", menus);
}
calcMenu();

export default router;
