import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect'),
      },
    ],
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true,
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true,
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    hidden: true,
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true,
  },
  {
    path: '/screen',
    component: () => import('@/views/screen/data/index'),
    hidden: true,
  },
  {
    path: '',
    component: Layout,
    redirect: 'index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/index'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true },
      },
    ],
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' },
      },
    ],
  },
  {
    path: '/notice',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'noticeAnnouncement',
        component: () => import('@/views/IndexPage/noticeAnnouncement'),
        name: 'NoticeAnnouncement',
        meta: { title: '通知公告', icon: 'notice' },
      },
    ],
  },
  {
    path: '/myToDoList',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'myToDoList',
        component: () => import('@/views/IndexPage/myToDoList'),
        name: 'MyToDoList',
        meta: { title: '我的待办', icon: 'notice' },
      },
    ],
  },
  {
    path: '/takePhoto',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'takePhoto',
        component: () => import('@/views/IndexPage/takePhoto'),
        name: 'takePhoto',
        meta: { title: '我的随手拍', icon: 'takePhoto' },
      },
    ],
  },
]

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user' },
      },
    ],
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role' },
      },
    ],
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict' },
      },
    ],
  },
  {
    path: '/monitor/job-log',
    component: Layout,
    hidden: true,
    permissions: ['monitor:job:list'],
    children: [
      {
        path: 'index/:jobId(\\d+)',
        component: () => import('@/views/monitor/job/log'),
        name: 'JobLog',
        meta: { title: '调度日志', activeMenu: '/monitor/job' },
      },
    ],
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen' },
      },
    ],
  },
  // {
  //   path: '/system/pro-auth',
  //   component: Layout,
  //   hidden: true,
  //   permissions: ['system:proList:add'],
  //   children: [
  //     {
  //       path: 'pro/:proId(\\d+)',
  //       component: () => import('@/views/pro/proList/addPro'),
  //       name: 'AuthPro',
  //       meta: { title: '新建工程', activeMenu: '/system/pro' }
  //     }
  //   ]
  // },
  {
    path: '/project/pro-auth/projectManagement/proList',
    component: Layout,
    hidden: true,
    permissions: ['system:proList:add'],
    children: [
      {
        path: 'addPro/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/proList/addPro'),
        name: 'AuthPro',
        meta: { title: '新建工程', activeMenu: '/project/projectManagement/proList/index' },
      },
    ],
  },
  {
    path: '/project/pro-auth/projectManagement/proList',
    component: Layout,
    hidden: true,
    permissions: ['system:proList:edit'],
    children: [
      {
        path: 'editPro/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/proList/addPro'),
        name: 'AuthEditPro',
        meta: { title: '修改工程', activeMenu: '/project/projectManagement/proList/index' },
      },
    ],
  },
  {
    path: '/project/outsourcing-supervisor-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:outsourcingPro:query'],
    children: [
      {
        path: 'supervisorInfo/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/outsourcingPro/supervisorInfo'),
        name: 'supervisorInfo',
        meta: { title: '监理信息', activeMenu: '/project/projectManagement/outsourcingPro' },
      },
    ],
  },
  {
    path: '/pro/supervisor/supervisedAdmission',
    component: Layout,
    hidden: true,
    permissions: ['system:supervisorApproval:query'],
    children: [
      {
        path: 'supervisorApproval/:data(.*)',
        component: () => import('@/views/pro/supervisor/supervisedAdmission/supervisorApproval'),
        name: 'AuthSupervisorApproval',
        meta: { title: '监理入场审核', activeMenu: '/system/supervisorApproval' },
      },
    ],
  },
  {
    path: '/project/projectManagements/outsourcingPro',
    component: Layout,
    hidden: true,
    permissions: ['system:outsourcingPro:edit'],
    children: [
      {
        path: 'editOutsourcingPro/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/outsourcingPro/editOutsourcingPro'),
        name: 'editOutsourcingPro',
        meta: { title: '工程基本信息', activeMenu: '/project/projectManagement' },
      },
    ],
  },
  {
    path: '/project/outsourcing-consDetailsInfo-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:outsourcingPro:query'],
    children: [
      {
        path: 'consInfo/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/outsourcingPro/consDetailsInfo'),
        name: 'consDetailsInfo',
        meta: { title: '承包商信息', activeMenu: '/project/projectManagement/outsourcingPro' },
      },
    ],
  },
  {
    path: '/system/outsourcing-subDetailsInfo-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:outsourcingPro:cons'],
    children: [
      {
        path: 'subDetailsInfo/:data(.*)',
        component: () => import('@/views/pro/projectManagement/outsourcingPro/subDetailsInfo'),
        name: 'AuthSubInfo',
        meta: { title: '分包商信息', activeMenu: '/system/subDetailsInfo' },
      },
    ],
  },
  {
    path: '/project/outsourcing-pro-profile-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:outsourcingPro:query'],
    children: [
      {
        path: 'proProfile/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/outsourcingPro/proProfile'),
        name: 'proProfile',
        meta: { title: '工程资料信息', activeMenu: '/project/projectManagement/outsourcingPro' },
      },
    ],
  },
  {
    path: '/pro/projectManagement/constructionPro',
    component: Layout,
    hidden: true,
    permissions: ['system:constructionPro:edit'],
    children: [
      {
        path: 'editConstructionPro',
        component: () => import('@/views/pro/projectManagement/constructionPro/editOutsourcingPro'),
        name: 'AuthEditConstructionPro',
        meta: { title: '工程基本信息', activeMenu: '/system/editConstructionPro' },
      },
    ],
  },
  {
    path: '/project/constructionPro-consDetailsInfo-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:constructionPro:query'],
    children: [
      {
        path: 'ConstructionProConsInfo/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/constructionPro/consDetailsInfo'),
        name: 'AuthConstructionProConsInfo',
        meta: { title: '承包商信息', activeMenu: '/project/projectManagement/constructionPro' },
      },
    ],
  },
  {
    path: '/system/constructionPro-subDetailsInfo-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:constructionPro:cons'],
    children: [
      {
        path: 'ConstructionProSubDetailsInfo/:id(\\d+)/:proId(\\d+)/:consId(\\d+)',
        component: () => import('@/views/pro/projectManagement/constructionPro/subDetailsInfo'),
        name: 'AuthConstructionProSubInfo',
        meta: { title: '分包商信息', activeMenu: '/system/ConstructionProSubDetailsInfo' },
      },
    ],
  },
  {
    path: '/project/constructionPro-pro-profile-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:constructionPro:query'],
    children: [
      {
        path: 'constructionProProProfile/:proId(\\d+)',
        component: () => import('@/views/pro/projectManagement/constructionPro/proProfile'),
        name: 'AuthConstructionProProProfile',
        meta: { title: '工程资料信息', activeMenu: '/project/projectManagement/constructionPro' },
      },
    ],
  },
  {
    path: '/project/admission-request-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:addPersonnel:query'],
    children: [
      {
        path: 'addPersonnel/:data(.*)',
        component: () => import('@/views/pro/admissionRequest/addPersonnel'),
        name: 'AuthAddPersonnel',
        meta: { title: '人员入场申请管理', activeMenu: '/project/personnel/admissionRequest' },
      },
    ],
  },

  {
    path: '/system/engineer-equipment-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:engineeringEquipment:query'],
    children: [
      {
        path: 'bindingInformation/:proId(\\d+)',
        component: () => import('@/views/terminalEquipment/engineeringEquipmentBinding/bindingInformation'),
        name: 'bindingInformation',
        meta: { title: '工程设备管理', activeMenu: '/system/bindingInformation' },
      },
    ],
  },

  //施工材料审批页面
  {
    path: '/project/construction_materials_approval',
    component: Layout,
    hidden: true,
    permissions: ['system:engineeringEquipment:listConstructionMaterial'],
    children: [
      {
        path: 'approvalPage/:data(.*)',
        component: () => import('@/views/worksUnderConstruction/cMaterialsApproval/approvalPage'),
        name: 'approvalPage',
        meta: {
          title: '施工材料页面',
          activeMenu: '/project/project_approval_during_construction/pro/approval/list/consMaterialApprovalList',
        },
      },
    ],
  },

  {
    path: '/pro/approval',
    component: Layout,
    hidden: true,
    permissions: ['system:listSubEntry:list'],
    children: [
      {
        path: 'subApproval/:data(.*)',
        component: () => import('@/views/pro/approval/subApproval'),
        name: 'subApproval',
        meta: { title: '入场审批', activeMenu: '/system/subApproval' },
      },
    ],
  },
  {
    path: '/pro/approval',
    component: Layout,
    hidden: true,
    permissions: ['system:listSubEntry:list'],
    children: [
      {
        path: 'subOutApproval/:data(.*)',
        component: () => import('@/views/pro/approval/subOutApproval'),
        name: 'subOutApproval',
        meta: { title: '出场审批', activeMenu: '/system/subOutApproval' },
      },
    ],
  },
  {
    path: '/system/cons-approval-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:consApproval:approval'],
    children: [
      {
        path: 'consEntranceApproval/:data(.*)',
        component: () => import('@/views/pro/preCommencementWorks/ContractorAdmissionQualification/consDetailsInfo'),
        name: 'AuthConsEntranceApproval',
        meta: { title: '承包商入场资质审批', activeMenu: '/system/consEntranceApproval' },
      },
    ],
  },
  {
    path: '/system/subManagement-personApproval-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:subPersonApply:list'],
    children: [
      {
        path: 'personApproval/:data(.*)',
        component: () => import('@/views/pro/cons/subManagement/approval/personApproval'),
        name: 'AuthPersonApproval',
        meta: { title: '人员审批', activeMenu: '/system/personApproval' },
      },
    ],
  },
  {
    path: '/system/person-approval-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:outsourcingPro:cons'],
    children: [
      {
        path: 'personApprovals/:data(.*)',
        component: () => import('@/views/pro/cons/subManagement/approval/personApproval'),
        name: 'AuthPersonApprovals',
        meta: { title: '人员审批', activeMenu: '/system/personApprovals' },
      },
    ],
  },
  {
    path: '/system/subManagement-personOutApproval-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:outsourcingPro:cons'],
    children: [
      {
        path: 'personOutApproval/:id(\\d+)/:proId(\\d+)/:consId(\\d+)/:subId(\\d+)/:type(\\d+)',
        component: () => import('@/views/pro/cons/subManagement/approval/personOutApproval.vue'),
        name: 'AuthPersonOutApproval',
        meta: { title: '人员审批', activeMenu: '/system/personOutApproval' },
      },
    ],
  },
  {
    path: '/pro/apply',
    component: Layout,
    hidden: true,
    permissions: ['system:consMaterial:add'],
    children: [
      {
        path: 'consMaterialApply/:data(.*)',
        component: () => import('@/views/pro/apply/consMaterialApply'),
        name: 'consMaterialApply',
        meta: { title: '施工材料编辑', activeMenu: '/system/consMaterialApply' },
      },
    ],
  },
  //首页出场申请页面
  {
    path: '/pro/apply',
    component: Layout,
    hidden: true,
    permissions: ['system:complete:btn'],
    children: [
      {
        path: 'subAppearingApply/:data(.*)',
        component: () => import('@/views/pro/apply/subAppearingApply'),
        name: 'subAppearingApply',
        meta: { title: '出场申请', activeMenu: '/system/subAppearingApply' },
      },
    ],
  },
  //首页完工申请页面
  {
    path: '/pro/apply',
    component: Layout,
    hidden: true,
    permissions: ['system:complete:btn'],
    children: [
      {
        path: 'consCompleteApply/:data(.*)',
        component: () => import('@/views/pro/apply/consCompleteApply'),
        name: 'consCompleteApply',
        meta: { title: '完工申请', activeMenu: '/system/consCompleteApply' },
      },
    ],
  },

  //首页入场申请页面
  {
    path: '/pro/apply',
    component: Layout,
    hidden: true,
    permissions: ['system:entrance:apply'],
    children: [
      {
        path: 'consEnterApply/:data(.*)',
        component: () => import('@/views/pro/apply/consEnterApply'),
        name: 'consEnterApply',
        meta: { title: '入场申请', activeMenu: '/system/consEnterApply' },
      },
    ],
  },
  //监理入场申请
  {
    path: '/pro/apply',
    component: Layout,
    hidden: true,
    permissions: ['system:entrance:apply'],
    children: [
      {
        path: 'supervisorEnterApply/:data(.*)',
        component: () => import('@/views/pro/apply/supervisorEnterApply'),
        name: 'supervisorEnterApply',
        meta: { title: '监理入场申请', activeMenu: '/system/supervisorEnterApply' },
      },
    ],
  },
  //工器具入场申请编辑页面
  {
    path: '/pro/apply',
    component: Layout,
    hidden: true,
    permissions: ['system:equip:add'],
    children: [
      {
        path: 'consEquipApply/:data(.*)',
        component: () => import('@/views/pro/apply/consEquipApply'),
        name: 'consEquipApply',
        meta: { title: '工器具编辑', activeMenu: '/system/consEquipApply' },
      },
    ],
  },

  //工程资料上传页面
  {
    path: '/project/proMaterialManagement-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:statistic:jump'],
    children: [
      {
        path: 'uploadMaterialPage/:proId(\\d+)',
        component: () => import('@/views/proMaterialManagement/upload/index'),
        name: 'uploadMaterialPage',
        meta: { title: '工程资料上传', activeMenu: '/project/proMaterialManagement/statistic' },
      },
    ],
  },
  //上传日常资料页面
  {
    path: '/project/proMaterialManagement-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:statistic:uploadRcMaterial'],
    children: [
      {
        path: 'uploadRcMaterial/:proId(\\d+)',
        component: () => import('@/views/proMaterialManagement/upload/rcMaterial'),
        name: 'uploadRcMaterial',
        meta: { title: '上传日常资料', activeMenu: '/project/proMaterialManagement/statistic' },
      },
    ],
  },
  //上传交底资料页面
  {
    path: '/project/proMaterialManagement-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:statistic:uploadJdMaterial'],
    children: [
      {
        path: 'uploadJdMaterial/:proId(\\d+)',
        component: () => import('@/views/proMaterialManagement/upload/jdMaterial'),
        name: 'uploadJdMaterial',
        meta: { title: '上传交底资料', activeMenu: '/project/proMaterialManagement/statistic' },
      },
    ],
  },

  //工机具中途入场审批页面
  {
    path: '/project/midEntry-component-auth',
    component: Layout,
    hidden: true,
    permissions: ['project:entranceOfEquipment:list'],
    children: [
      {
        path: 'toolEntryReviewComponent/:data(.*)',
        component: () => import('@/views/midEntryComponent/toolEntryReviewComponent'),
        name: 'toolEntryReviewComponent',
        meta: {
          title: '工器具审核',
          activeMenu: '/project/project_approval_during_construction/pro/approval/list/consEquipApproval',
        },
      },
    ],
  },
  //开工申请页面
  {
    path: '/pro/apply',
    component: Layout,
    hidden: true,
    permissions: ['system:startWork:edit'],
    children: [
      {
        path: 'consStartWorkApply/:data(.*)',
        component: () => import('@/views/pro/apply/consStartWorkApply'),
        name: 'consStartWorkApplyList',
        meta: { title: '开工申请页面', activeMenu: '/system/consStartWorkApply' },
      },
    ],
  },
  //开工审批-审批页面
  {
    path: '/pro/approval',
    component: Layout,
    hidden: true,
    permissions: ['system:listCommencementApproval:list'],
    children: [
      {
        path: 'consStartWorkApproval/:data(.*)',
        component: () => import('@/views/pro/approval/consStartWorkApproval'),
        name: 'consStartWorkApproval',
        meta: { title: '开工审批页面', activeMenu: '/system/consStartWorkApproval' },
      },
    ],
  },
  //承包商完工申请-审批页面
  {
    path: '/system/cons-complete-approval-auth/pro/approval',
    component: Layout,
    hidden: true,
    permissions: ['system:listCompleteApproval:list'],
    children: [
      {
        path: 'consCompleteApproval/:data(.*)',
        component: () => import('@/views/pro/approval/consCompleteApproval'),
        name: 'consCompleteApproval',
        meta: { title: '完工审批', activeMenu: '/system/consCompleteApproval' },
      },
    ],
  },

  //审核流-新增审核流
  {
    path: '/system/flowable/add-flowable-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:proList:list'],
    children: [
      {
        path: 'addFlowable/:data(.*)',
        component: () => import('@/views/flow/definition/design/ProcessDesign'),
        name: 'addFlowable',
        meta: { title: '审核流配置', activeMenu: '/system/addFlowable' },
      },
    ],
  },
  {
    path: '/flow',
    component: Layout,
    hidden: true,
    permissions: ['system:audit:query'],
    children: [
      {
        path: 'auditRecord/:data(.*)',
        component: () => import('@/views/flow/auditRecord'),
        name: 'auditRecord',
        meta: { title: '审批记录', activeMenu: '/system/auditRecord' },
      },
    ],
  },
  {
    path: '/basics/disposition',
    component: Layout,
    hidden: true,
    permissions: ['system:audit:query'],
    children: [
      {
        path: 'auditProcess/:data(.*)',
        component: () => import('@/views/flow/definition/auditProcess'),
        name: 'auditProcess',
        meta: { title: '审批流配置', activeMenu: '/system/auditProcess' },
      },
    ],
  },
]

// 防止连续点击多次路由报错
let routerPush = Router.prototype.push
let routerReplace = Router.prototype.replace
// push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}
// replace
Router.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch(err => err)
}

const createRouter = () =>
  new Router({
    mode: 'history', // 去掉url中的#
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
    base: process.env.VUE_APP_ENV === 'testing' ? '/nxdt' : '',
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
