let tools = {}
/**
 * 大小判断
 * @param a
 * @param b
 * @returns {number}
 */
tools.sortNumber = function(a, b) {
	return a - b;
}

/**
 * 隐藏手机号码
 * @param phone
 * @returns {*}
 */
tools.hidePhone = function(phone) {
	let reg = /^(\d{3})\d{4}(\d{4})$/;
	return phone.replace(reg, '$1****$2');
}
/**
 * 保留两位小数
 * @param num
 * @returns {string}
 */
tools.twoFloating = function(num) {
	// 获取两位小数
	let price = "";
	price = num * 1;
	price = String(price).split(".")[1];
	if (price !== undefined && price.length === 1) {
		price = `.${price}0`;
	} else {
		price === undefined ? (price = ".00") : (price = `.${price}`);
	}
	return price;
}
tools.formatDecimal = function(num, decimal) {
	num = num.toString()
	let index = num.indexOf('.')
	if (index !== -1) {
		num = num.substring(0, decimal + index + 1)
	} else {
		num = num.substring(0)
	}
	return parseFloat(num).toFixed(decimal)
}

/**
 * 错误提示
 * @param msg
 */
tools.error = function(msg) {
	uni.showToast({
		'title': msg,
		'icon': 'error',
		'mask': true,
		'duration': 1000
	})
}

/**
 * 成功提示
 * @param msg
 */
tools.success = function(msg) {
	uni.showToast({
		'title': msg,
		'icon': 'success',
		'mask': true,
		'duration': 1000
	})
}

/**
 * 显示Loading
 */
tools.showLoading = function() {
	uni.showLoading({
		title: '加载中...',
		mask: true
	});
}

/**
 * 关闭Loading
 */
tools.hideLoading = function() {
	uni.hideLoading();
}

/**
 * 32位随机字符串+时间 首位字母
 */
tools.SetString = function() {
	let str = '';
	let list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', '1', '2', '3', '4', '5',
		'6', '7', '8', '9', '0'
	];
	let numOne = Math.round(Math.random() * 14);
	let oneStr = list[numOne];
	for (let i = 1; i <= 32; i++) {
		let num = Math.round(Math.random() * 24);
		str += list[num];
	}
	let time = new Date().getTime()
	return oneStr + str + time;
}


/**
 * 获取时间戳（毫秒）
 * @returns {number}
 */
tools.getTime = function() {
	return new Date().getTime();
}


// 返回上级
tools.leftClick = function() {
	let pages = getCurrentPages();
	if (pages.length > 1) {
		uni.navigateBack({
			delta: 1
		})
	} else {
		uni.reLaunch({
			url: '/pages/index/index'
		});
	}
}

tools.updateVersion = function(sysVersion, appUrl) {
	let app_version = plus.runtime.version;
	console.log('版本号信息对比------------------------------' + app_version + '---------' + sysVersion)
	console.log(app_version < sysVersion)
	if (app_version < sysVersion) {
		uni.showLoading({
			title: '更新中……'
		})
		uni.downloadFile({ //执行下载
			url: appUrl, //下载地址
			success: (downloadResult) => { //下载成功
				uni.hideLoading();
				if (downloadResult.statusCode === 200) {
					uni.showModal({
						title: '',
						content: '更新成功，确定现在重启吗？',
						confirmText: '重启',
						confirmColor: '#EE8F57',
						success: function(res) {
							if (res.confirm === true) {
								plus.runtime.install( //安装
									downloadResult.tempFilePath, {
										force: true
									},
									function(res) {
										tools.success('更新成功，重启中')
										plus.runtime.restart();
									}
								);
							}
						}
					});
				}
			}
		});
	} else {
		// tools.success('你已是最新版本')
	}
}

/**
 * uniapp  html图片显示控制
 * @param str
 * @returns {*}
 */
tools.imgDeal = function(str) {
	if (str === null || str === undefined) {
		return '';
	} else {
		return str.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;"');
	}

}

/**
 * 获取用户类型
 * @returns {number}
 */
tools.getReceivingType = function() {
	let receivingType = uni.getStorageSync('receivingType')
	if (!receivingType) {
		receivingType = 0;
	} else {
		receivingType = receivingType * 1;
	}
	return receivingType;
}

/**
 * 获取是否有录单权限
 * @returns {number}
 */
tools.getEnteringType = function() {
	let enteringType = uni.getStorageSync('enteringType')
	if (!enteringType) {
		enteringType = 0;
	} else {
		enteringType = enteringType * 1;
	}
	return enteringType;
}

/**
 * 记录用户登录信息
 * @param data
 * @param type
 */
tools.setLoginData = function(data, type) {
	uni.setStorageSync('m_id', data.m_id)
	uni.setStorageSync('token', data.token)
	uni.setStorageSync('status', data.status)
	uni.setStorageSync('ministerType', data.minister_type)
	uni.setStorageSync('receivingType', data.receivingType)
	uni.setStorageSync('enteringType', data.enteringType)
	if (type) {
		tools.success('登陆成功')
		setTimeout(() => {
			if (data.status * 1 === 0) {
				//审核状态跳转至待审核页面
				uni.reLaunch({
					url: '/pages/login/module/await-audit'
				});
			} else {
				let excelPath = uni.getStorageSync('excelPath');
				console.log('excelPath:' + excelPath)
				if (excelPath) {
					uni.reLaunch({
						url: excelPath
					});
				} else {
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}

			}
		}, 1500)
	}

}


/**
 * 获取日期
 * @returns {string}
 */
tools.getDate = function(type) {
	let myDate = new Date();
	// let myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
	// let myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
	// let myToday = myDate.getDate(); //获取当前日(1-31)
	// let myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
	// let myHour = myDate.getHours(); //获取当前小时数(0-23)
	// let myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
	// let mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
	if (type === '-') {
		return myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate()
	} else {
		return myDate.getFullYear() + '年' + (myDate.getMonth() + 1) + '月' + myDate.getDate() + '日'
	}
}

tools.getRandFileName = function(filePath) {
	let extIndex = filePath.lastIndexOf('.');
	let extName = extIndex === -1 ? '' : filePath.substr(extIndex);
	return parseInt('' + Date.now() + Math.floor(Math.random() * 900 + 100), 10).toString(36) + extName;
}

/**
 * 拨打电话
 * @param phone
 */
tools.makingCall = function(phone) {
	uni.makePhoneCall({
		phoneNumber: phone
	});
}


tools.getOssVideo = function(url) {
	let strFileName = url.substring(0, url.lastIndexOf("."));
	return strFileName + '_vj_0.jpg';
}

tools.getFileType = function(url) {
	let suffixArr = ['.pdf', '.doc', '.docx', '.xlsx', '.xls']
	let suffix = url.substring(url.lastIndexOf("."));
	console.log(suffix)
	let suffixKey = suffixArr.indexOf(suffix)
	let fileType = 1;
	if (suffixKey < 1) {
		fileType = 1
	} else if (suffixKey < 3) {
		fileType = 2
	} else {
		fileType = 3
	}
	return fileType
}
tools.getFileName = function(url) {
	return url.substring(url.lastIndexOf("/") - 1, url.lastIndexOf("."));
}

/**
 * 获取平台类型
 * @returns {string}
 */
tools.getPlatform = function() {
	let platForm = undefined;
	// #ifdef H5
	platForm = 'H5';
	//#endif
	// #ifdef APP-PLUS
	platForm = 'APP';
	//#endif
	// #ifdef APP-PLUS-NVUE
	platForm = 'APP';
	//#endif
	// #ifdef APP-NVUE
	platForm = 'APP';
	//#endif
	// #ifdef MP-WEIXIN
	platForm = 'MP-WEIXIN';
	//#endif
	// #ifdef MP-ALIPAY
	platForm = 'MP-ALIPAY';
	//#endif
	// #ifdef MP-BAIDU
	platForm = 'MP-BAIDU';
	//#endif
	// #ifdef MP-TOUTIAO
	platForm = 'MP-TOUTIAO';
	//#endif
	// #ifdef MP-LARK
	platForm = 'MP-LARK';
	//#endif
	// #ifdef MP-QQ
	platForm = 'MP-QQ';
	//#endif
	// #ifdef MP-KUAISHOU
	platForm = 'MP-KUAISHOU';
	//#endif
	// #ifdef QUICKAPP-WEBVIEW
	platForm = 'QUICKAPP-WEBVIEW';
	//#endif
	return platForm;

}

/**
 * 等额本息
 * @param loansMoney
 * @param refundNum
 * @param interestRate
 */
tools.getAverageCapitalPlusInterest = function(loansMoney, refundNum, interestRate) {
	console.log('getAverageCapitalPlusInterest:等额本息')
	let repaymentMoney = 0;
	if (!loansMoney || loansMoney <= 0 || !refundNum || refundNum <= 0 || !interestRate || interestRate <= 0) {
		return repaymentMoney
	}
	let monthlyInterest = interestRate / 12 / 100;
	// console.log( ((monthlyInterest * Math.pow((1 + monthlyInterest), refundNum)) / (Math.pow((1 + monthlyInterest), refundNum) - 1)))
	repaymentMoney = (loansMoney * ((monthlyInterest * Math.pow((1 + monthlyInterest), refundNum)) / (Math.pow((1 +
		monthlyInterest), refundNum) - 1))).toFixed(2)
	// console.log('repaymentMoney:'+repaymentMoney)
	return repaymentMoney
}

/**
 * 等额本金
 * @param loansMoney
 * @param refundNum
 * @param interestRate
 * @returns {number}
 */
tools.getAverageCapital = function(loansMoney, refundNum, interestRate) {
	console.log('getAverageCapital:等额本金')
	let repaymentMoney = 0;
	if (!loansMoney || loansMoney <= 0 || !refundNum || refundNum <= 0 || !interestRate || interestRate <= 0) {
		return repaymentMoney
	}
	let monthlyInterest = interestRate / 12 / 100;
	repaymentMoney = (loansMoney / refundNum + loansMoney * monthlyInterest).toFixed(2)
	return repaymentMoney
}

/**
 * 先息后本
 * @param loansMoney
 * @param interestRate
 * @returns {string|number}
 */
tools.getInterestFirst = function(loansMoney, interestRate) {
	console.log('getInterestFirst:先息后本')
	let repaymentMoney = 0;
	if (!loansMoney || loansMoney <= 0 || !interestRate || interestRate <= 0) {
		return repaymentMoney
	}
	let monthlyInterest = interestRate / 12 / 100;
	repaymentMoney = (loansMoney * monthlyInterest).toFixed(2)
	return repaymentMoney
}

/**
 * 获取日期
 * @returns {{d: number, y: number, m: number}}
 */
tools.getDateArr = function(time) {
	console.log('time:' + time)
	let myDate = new Date(time);
	let myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
	let myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
	let myToday = myDate.getDate(); //获取当前日(1-31)
	// let myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
	// let myHour = myDate.getHours(); //获取当前小时数(0-23)
	// let myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
	// let mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
	return {
		'y': myYear,
		'm': myMonth >= 10 ? myMonth : '0' + myMonth,
		'd': myToday >= 10 ? myToday : '0' + myToday
	};
}

/**
 * 获取统计时间
 * @param d
 * @param num
 * @returns {[{d: number, y: number, m: number},{d: number, y: number, m: number}]}
 */
tools.getStatisticsDate = function(d, num) {
	let date = new Date(d)
	let dateTime = date.getTime();
	dateTime += (num * 86400000);
	return [tools.getDateArr(dateTime), tools.getDateArr(dateTime)];
}

/**
 *
 * @returns {[{d: number, y: number, m: number},{d: number, y: number, m: number}]}
 */
tools.getNextWeek = function(d, num) {
	let date = new Date(d)
	let dateTime = date.getTime();
	console.log('start:' + dateTime)
	dateTime += (num * 86400000);
	let weekDate = new Date(dateTime)
	let weekday = weekDate.getDay();
	let weekStarTime;
	let weekEndTime;
	if (weekday === 0) {
		weekStarTime = dateTime - 86400000 * 6;
		weekEndTime = dateTime;
	} else {
		weekStarTime = dateTime - (86400000 * (weekday - 1));
		weekEndTime = weekStarTime + (86400000 * 7);
	}
	return [tools.getDateArr(weekStarTime), tools.getDateArr(weekEndTime)];
}

tools.getNextMonth = function(d, num) {
	let date = new Date(d)
	let dateTime = date.getTime();
	dateTime += (num * 86400000);
	let newDate = new Date(dateTime)
	let year = newDate.getFullYear(); //获取完整的年份(4位,1970-????)
	let month = newDate.getMonth(); //获取当前月份(0-11,0代表1月)
	let starTime = new Date(year, month, 1).valueOf();
	let endTime = new Date(year, month + 1, 1).valueOf() - 86400000;
	return [tools.getDateArr(starTime), tools.getDateArr(endTime)];
}

tools.getNextYear = function(d, num) {
	let date = new Date(d)
	let dateTime = date.getTime();
	dateTime += (num * 86400000);
	let newDate = new Date(dateTime)
	let year = newDate.getFullYear(); //获取完整的年份(4位,1970-????)
	let starTime = new Date(year, 0, 1).valueOf();
	let endTime = new Date(year + 1, 0, 1).valueOf() - 86400000;
	return [tools.getDateArr(starTime), tools.getDateArr(endTime)];
}

tools.topHeight = function() {
	const navTop = uni.getMenuButtonBoundingClientRect()
	return navTop.height + navTop.top + 10
}

export default tools