import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from '$project/store'

import * as API_User from '$project/apis/user'
import {
	clear,
} from '$project/utils/storage'

//引入mui
import mui from '$project/assets/js/mui.min.js'
import '$project/assets/css/mui.min.css'
import Vconsole from 'vconsole'

window.mui = mui;
window.mui.init();

if (location.hostname === 'localhost') {
	Vue.prototype.HTTPLOCAT = ''
} else {
	const http = window.location.protocol + '//' + location.hostname + ':' + location.port
	Vue.prototype.HTTPLOCAT = http + ''
}

Vue.config.productionTip = false

//图片预览
import VueDirectiveImagePreviewer from 'vue-directive-image-previewer'
import 'vue-directive-image-previewer/dist/assets/style.css'
Vue.use(VueDirectiveImagePreviewer, {
	background: {
		color: '#000'
	},
	// transition
	animate: {
		duration: 600,
		delay: 500
	},
	// loading (not supported)
	loading: {
		image: ''
	},
	// cursor(css)
	cursor: 'pointer',
	clickMethod: 'doubleClick'
})


import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer)
Viewer.setDefaults({
	Options: {
		"inline": true,
		"button": true,
		"navbar": false,
		"title": true,
		"toolbar": false,
		"tooltip": true,
		"movable": true,
		"zoomable": true,
		"rotatable": true,
		"scalable": true,
		"transition": true,
		"fullscreen": true,
		"keyboard": true,
		"url": "data-source"
	}
});

//获取纯权限路由数组,由函数每次获取
function getAuthFunList() {
	let funList = [];
	let fun_list = store.state.auth_fun_list;
	if (fun_list && fun_list.length > 0) {
		fun_list.forEach(function(item) {
			funList.push(item)
		})
	}
	return funList;
}
//console.log(funList)

const checkAuth = (to, from, next) => {
	if (to.meta != null && to.meta.requireAuth) {
		if (store.state.person_data || store.state.auth_fun_list) {

			if ((to.meta.form != null && to.meta.form) || (to.meta.mode != null && to.meta.mode)) {

				let funList = getAuthFunList();
				//验证
				if (funList.indexOf(to.name) > -1) {
					next();
				} else {
					//测试环境不验证权限
					if (process.env.VUE_APP_NODE_NAME == 'devlopment') {
						next();
					} else {
						mui.toast('无权访问');
						router.replace({
							name: 'Master'
						})
					}
				}
			} else {
				//不验证
				next();
			}
		} else {
			window.location.href = '#/user/login';
		}
	} else {
		next();
	}
}

const checkRole = (to, from, next) => {
	//切换当前场景
	if (to.query.exchange == '1' && to.query.role_id) {
		API_User.exchangePopedom({
			id: to.query.role_id
		}).then(response => {
			checkFun(to, from, next)
		}).catch(error => {
			mui.toast(error);
		})
	} else {
		checkFun(to, from, next)
	}
}

const checkFun = (to, from, next) => {
	//如果funlist不存在，在更新版本号清空后，先重新请求再判断
	var token = to.query.token;
	if (token) {
		if (store.state.auth_fun_list) {
			checkAuth(to, from, next);
		} else {
			//这里token一定要存在,从微信模板消息带过来的
			API_User.getAuthFunList(token).then(response => {

				if (response && response.allIconRoute) {
					var allIconRoute = response.allIconRoute;
					store.commit('SET_AUTH_FUN_LIST', allIconRoute);

					checkAuth(to, from, next);
				} else {
					mui.toast('链接已失效，请登录小鹏管家查看');
					router.replace({
						name: 'Master'
					})
				}

			}).catch(error => {
				mui.toast(error);
			})
		}
	} else {
		checkAuth(to, from, next);
	}
}

router.beforeEach((to, from, next) => {
	if (to.query.test) {
		if (to.query.test == 'test') {
			let vConsole = new Vconsole()
		}
	}

	//更新版本号，清除本地数据
	if (store.state.app_version != process.env.VUE_APP_VERSION) {
		clear();
		//写入当前版本号
		store.commit('SET_APP_VERSION', process.env.VUE_APP_VERSION);
	}

	//如果传递过来了token则写入
	if (to.query.token) {
		console.log(to.query.token)
		store.commit('SET_TOKEN', to.query.token);
	}

	if (process.env.VUE_APP_NODE_NAME == 'production') {
		window._hmt.push(['_trackEvent', '页面跳转', 'click', to.meta.title, 1]);
	}

	checkRole(to, from, next);

})

new Vue({
	router,
	store,
	render: h => h(App)
}).$mount('#app')
