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

Vue.use(VueRouter);

const routes = [
  // 绑定
  {
    path: '/bind',
    name: 'bind',
    meta: { title: '绑定', isleftarrow: false },
    component: () => import('../views/bind.vue'),
  },
  // 错误
  {
    path: '/error',
    name: '',
    meta: { title: '错误页面', isleftarrow: false },
    component: () => import('../views/error.vue'),
  },
  // 主页
  {
    path: '/live/index',
    name: 'live_index',
    meta: { title: '直播大厅', isleftarrow: true },
    component: () => import('../views/index.vue'),
  },
  // 直播大厅
  {
    path: '/live/roomlist',
    name: 'live_roomlist',
    meta: { title: '直播大厅', isleftarrow: true },
    component: () => import('../views/live/index.vue'),
  },
  {
    path: '/live/roomrecord',
    name: 'live_roomrecord',
    meta: { title: '直播大厅', isleftarrow: true },
    component: () => import('../views/record/index.vue'),
  },
  {
    path: '/onlive/videoinfo',
    name: 'live_roomvideo',
    meta: { title: '直播大厅', isleftarrow: true },
    component: () => import('../views/record/videoDetail.vue'),
  },
  // 直播详情
  {
    path: '/live/roomDetail',
    name: 'live_detail',
    meta: { title: '直播详情', isleftarrow: true },
    component: () => import('../views/live/roomDetail.vue'),
  },
  // 会议直播详情
  {
    path: '/live/roomMeet',
    name: 'live_meet',
    meta: { title: '直播会议', isleftarrow: true },
    component: () => import('../views/live/roomMeet.vue'),
  },
  // 直播详情
  {
    path: '/live/detail',
    name: 'live_detail',
    meta: { title: '直播详情', isleftarrow: true },
    component: () => import('../views/live/detail.vue'),
  },
  // 直播房间详情
  {
    path: '/onlive/roomInfo',
    name: 'onlive_roomInfo',
    meta: { title: '直播房间详情', isleftarrow: true },
    component: () => import('../views/onlive/roomInfo.vue'),
  },

  // 用户-个人中心
  {
    path: '/userCenter/user/index',
    name: 'user_index',
    meta: { title: '个人中心', isleftarrow: true },
    component: () => import('../views/userCenter/user/index.vue'),
  },
  // // 个人中心
  {
    path: '/user/index',
    name: 'user_index',
    meta: { title: '个人中心', isleftarrow: true },
    component: () => import('../views/user/index.vue'),
  },
  // // 注册
  {
    path: '/registered',
    name: 'registered',
    meta: { title: '注册', isleftarrow: true },
    component: () => import('../views/registered.vue'),
  },
  // 推送直播房间详情
  {
    path: '/onlive/roomdetail',
    name: 'onlive_roomdetail',
    meta: { title: '直播房间详情', isleftarrow: true },
    component: () => import('../views/onlive/roomdetail.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 === 'user_index') {
    store.commit('setUser');
    if (to.name === 'home_index') {
      next();
      return;
    }
    let user = store.state.user;
    if (user) {
      next();
    }
    //下面是没登录的情况,需要跳转页面到用户未登录页
    else next({ name: 'home_index' });
  } else {
    store.commit('setUser');
    next();
  }
});

export default router;
