// 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 { Toast } from 'vant';
import router from './router'
import vantui from 'vant'
import { Lazyload } from 'vant'
import 'vant/lib/index.css'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
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的样式
const echarts = require('echarts');
import VueCookies from 'vue-cookies'
import ajax from './common/axios.config'
import store from './config/config'
import { Dialog } from 'vant';
import Vconsole from 'vconsole'

// import 'videojs-flash'; //引入才能播放rtmp视屏
// .use(VueAwesomeSwiper)
Vue.use(vantui).use(Lazyload).use(ElementUI).use(VideoPlayer).use(VueCookies)
Vue.prototype.$echarts = echarts
Vue.prototype.ajax = ajax
Vue.prototype.$store = store
Vue.prototype.$toast = Toast
Vue.prototype.$Dialog = Dialog
// Vue.prototype.$vConsole = new Vconsole()

Vue.config.productionTip = false

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


window.addEventListener('popstate', function (e) {
  router.isBack = true
}, false)

router.onError((error) => {
  // console.error("error:" + error);
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
    router.replace(targetPath);
  }
});
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('login')
    if (isLogin == "1") {
      var userid = VueCookies.get('userid')
      var userinfo = VueCookies.get('userInfo')
      store.commit("update", ["isLogin", true]);

      // var info = JSON.parse(window.sessionStorage.getItem("userInfo"))
      store.commit("update", ["userInfo", userinfo]);
      store.state.luyou = store.state.luyou + 1
      store.commit("update", ["luyou", store.state.luyou]);
      console.log(store.state.luyou);
      next()
    } else {
      store.commit("update", ["isLogin", false]);
      // Notify('用户未登录');
      Toast.fail('用户未登录');
      setTimeout(() => {
        next('/Login')
      }, 2000);
    }
  } else {
    next()  // 确保一定要有next()被调用
  }
})