// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import { Message, Loading } from 'element-ui';
import store from './config/config'
import ajax from './common/axios.config'
import VueCookies from 'vue-cookies'
import './common/player.css'
import VideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css' //videoJs的样式
import 'vue-video-player/src/custom-theme.css' //vue-video-player的样式
Vue.use(VueCookies).use(VideoPlayer)

Vue.prototype.$loading = Loading
Vue.prototype.$store = store;// 将store实例挂在vue原型上
Vue.prototype.ajax = ajax
Vue.prototype.$message = Message
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

VueCookies.config('30d')
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  const requireAuth = to.meta.requireAuth
  // 判断该路由是否需要登录权限
  if (requireAuth) {
    var isLogin = VueCookies.get("Alogin")
    var info = VueCookies.get("AuserInfo")
    if (isLogin == "1") {
      store.commit("update", ["isLogin", true]);
      // var info = JSON.parse(window.sessionStorage.getItem("userInfo"))
      store.commit("update", ["userInfo", info]);
      store.state.luyou = store.state.luyou + 1
      store.commit("update", ["luyou", store.state.luyou]);
      next()
    } else {
      const loading = Loading.service({
        background: "rgba(255, 255, 255)",
        target: document.querySelector("body"),
      });
      store.commit("update", ["isLogin", false]);
      Message({
        message: '未登录，请登录',
        type: 'warning'
      });
      setTimeout(() => {
        loading.close();
        next('/login')
      }, 2000);
    }
  } else {
    next()  // 确保一定要有next()被调用
  }
})
