// 判断字符串是否为空
const IsEmptyStr = (str) => {
	str = str.toString();
	if (str == null || str == 'null' || str == undefined || str == 'undefined' || str.match(/^[\s]*$/)) {
		return true;
	} else {
		return false;
	}
}

// 判断数组对象中所有属性是否为空
const judgeObjectComplete = (ObjectValue) => {
	let flag = new Boolean()
	flag = true
	for (let key in ObjectValue) {
		if (ObjectValue[key]) {} else {
			flag = false
		}
	}
	if (!flag) {
		return false
	} else {
		return true
	}

}

// 判断对象中所有属性是否为空
const ifObjIIsNull = (ObjectValue) => { // 判断一个对象下是否有空属性
	let flag = new Boolean()
	flag = true
	for (let key in ObjectValue) {
		if (!IsEmptyStr(ObjectValue[key])) {
			return false
		}
	}
	return flag
}

//验证手机号
const phoneFun = (phones) => {
	var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
	if (!myreg.test(phones)) {
		return false;
	} else {
		return true;
	}
}

/*
  获取当前日期  YYYY-MM-DD
  @returns {string}
*/
const getToday = () => {
	var date = new Date();
	var date = new Date();
	var seperator1 = "-";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}


/**
 * 防抖函数(可用于防止重复提交)
 * 当持续触发事件时，一定时间段内没有再触发事件，事件处理函数才会执行一次，
 * 如果设定时间到来之前，又触发了事件，就重新开始延时。也就是说当一个用户一直触发这个函数，
 * 且每次触发函数的间隔小于既定时间，那么防抖的情况下只会执行一次。
 *
 * @param fn 执行函数
 * @param delay 间隔时间
 */
const debounce = (fn, delay) => {
	let timeout = null;
	return function() {
		if (timeout) {
			clearTimeout(timeout);
		}
		timeout = setTimeout(() => {
			this[fn](); // 兼容vue函数 
		}, delay);
	};
}

/**
 * 节流函数
 * 当持续触发事件时，保证在一定时间内只调用一次事件处理函数
 * 小于既定值，函数节流会每隔这个时间调用一次
 *
 * @param fn 执行函数
 * @param delay 间隔时间
 */
const throttle = (fn, delay) => {
	let lastCall = 0;
	let throttled = function() {
		const now = new Date().getTime();
		// remaining 不触发下一次函数的剩余时间
		if (now - lastCall < delay && lastCall) return;
		lastCall = now;
		this[fn].apply(this, arguments);
	}
	return throttled;
}

/**
 * 时间戳转换方法
 *
 * @param timestamp  时间戳
 * @param format 时间格式
 */
// const formatDate = (timestamp, format = 'YYYY-MM-dd HH:mm:ss') => {
const formatDate = (timestamp, format = 'YYYY/MM/dd HH:mm:ss') => {
	let time = new Date(timestamp)
	// 获取年月日时分秒，使用es6 padStart补0
	let year = time.getFullYear()
	const month = (time.getMonth() + 1).toString().padStart(2, '0')
	const date = (time.getDate()).toString().padStart(2, '0')
	const hours = (time.getHours()).toString().padStart(2, '0')
	const minute = (time.getMinutes()).toString().padStart(2, '0')
	const second = (time.getSeconds()).toString().padStart(2, '0')

	// 替换时间格式
	format = format
		.replace('YYYY', year)
		.replace('MM', month)
		.replace('dd', date)
		.replace('HH', hours)
		.replace('mm', minute)
		.replace('ss', second)

	return format
}


// 数组去重
const dedupe = (array) => {
	return Array.from(new Set(array));
}

// 获取今天几天后时间
const getDate = (num, type) => {
	var date = new Date();
	date.setDate(date.getDate() + num)
	//当使用setDate()之后，date对象就发生了改变，所以之后getDate()就能获取到指定的日期
	//这里的num就是你要加的天数，减也可以。
	let year = date.getFullYear() //年
	let month = date.getMonth() + 1 //月
	let day = date.getDate(); //天数
	let hours = date.getHours();
	let minute = date.getMinutes();
	let second = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = `0${month}`
	}
	if (day >= 1 && day <= 9) {
		day = `0${day}`
	}
	if (hours >= 0 && hours <= 9) {
		hours = `0${hours}`
	}
	if (minute >= 0 && minute <= 9) {
		minute = `0${minute}`
	}
	if (second >= 0 && second <= 9) {
		second = `0${second}`
	}
	switch (type) {
		case 1:
			return `${month}月${day}日 ${hours}:${minute}`
			break;
		case 2:
			return `${year}/${month}/${day} ${hours}:${minute}:${second}`
			break;
		case 3:
			return `${year}/${month}/${day} ${hours}:${minute}`
			break;
		default:
			return `${year}/${month}/${day}`
			break
	}

}

const showTimeNT = (endTime) => {
	let nowTime = new Date(); //获取当前时间
	let setTime = new Date(endTime); //设置结束时间

	let remianAllSeconds = Math.floor((setTime.getTime() - nowTime.getTime()) / 1000); //剩余总秒数
	let remainDays = Math.floor(remianAllSeconds / (60 * 60 * 24)); //剩余天数 
	let remainHours = Math.floor(remianAllSeconds / (60 * 60) % 24); //剩余小时数
	Number(remainHours) < 10 ? remainHours = `0${remainHours}` : remainHours = remainHours
	let remainMinutes = Math.floor(remianAllSeconds / 60 % 60); //剩余分钟数
	Number(remainMinutes) < 10 ? remainMinutes = `0${remainMinutes}` : remainMinutes = remainMinutes
	let remainSeconds = Math.floor(remianAllSeconds % 60); //剩余秒数
	Number(remainSeconds) < 10 ? remainSeconds = `0${remainSeconds}` : remainSeconds = remainSeconds
	let str = `${remainDays}天 ${remainHours}:${remainMinutes}:${remainSeconds}`
	return str;
}

const dateDiff = (hisTime, nowTime) => {
	if (!hisTime) return '';
	const now = nowTime ? new Date(nowTime).getTime() : new Date().getTime()
	const diffValue = now - new Date(hisTime).getTime()
	let result = ""
	const minute = 1000 * 60
	const hour = minute * 60
	const day = hour * 24
	const halfamonth = day * 15
	const month = day * 30
	const year = month * 12

	const _year = diffValue / year
	const _month = diffValue / month
	const _week = diffValue / (7 * day)
	const _day = diffValue / day
	const _hour = diffValue / hour
	const _min = diffValue / minute
	if (_year >= 1) {
		result = parseInt(_year) + "年前"
	} else if (_month >= 1) {
		result = parseInt(_month) + "个月前"
	} else if (_week >= 1) {
		result = parseInt(_week) + "周前"
	} else if (_day >= 1) {
		result = parseInt(_day) + "天前"
	} else if (_hour >= 1) {
		result = parseInt(_hour) + "个小时前"
	} else if (_min >= 1) {
		result = parseInt(_min) + "分钟前"
	} else {
		result = "刚刚"
	}
	return result
}

//删除左右两端的空格
const trim = (str) => {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

// 获取每月有多少天
const getDays = (year, month) => {
	let days = [31, 28, 31, 30, 31, 30, 31, 31, 31, 31, 30, 31]
	if ((year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0)) {
		days[1] = 29
	}
	return days[month]
}

// 获取星期几
const myFunction = (month, day) => {
	let time = new Date().getFullYear() + '-' + month + '-' + day;
	var d = new Date(time);
	var x = d.getDay();
	var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
	var week = weeks[x];
	return week
}


module.exports = {
	IsEmptyStr,
	judgeObjectComplete,
	phoneFun,
	getToday,
	debounce,
	throttle,
	formatDate,
	dedupe,
	getDate,
	showTimeNT,
	dateDiff,
	ifObjIIsNull,
	trim,
	getDays,
	myFunction
}
