import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import store from './store'
import { initLocale } from '@/locales/i18n'
import loadCloudFontFace from '@/utils/font'
import Dialog from '@/wxcomponents/vant/dialog/dialog'
	
import './uni.promisify.adaptor'
Vue.config.productionTip = false
Vue.prototype.$config = {
	pageHeight: 0
}
Vue.prototype.onInitNavbar = function(e) {
	this.$config.pageHeight = e.detail.pageHeight
}
Vue.prototype.checkAuth = function(pagePath) {
	if (!store.getters.user) {
		this.redirectTo('/pages/user/login?redirect=' + encodeURIComponent(pagePath))
		return false
	} else {
		return true
	}
}
Vue.prototype.redirectTo = function(pagePath) {
	if (pagePath.indexOf('https') === -1) {
		uni.redirectTo({
			url: pagePath
		})
	} else {
		uni.redirectTo({
			url: '/pages/index/webview?url=' + encodeURIComponent(pagePath)
		})
	}
}
Vue.prototype.navigateTo = function(pagePath) {
	if (pagePath.indexOf('https') === -1) {
		uni.navigateTo({
			url: pagePath
		})
	} else {
		this.webviewTo(pagePath)
	}
}
Vue.prototype.webviewTo = function(url) {
	uni.navigateTo({
		url: '/pages/index/webview?url=' + encodeURIComponent(url)
	})
}

Vue.prototype.showToast = function(title, complete) {
	uni.showToast({
		icon: 'none',
		title: title
	})
	if (complete)
		setTimeout(complete, 2000)
}
Vue.prototype.showSuccessToast = function(title, complete) {
	uni.showToast({
		icon: 'success',
		title: title,
		duration: 2000
	})
	if (complete)
		setTimeout(complete, 2000)
}
Vue.prototype.showFailToast = function(title, complete) {
	uni.showToast({
		icon: 'fail',
		title: title,
		duration: 2000
	})
	if (complete)
		setTimeout(complete, 2000)
}

Vue.prototype.showLoading = function(options) {
	options = options || {
		forbidClick: true,
		loadingType: 'spinner',
	}
	if (!options.title) {
		options.title = '加载中'
	}
	uni.showLoading(options)
}

Vue.prototype.hideLoading = function() {
	uni.hideLoading()
}

// 所在页面需放置元素
// <van-dialog id="van-dialog" />
Vue.prototype.showAlertDialog = function(message, title) {
	return Dialog.alert({
		title: title || '系统提示',
		message: message
	})
}

// 所在页面需放置元素
// <van-dialog id="van-dialog" />
Vue.prototype.showConfirmDialog = function(message, title) {
	const options = {
		title: title || '系统提示',
		message: message
	}
	return Dialog.confirm(options)
}

Vue.prototype.loadFontFace = function(fontFamily) {
	let fontUrl = ''
	if (fontFamily === 'Poppins') {
		fontUrl = 'https://oss.starify.cn/test/Poppins-SemiBold.ttf'
	}
	loadCloudFontFace(fontUrl, fontFamily + '.ttf', fontFamily)
}

App.mpType = 'app'
const app = new Vue({
	App,
	store
})
app.$mount()
const token = uni.getStorageSync('token')
const user = uni.getStorageSync('user')
if (user) {
	store.commit('SET_USER', user)
}
if (token) {
	store.commit('SET_TOKEN', token)
	store.dispatch('getInfo')
}

initLocale()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif