import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

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

export const constantRoutes = [
  {
    path: '/sso',
    component: () => import('@/views/login/sso.vue'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/signin-oidc',
    name: 'signin-oidc',
    component: () => import('@/views/login/signin-oidc.vue'),
    hidden: true
  },
  {
    path: '/silent-signin-oidc',
    name: 'silent-signin-oidc',
    component: () => import('@/views/login/silent-signin-oidc.vue'),
    hidden: true
  },
  {
    path: '/signout-callback-oidc',
    name: 'signout-callback-oidc',
    component: () => import('@/views/login/signout-callback-oidc.vue'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401.vue'),
    hidden: true
  },
  {
    path: '/gride-work-info',
    name: 'gride-work-info',
    component: () => import('@/views/grider-erections/gride-work-info/tky-index'),
    hidden: true
  },
  {
    path: '/beam-carrier-work-info',
    name: 'beam-carrier-work-info',
    component: () => import('@/views/grider-erections/beam-carrier-work-info/tky-index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', affix: true }
      },
      {
        path: 'todo',
        component: () => import('@/views/todo/index.vue'),
        name: 'Todo',
        hidden: true,
        meta: { title: '待办任务', icon: 'documentation' }
      }
    ]
  },
  {
    path: '*',
    component: () => import('@/views/error-page/404.vue'),
    hidden: true
  }
]

export const asyncRoutes = [
  {
    path: '/bridge',
    component: Layout,
    redirect: '/bridge/device-manage',
    alwaysShow: true,
    name: 'basicSetMng',
    meta: {
      title: '基础配置',
      icon: 'qiapianmoshi_kuai'
    },
    children: [
      {
        path: 'device-manage',
        component: () => import('@/views/bridge/device-manage/index'),
        name: 'diviceMng',
        meta: {
          title: '特种设备管理',
          icon: 'qiapianmoshi_kuai',
          permissions: ['BSET.DVMG']
        }
      },
      {
        path: 'camera-manage',
        component: () => import('@/views/bridge/camera-manage/index'),
        name: 'cameraMng',
        meta: {
          title: '摄像头管理',
          icon: 'qiapianmoshi_kuai',
          permissions: ['BSET.CMMG']
        }
      },
      {
        path: 'early-warning-manage',
        component: () => import('@/views/bridge/early-warning-manage/index'),
        name: 'early-warning-manageMng',
        meta: {
          title: '设备问题点配置',
          icon: 'qiapianmoshi_kuai',
          permissions: ['BSET.EWCF']
        }
      }

    ]
  },
  {
    path: '/grider-erections',
    component: Layout,
    redirect: '/grider-erections/gride-work-manage',
    alwaysShow: true,
    name: 'ggrider-erectionsMng',
    meta: {
      title: '实时工况',
      icon: 'qiapianmoshi_kuai'
    },
    children: [
      {
        path: 'gride-work-manage',
        component: () => import('@/views/grider-erections/gride-work-manage/index'),
        name: 'gride-work-manageMng',
        meta: {
          title: '架桥机实时工况',
          icon: 'qiapianmoshi_kuai'
        }
      },
      {
        path: 'beam-carrier-manage',
        component: () => import('@/views/grider-erections/beam-carrier-manage/index'),
        name: 'beam-carrier-manageMng',
        meta: {
          title: '运梁车实时工况',
          icon: 'qiapianmoshi_kuai'
        }
      }
    ]
  },
  {
    path: '/history',
    component: Layout,
    redirect: '/grider-erections/gride-history',
    alwaysShow: true,
    name: 'gride-historyMng',
    meta: {
      title: '历史记录',
      icon: 'qiapianmoshi_kuai'
    },
    children: [
      {
        path: 'gride-history',
        component: () => import('@/views/grider-erections/gride-history/index'),
        name: 'gride-historyMng',
        meta: {
          title: '架桥机历史记录',
          icon: 'qiapianmoshi_kuai'
        }
      },
      {
        path: 'beam-carrier-history',
        component: () => import('@/views/grider-erections/beam-carrier-history/index'),
        name: 'beam-carrier-historyMng',
        meta: {
          title: '运梁车历史记录',
          icon: 'qiapianmoshi_kuai'
        }
      },
      {
        path: 'device-warning',
        component: () => import('@/views/bridge/device-warning/index'),
        name: 'device-warningMng',
        meta: {
          title: '问题点',
          icon: 'qiapianmoshi_kuai'
        }
      }
    ]
  },
  // {
  //   path: '/grider-erections',
  //   component: Layout,
  //   redirect: '/grider-erections/gride-work-info',
  //   alwaysShow: true,
  //   name: 'ggrider-erectionsMng',
  //   meta: {
  //     title: '工作台',
  //     icon: 'qiapianmoshi_kuai'
  //   },
  //   children: [
  //     {
  //       path: 'gride-work-info',
  //       component: () => import('@/views/grider-erections/gride-work-info/index'),
  //       name: 'gride-work-infoMng',
  //       meta: {
  //         title: '架桥机监控',
  //         icon: 'qiapianmoshi_kuai'
  //       }
  //     },
  //     {
  //       path: 'beam-carrier-work-info',
  //       component: () => import('@/views/grider-erections/beam-carrier-work-info/index'),
  //       name: 'beam-carrier-work-infoMng',
  //       meta: {
  //         title: '运梁车监控',
  //         icon: 'qiapianmoshi_kuai'
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: '/demo',
  //   component: Layout,
  //   redirect: '/demo/sample',
  //   alwaysShow: true,
  //   name: 'DemoMng',
  //   meta: {
  //     title: '功能示例',
  //     icon: 'qiapianmoshi_kuai',
  //     permissions: ['DEMO']
  //   },
  //   children: [
  //     {
  //       path: 'sample',
  //       component: () => import('@/views/demo/sample/index'),
  //       name: 'SampleMng',
  //       meta: {
  //         title: '样本管理',
  //         icon: 'qiapianmoshi_kuai',
  //         permissions: ['DEMO.SAM']
  //       }
  //     },
  //     {
  //       path: 'sample-submit',
  //       component: () => import('@/views/demo/sample-submit/index'),
  //       name: 'SampleSubmitMng',
  //       meta: {
  //         title: '样本提交',
  //         icon: 'qiapianmoshi_kuai',
  //         permissions: ['DEMO.SAM.S']
  //       }
  //     },
  //     {
  //       path: 'sample-audit',
  //       component: () => import('@/views/demo/sample-audit/index'),
  //       name: 'SampleAuditMng',
  //       meta: {
  //         title: '样本审核',
  //         icon: 'qiapianmoshi_kuai',
  //         permissions: ['DEMO.SAM.A']
  //       }
  //     }
  //   ]
  // },
  {
    path: '/sys',
    component: Layout,
    redirect: '/sys/audit-flow',
    alwaysShow: true,
    name: 'SysMng',
    meta: {
      title: '系统管理',
      icon: 'qiapianmoshi_kuai',
      permissions: ['XT']
    },
    children: [
      {
        path: 'audit-flow',
        component: () => import('@/views/sys/audit-flow/index'),
        name: 'AuditFlowMng',
        meta: {
          title: '审核流程配置',
          icon: 'qiapianmoshi_kuai',
          permissions: ['XT.SH']
        }
      },
      {
        path: 'dict',
        component: () => import('@/views/sys/dicts/dict'),
        name: 'DictMng',
        meta: {
          title: '数据字典配置',
          icon: 'qiapianmoshi_kuai',
          permissions: ['XT.ZD']
        }
      },
      {
        path: 'dict-entry',
        component: () => import('@/views/sys/dicts/dict-entry'),
        name: 'DictEntryMng',
        meta: {
          title: '字典条目配置',
          icon: 'qiapianmoshi_kuai',
          permissions: ['XT.ZD']
        }
      }
    ]
  }
]

const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    base: window.__POWERED_BY_QIANKUN__ ? ('/app-backbone/') : process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

router.goBack = function() {
  this.isBack = true
  window.history.go(-1)
}

export default router
