import Base64 from './base64';
export const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

export const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : `0${n}`
}

export const getGUID = () => {
	let guid = "";
	for (let ii = 1; ii <= 32; ii++) {
		let n = Math.floor(Math.random() * 16.0).toString(16).toUpperCase();
		guid += n;
		if ((ii == 8) || (ii == 12) || (ii == 16) || (ii == 20)) guid += "-";
	}
	return guid.toUpperCase();
}

// 显示消息提示框
export const showMessage = (title, icon, time) => {
	uni.showToast({
		title: title,
		icon: icon,
		duration: time
	});
}

// 显示 loading 提示框。需主动调用 uni.hideLoading 才能关闭提示框
export const loading = (title, time) => {
	if (time == null) time = 5000;
	uni.showLoading(title);
	if (time && time != false) {
		setTimeout(function() {
			uni.hideLoading();
		}, time);
	}

}

// 显示模态提示框
export const showModelMessage = (content, title, showCancel, confirmText = "确定") => {
	if (title == null || title == "") title = "提示";
	if (showCancel == null) showCancel = false;
	return new Promise((resolve, reject) => {
		uni.showModal({
			title,
			content,
			showCancel,
			confirmText,
			success: function(res) {
				resolve(res);
			}
		});
	});
}

export function dataFormat(date, fmt) {
	var o = {
		"M+": date.getMonth() + 1, //月份
		"d+": date.getDate(), //日
		"H+": date.getHours(), //小时
		"m+": date.getMinutes(), //分
		"s+": date.getSeconds(), //秒
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度
		"S": date.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((
			"00" + o[k]).substr(("" + o[k]).length)));
	}
	return fmt;
}

//格式话长日期
export function longToDateFormat(value) {
	return value ? dataFormat((new Date(value)), 'yyyy-MM-dd') : "";
};
//格式话长日期（有时间）
export function longToDateFormatTime(value) {
	return value ? dataFormat((new Date(value)), 'yyyy-MM-dd HH:mm:ss') : "";
};

export function jkcBaseEncode(text) { //加密
	// 确保输入是字符串类型
	if (typeof text !== 'string') {
		throw new Error('Input must be a string');
	}
	const jkcBase = base64Encode("jkcBase")
	// 第一次 Base64 编码
	let firstEncode = Base64.encode(text);
	// 将 "jkcBase" 插入到第一次编码结果的第二个字母后
	let modifiedFirstEncode = firstEncode.slice(0, 2) + jkcBase + firstEncode.slice(2);
	// 第二次 Base64 编码
	let secondEncode = Base64.encode(modifiedFirstEncode);
	return secondEncode;
}
export function jkcBaseDecode(text) { //解密
	const jkcBase = base64Encode("jkcBase")
	// 确保输入是字符串类型
	if (typeof text !== 'string') {
		throw new Error('Input must be a string');
	}
	// 第一次 Base64 解码
	let firstDecode = Base64.decode(text);
	// 移除 "jkcBase"，它在第一次编码后的第二个字符后
	let modifiedText = firstDecode.replace(jkcBase, '');
	// 第二次 Base64 解码
	let originalText = Base64.decode(modifiedText);
	return originalText;
}

export function base64Encode(text) {
	return Base64.encode(text);
}

export function base64Decode(text) {
	return Base64.decode(text);
}

export function getUrlParams(url) {
	const paramsRegex = /[?&]+([^=&]+)=([^&]*)/gi;
	const params = {};
	let match;
	while (match = paramsRegex.exec(url)) {
		params[match[1]] = match[2];
	}
	return params;
}

export function isNullOrEmpty(value) {
	if (typeof(value) == "undefined" || value == null || value == "")
		return true;
	else
		return false;
}