import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store/index';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: '',
    component: () => import('../views/index.vue'),
  },
  // 登录
  {
    path: '/login',
    name: 'login',
    meta: { title: '登录', subSite: true },
    component: () => import('../views/login.vue'),
  },
  // 注册
  {
    path: '/register',
    meta: { title: '注册', subSite: true },
    component: () => import('../views/register.vue'),
  },
  // 新闻动态
  {
    path: '/news/index',
    meta: { title: '新闻动态', subSite: true },
    component: () => import('../views/news/index.vue'),
  },
  // 新闻动态列表+详情
  {
    path: '/news/listDetail',
    meta: { title: '新闻详情', subSite: true },
    component: () => import('../views/news/listDetail.vue'),
  },
  // 计算服务
  {
    path: '/service/index',
    meta: { title: '计算服务', subSite: true },
    component: () => import('../views/service/index.vue'),
  },
  // 计算服务-详情
  {
    path: '/service/detail',
    meta: { title: '计算服务', subSite: true },
    component: () => import('../views/service/detail.vue'),
  },
  // 交流互动
  {
    path: '/communication/index',
    meta: { title: '交流互动', subSite: true },
    component: () => import('../views/communication/index.vue'),
  },

  //交流互动列表详情

  {
    path: '/communication/list',
    meta: { title: '交流互动列表详情', subSite: true },
    component: () => import('../views/communication/list.vue'),
  },

  //发布需求

  {
    path: '/demand/demand',
    meta: { title: '发布需求', subSite: true },
    component: () => import('../views/demand/demand.vue'),
  },

  {
    path: '/achievement/index',
    meta: { title: '成果展示', subSite: true },
    component: () => import('../views/achievement/index.vue'),
  },

  // 党建学苑
  {
    path: '/partisan/index',
    name: '党建学苑',
    meta: { title: '党建学苑', subSite: true },
    component: () => import('../views/partisan/index.vue'),
  },
  // 党建学苑-列表详情
  {
    path: '/partisan/listDetail',
    name: '党建学苑',
    meta: { title: '党建学苑', subSite: true },
    component: () => import('../views/partisan/listDetail.vue'),
  },
  // 个人中心
  {
    path: '/pcenter/index',
    name: 'pcenter_center',
    component: () => import('../views/pcenter/index.vue'),
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.NODE_ENV === 'development' ? '' : process.env.VUE_APP_ROUTER,
  routes,
});
router.beforeEach((to, form, next) => {
  if (to.name === 'pcenter_center') {
    store.commit('setUser');
    if (to.name === 'login') {
      next();
      return;
    }
    let user = store.state.user;
    if (user) {
      next();
    }
    //下面是没登录的情况,需要跳转页面到用户未登录页
    else next({ name: 'login' });
  } else {
    store.commit('setUser');
    next();
  }
});

export default router;
