import Vue from "vue"; /*引入vue框架*/
import Router from "vue-router"; /*引入路由依赖*/
import layout from "@/pages/layout";

Vue.use(Router);

//获取原型对象上的push函数
const originalPush = Router.prototype.push;
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const routes = [
  {
    path: "/",
    name: "/",
    component: layout,
    meta: { title: "金桥云调解" },
    redirect: '/login',
    children: [
      {
        path: "/workbench",
        name: "/workbench",
        component: () => import("@/pages/workbench"),
        meta: { title: "调解首页", icon: 'menu-home' },
      },
      {
        path: "/workbenchcm",
        name: "/workbenchcm",
        component: () => import("@/pages/workbench/case-manage"),
        meta: { title: "调解首页", icon: 'menu-home' },
      },
      {
        path: "/case-package",
        name: "/case-package",
        component: () => import("@/pages/case-package"),
        meta: { title: "案件包管理", icon: 'menu-case-management' },
      },
      {
        path: "/case-mistake",
        name: "/case-mistake",
        component: () => import("@/pages/case-mistake"),
        meta: { title: "错误修复", icon: 'menu-case-management' },
      },
      {
        path: "/case-management",
        name: "/case-management",
        component: () => import("@/pages/case-management"),
        meta: { title: "案件管理", icon: 'menu-case-management' },
      },
      {
        path: "/case-detail",
        name: "/case-management/detail",
        component: () => import("@/pages/case-management/detail"),
        meta: { title: "案件详情", icon: 'menu-case-management' },
      },
      
      {
        path: "/statistical-analysis",
        name: "/statistical-analysis",
        component: () => import("@/pages/statistical-analysis"),
        meta: { title: "统计分析", icon: 'menu-statistical-analysis' },
      },
      
      {
        path: "/system-management",
        name: "/system-management",
        component: () => import("@/pages/system-management"),
        meta: { title: "系统管理", icon: 'menu-system-management' },
        children:[
          {
            path: "/system-management/user-management",
            name: "/system-management/user-management",
            component: () => import("@/pages/system-management/user-management"),
            meta: { title: "用户管理", icon: 'menu-user-management' },
          },
          {
            path: "/system-management/role-management",
            name: "/system-management/role-management",
            component: () => import("@/pages/system-management/role-management"),
            meta: { title: "角色管理", icon: 'menu-role-management' },
          },
          {
            path: "/system-management/role-permissions-management",
            name: "/system-management/role-permissions-management",
            component: () => import("@/pages/system-management/role-permissions-management"),
            meta: { title: "角色权限管理", icon: 'menu-role-permissions-management' },
          },
          {
            path: "/system-management/area-management",
            name: "/system-management/area-management",
            component: () => import("@/pages/system-management/area-management"),
            meta: { title: "区域管理", icon: 'menu-department-management' },
          },
          {
            path: "/system-management/department-management",
            name: "/system-management/department-management",
            component: () => import("@/pages/system-management/department-management"),
            meta: { title: "团队管理", icon: 'menu-department-management' },
          },
          {
            path: "/system-management/take-management",
            name: "/system-management/take-management",
            component: () => import("@/pages/system-management/take-management"),
            meta: { title: "坐席管理", icon: 'menu-take-management' },
          },
        ]
      },

      {
        path: "/basic-set",
        name: "/basic-set",
        component: () => import("@/pages/basic-set"),
        meta: { title: "设置", icon: 'menu-base-set' },
      },
      {
        path: "/mediation-management",
        name: "/mediation-management",
        component: () => import("@/pages/mediation-management"),
        meta: { title: "调解", icon: 'menu-mediat-management' },
      },
      {
        path: "/mediation-package",
        name: "/mediation-package",
        component: () => import("@/pages/mediation-package"),
        meta: { title: "调解管理", icon: 'menu-mediat-management' },
      },
      {
        path: "/event-tracing",
        name: "/event-tracing",
        component: () => import("@/pages/event-tracing"),
        meta: { title: "事项追踪", icon: 'menu-mediat-management' },
      },
      {
        path: "/payment-receipt",
        name: "/payment-receipt",
        component: () => import("@/pages/payment-receipt"),
        meta: { title: "还款凭证", icon: 'menu-mediat-management' },
      },

      
    ],
  },
  {
    path: "/login",
    name: "/login",
    component: () => import("@/pages/login"),
    meta: { title: "登录", notMenu: true },
  },
  {
    path: "/mediation-page",
    name: "/mediation-page",
    component: () => import("@/pages/mediation-page"),
    meta: { title: "调解记录", notMenu: true },
  },
];
const router = new Router({
  routes,
  // mode:'history'//默认的路由模式是hash，我们要去掉url中的#需要将路由模式切换为history
});

//路由拦截器
router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title  main.js  MetaInfo二选一 */
  // if (to.meta.title) {
  //   document.title = to.meta.title
  // }

  //根据字段判断是否路由过滤
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    if (localStorage.getItem("sid")) {
      //身份信息获取
      next();
    } else {
      next({
        path: "/login",
      });
    }
  } else {
    next();
  }
});

export default router;
