import {
	getUrlParams
} from './utils.js'



/**
 * 全局uni api二次封装工具
 */

//统一消息提醒
export const msg = (title, icon = 'none', duration = 1500, mask = false) => {
	//统一提示方便全局修改
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}
//showLoading状态
export const loading = (title) => {
	uni.showLoading({
		title: title,
		mask: true
	});
}
export const hide = () => {
	uni.hideLoading();
}

//确认模态框封装
export async function showModal(content = "", color = "#108DFC", confirmText = "确定") {
	let res = await uni.showModal({
		title: '提示',
		content: content,
		confirmColor: color,
		confirmText: confirmText
	})
	if (!res[1].confirm) {
		return false;
	}
	return true;
}


// 函数用于获取上一页页面栈的实例
export const prePage = (index = 2) => {
	return getCurrentPageCustom(index);
}
// 函数用于获取当前页面栈的实例
export const currentPage = (index = 1) => {
	return getCurrentPageCustom(index);
}
//  获取自定义页面栈
function getCurrentPageCustom(index) {
	let pages = getCurrentPages(); // 函数用于获取当前页面栈的实例
	let prePage = pages[pages.length - index];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}

/**
 * 页面滚动到指定位置
 */
export const pageScrollTo = (id, duration = 300) => {
	// 获取某元素距离顶部的距离
	// #ifdef H5
	let res = document.querySelector(id)
	let scrollTop = res.offsetTop;
	uni.pageScrollTo({
		scrollTop: scrollTop,
	});
	// #endif

	// #ifdef MP-WEIXIN
	let query = uni.createSelectorQuery();
	query.select(id).boundingClientRect(res => {
		let scrollTop = res.top;
		console.log(res);
		// 开始滚动
		uni.pageScrollTo({
			scrollTop: scrollTop,
			duration: duration
		});
	}).exec()
	// #endif

}
