// 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 store from './config/config'
import { Message, Loading } from 'element-ui';
import ajax from './common/axios.config'
import qs from 'qs'
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的样式
import VueCookies from 'vue-cookies'

Vue.use(VideoPlayer).use(VueCookies)
Vue.config.productionTip = false
Vue.prototype.$store = store; // 将store实例挂在vue原型上
Vue.prototype.ajax = ajax
Vue.prototype.$message = Message
Vue.prototype.$loading = Loading
Vue.prototype.openLoading = function(target) {
    const loading = this.$loading.service({ // 声明一个loading对象
        lock: true, // 是否锁屏
        background: 'rgba(255, 255, 255, 0.7)', // 背景颜色
        target: target ? target : document.body, // 需要遮罩的区域
        body: true,
    })
    return loading;
}
Vue.prototype.$qs = qs

/* 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('tlogin')
        if (isLogin == "1") {
            var userinfo = VueCookies.get('teacherInfo')
            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]);
            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()被调用
    }
})