import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store/index';
const jwt = require('jsonwebtoken');
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);
const web = [
  {
    path: '/',
    name: 'index',
    meta: { title: '网站首页' },
    component: () => import('../views/index.vue'),
  },
  {
    path: '/website',
    name: 'website',
    component: () => import('../views/website.vue'),
    children: [
      {
        path: '/news/index',
        name: 'news_index',
        meta: { title: '新闻咨询' },
        component: () => import('../views/news/index.vue'),
      },
      {
        path: '/news/list',
        name: 'news_list',
        meta: { title: '新闻咨询-列表页' },
        component: () => import('../views/news/list.vue'),
      },
      {
        path: '/service/index',
        name: 'service_index',
        meta: { title: '科技服务' },
        component: () => import('../views/service/index.vue'),
      },
      {
        path: '/service/policy',
        name: 'service_policy',
        meta: { title: '科技服务-高企政策服务' },
        component: () => import('../views/service/policy.vue'),
      },
      {
        path: '/service/ticket',
        name: 'service_ticket',
        meta: { title: '科技服务-创新券服务' },
        component: () => import('../views/service/ticket.vue'),
      },

      {
        path: '/service/register',
        name: 'service_register',
        meta: { title: '用户注册' },
        component: () => import('../views/service/register.vue'),
      },

      {
        path: '/market/index',
        name: 'market_index',
        meta: { title: '技术超市' },
        component: () => import('../views/market/index.vue'),
      },
      {
        path: '/market/list',
        name: 'market_list',
        meta: { title: '技术超市-列表页' },
        component: () => import('../views/market/list.vue'),
      },
      {
        path: '/train/index',
        name: 'train_index',
        meta: { title: '宣传培训' },
        component: () => import('../views/train/index.vue'),
      },
      {
        path: '/dynamic/index',
        name: 'dynamic_index',
        meta: { title: '数据动态' },
        component: () => import('../views/dynamic/index.vue'),
      },
    ],
  },
  // 管理登录
  {
    path: '/login',
    name: 'login',
    meta: { title: '管理登录' },
    component: () => import('../views/login.vue'),
  },
  // 宣传培训详情页
  {
    path: '/train/detail',
    name: 'train_detail',
    meta: { title: '宣传培训-详情页' },
    component: () => import('../views/train/detail.vue'),
  },
  {
    path: '/adminCenter/homeIndex',
    name: 'adminCenter',
    component: () => import('../views/adminCenter/index.vue'),
    children: [
      {
        path: '/adminCenter/homeIndex',
        name: 'admin_homeIndex',
        meta: { title: '首页' },
        component: () => import('../views/adminCenter/homeIndex/index.vue'),
      },
      {
        path: '/adminCenter/test',
        name: 'admin_test',
        meta: { title: '测试页面' },
        component: () => import('../views/adminCenter/test/index.vue'),
      },
      {
        path: '/adminCenter/news',
        name: 'admin_news',
        meta: { title: '新闻管理' },
        component: () => import('../views/adminCenter/news/index.vue'),
      },
      {
        path: '/adminCenter/news/detail',
        name: 'admin_news_detail',
        meta: { title: '新闻管理' },
        component: () => import('../views/adminCenter/news/detail.vue'),
      },
      {
        path: '/adminCenter/product',
        name: 'admin_product',
        meta: { title: '科技成果' },
        component: () => import('../views/adminCenter/product/index.vue'),
      },
      {
        path: '/adminCenter/product/detail',
        name: 'admin_product_detail',
        meta: { title: '科技成果' },
        component: () => import('../views/adminCenter/product/detail.vue'),
      },
      {
        path: '/adminCenter/patent',
        name: 'admin_patent',
        meta: { title: '专利管理' },
        component: () => import('../views/adminCenter/patent/index.vue'),
      },
      {
        path: '/adminCenter/patent/detail',
        name: 'admin_patent_detail',
        meta: { title: '专利管理' },
        component: () => import('../views/adminCenter/patent/detail.vue'),
      },
      {
        path: '/adminCenter/roadShow',
        name: 'admin_road_show',
        meta: { title: '路演管理' },
        component: () => import('../views/adminCenter/road_show/index.vue'),
      },
      {
        path: '/adminCenter/roadShow/detail',
        name: 'admin_road_show_detail',
        meta: { title: '路演管理' },
        component: () => import('../views/adminCenter/road_show/detail.vue'),
      },
      {
        path: '/adminCenter/expert',
        name: 'admin_expert',
        meta: { title: '专家管理' },
        component: () => import('../views/adminCenter/expert/index.vue'),
      },
      {
        path: '/adminCenter/expert/detail',
        name: 'admin_expert_detail',
        meta: { title: '专家管理' },
        component: () => import('../views/adminCenter/expert/detail.vue'),
      },
      {
        path: '/adminCenter/organization',
        name: 'admin_organization',
        meta: { title: '企业管理' },
        component: () => import('../views/adminCenter/organization/index.vue'),
      },
      {
        path: '/adminCenter/organization/detail',
        name: 'admin_organization_detail',
        meta: { title: '企业管理' },
        component: () => import('../views/adminCenter/organization/detail.vue'),
      },
      {
        path: '/adminCenter/mechanism',
        name: 'admin_mechanism',
        meta: { title: '机构管理' },
        component: () => import('../views/adminCenter/mechanism/index.vue'),
      },
      {
        path: '/adminCenter/mechanism/detail',
        name: 'admin_mechanism_detail',
        meta: { title: '机构管理' },
        component: () => import('../views/adminCenter/mechanism/detail.vue'),
      },
      {
        path: '/adminCenter/online',
        name: 'admin_online',
        meta: { title: '线上管理' },
        component: () => import('../views/adminCenter/online/index.vue'),
      },
      {
        path: '/adminCenter/online/detail',
        name: 'admin_online_detail',
        meta: { title: '线上管理' },
        component: () => import('../views/adminCenter/online/detail.vue'),
      },
      {
        path: '/adminCenter/online/file',
        name: 'admin_online_file',
        meta: { title: '线上管理' },
        component: () => import('../views/adminCenter/online/file.vue'),
      },
      {
        path: '/adminCenter/policy',
        name: 'admin_policy',
        meta: { title: '高企政策服务管理' },
        component: () => import('../views/adminCenter/policy/index.vue'),
      },
      {
        path: '/adminCenter/policy/detail',
        name: 'admin_online_detail',
        meta: { title: '高企政策服务管理' },
        component: () => import('../views/adminCenter/policy/detail.vue'),
      },
      {
        path: '/adminCenter/ticket',
        name: 'admin_ticket',
        meta: { title: '创新劵服务管理' },
        component: () => import('../views/adminCenter/ticket/status.vue'),
      },
      {
        path: '/adminCenter/ticket/detail',
        name: 'admin_ticket_detail',
        meta: { title: '创新劵服务管理' },
        component: () => import('../views/adminCenter/ticket/detail.vue'),
      },
      // 企业信息
      {
        path: '/adminCenter/company/basic',
        name: 'company_basic_index',
        meta: { title: '企业基本信息' },
        component: () => import('../views/adminCenter/company/basic/index.vue'),
      },
      {
        path: '/adminCenter/company/policy',
        name: 'company_policy_index',
        meta: { title: '政策服务管理' },
        component: () => import('../views/adminCenter/company/policy/index.vue'),
      },
      {
        path: '/adminCenter/company/policy/detail',
        name: 'company_policy_detail',
        meta: { title: '政策服务申请' },
        component: () => import('../views/adminCenter/company/policy/detail.vue'),
      },
      {
        path: '/adminCenter/company/ticket',
        name: 'company_ticket_index',
        meta: { title: '创新券管理' },
        component: () => import('../views/adminCenter/company/ticket/index.vue'),
      },
      {
        path: '/adminCenter/company/ticket/detail',
        name: 'company_ticket_detail',
        meta: { title: '创新券信息管理' },
        component: () => import('../views/adminCenter/company/ticket/detail.vue'),
      },
      {
        path: '/adminCenter/company/ticket/info',
        name: 'company_ticket_info',
        meta: { title: '创新券信息查看' },
        component: () => import('../views/adminCenter/company/ticket/info.vue'),
      },
      {
        path: '/adminCenter/company/ticket/update',
        name: 'company_ticket_update',
        meta: { title: '创新券信息重申' },
        component: () => import('../views/adminCenter/company/ticket/update.vue'),
      },

      // 中介机构
      {
        path: '/adminCenter/inter/basic',
        name: 'inter_basic_index',
        meta: { title: '机构基本信息' },
        component: () => import('../views/adminCenter/inter/basic/index.vue'),
      },
      {
        path: '/adminCenter/inter/ticket',
        name: 'inter_ticket_index',
        meta: { title: '创新券审核管理' },
        component: () => import('../views/adminCenter/inter/ticket/index.vue'),
      },
      {
        path: '/adminCenter/inter/ticket/detail',
        name: 'inter_ticket_detail',
        meta: { title: '创新券审核管理' },
        component: () => import('../views/adminCenter/inter/ticket/detail.vue'),
      },
    ],
  },
];
const routes = [...web];
const router = new VueRouter({
  mode: 'history',
  base: process.env.VUE_APP_ROUTER,
  routes,
});
router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title} `;
  const token = localStorage.getItem('token');
  if (to.path == '/adminCenter/homeIndex') {
    if (!token) {
      next('/login');
    } else {
      let user = jwt.decode(token);
      store.commit('setUser', user, { root: true });
      next();
    }
  } else {
    let user = jwt.decode(token);
    store.commit('setUser', user, { root: true });
    next();
  }
});

export default router;
