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

//引入mui
import mui from '$project/assets/js/mui.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'
})

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


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

	if (to.meta != null && to.meta.requireAuth) {
		if (store.state.person_data) {

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

				let funList = getFunList();
				//验证
				if (funList.indexOf(to.name) > -1) {
					next();
				} else {
					//测试环境不验证权限
					if (process.env.VUE_APP_NODE_NAME == 'devlopment') {
						next();
					} else {
						mui.toast('无权访问');
					}
				}
			} else {
				//不验证
				next();
			}

		} else {
			window.location.href = '../home/#/user/login';
		}
	} else {
		next();
	}
})

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