function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}
	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time
	return ([hour, minute, second]).map(function (n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}
	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)
	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}

function startCountdown(endTime, callback) {
	const timer = setInterval(() => {
		const now = new Date();
		const distance = endTime - now;
		if (distance < 0) {
			clearInterval(timer);
			callback('已超时');
			return;
		}
		const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
		const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
		const seconds = Math.floor((distance % (1000 * 60)) / 1000);
		callback(`${hours}小时 ${minutes}分钟 ${seconds}秒`);
	}, 1000);
	return timer;
}
//获取两日期之间日期列表函数
function getdiffdate(stime, etime) {
	//初始化日期列表，数组
	var diffdate = new Array();
	var i = 0;
	//开始日期小于等于结束日期,并循环
	while (stime <= etime) {
		diffdate[i] = stime;
		//获取开始日期时间戳
		var stime_ts = new Date(stime).getTime();
		//增加一天时间戳后的日期
		var next_date = stime_ts + (24 * 60 * 60 * 1000);
		//拼接年月日，这里的月份会返回（0-11），所以要+1
		var next_dates_y = new Date(next_date).getFullYear() + '-';
		var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
			'-' : (new Date(next_date).getMonth() + 1) + '-';
		var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
			next_date).getDate();
		stime = next_dates_y + next_dates_m + next_dates_d;
		//增加数组key
		i++;
	}
	return diffdate;
}
/**
 * 计算两个日期之间的天数
 * @param dateString1  开始日期 yyyy-MM-dd
 * @param dateString2  结束日期 yyyy-MM-dd
 * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期，返回0
 */
const getDaysBetween = (dateString1, dateString2) => {
	let startDate = Date.parse(dateString1);
	let endDate = Date.parse(dateString2);
	if (startDate > endDate) {
		return 0;
	}
	if (startDate == endDate) {
		return 1;
	}
	let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
	return days;
}
const days = (date1, date2) => {
	// 将日期字符串转换为 Date 对象
	const startDate = new Date(date1);
	const endDate = new Date(date2);
	// 计算两个日期之间的毫秒数差
	const timeDifference = endDate - startDate;
	// 将毫秒数转换为天数
	const dayDifference = Math.ceil(timeDifference / (1000 * 60 * 60 * 24));
	// 如果不足一天，取1天
	return Math.max(1, dayDifference);
}
/**
 * 乘法函数，用来得到精确的乘法结果
 * @param {Object} arg1
 * @param {Object} arg2
 */
const accMul = (arg1, arg2) => {
	var m = 0;
	m += deal(arg1);
	m += deal(arg2);
	var r1 = Number(arg1.toString().replace('.', ''));
	var r2 = Number(arg2.toString().replace('.', ''));
	return (r1 * r2) / Math.pow(10, m);
}
/**
 * 加法函数，用来得到精确的加法结果
 * @param {Object} arg1
 * @param {Object} arg2
 */
const accAdd = (arg1, arg2) => {
	var r1 = deal(arg1);
	var r2 = deal(arg2);
	var m = Math.pow(10, Math.max(r1, r2));
	return (arg1 * m + arg2 * m) / m;
}
/**
 * 除法函数，用来得到精确的除法结果
 * @param {Object} arg1
 * @param {Object} arg2
 */
const accDiv = (arg1, arg2) => {
	var t1 = deal(arg1);
	var t2 = deal(arg2);
	var r1 = Number(arg1.toString().replace(".", ""))
	var r2 = Number(arg2.toString().replace(".", ""))
	return (r1 / r2) * Math.pow(10, t2 - t1);
}
/**
 * 求小数点后的数据长度
 */
const deal = (arg) => {
	var t = 0;
	try {
		t = arg.toString().split('.')[1].length;
	} catch (e) {}
	return t;
}
/**
 * 判断 有效日期不能小于今天
 */
const dateCompare = (beginDate, endDate) => {
	var beginArrayDate = beginDate.split('-');
	var endArrayDate = endDate.split('-')
	if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true;
	if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) {
		if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true;
		else {
			if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) {
				if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true;
			}
		}
	}
	return false;
}
/**
 * 获取日期
 * day 获取日期
 * time 获取时间
 */
const getDate = (obj = 'day') => {
	const date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let Hours = date.getHours();
	let Minutes = date.getMinutes();
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	Hours = Hours > 9 ? Hours : '0' + Hours;
	Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
	if (obj == 'year') {
		return `${year}`;
	}
	if (obj == 'month') {
		return `${month}`;
	}
	if (obj == 'day') {
		return `${year}-${month}-${day}`;
	}
	if (obj == 'time') {
		return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
	}
}
/**
 * 一些固定的基础数据
 */
const getData = (obj = 'education') => {
	//性别
	if (obj == 'state') {
		return ["离职-随时到岗", "在职-月内到岗", "在职-考虑机会", "在职-暂不考虑"]
	}
	//性别
	if (obj == 'sex') {
		return ["男", "女"]
	}
	//学历
	if (obj == 'qualification') {
		return ["初中及以下", "中专/中技", "高中", "大专", "本科", "硕士", "博士"]
	}
	//求职类型
	if (obj == 'type') {
		return ["全职", "兼职"]
	}
	//工作经验
	if (obj == 'experience') {
		return ["不限", "1年以内", "1-3年", "3-5年", "5-10年", "10年以上"]
	}
	//职位要求
	if (obj == 'positionName') {
		return ["不限", "选择职位"]
	}
	//工作地点
	if (obj == 'address') {
		return ["不限", "选择工作地点"]
	}
	//人员规模
	if (obj == 'nums') {
		return ["0-20人", "20-99人", "100-499人", "500-999人", "1000-9999人", "10000人以上"]
	}
	// 结算方式
	if (obj == 'unit') {
		return ["日结", "单结", ]
	}
}
const toRadians = (degree) => {
	return degree * Math.PI / 180;
}
const distance = (latitude1, longitude1, latitude2, longitude2) => {
	var R = 6371;
	var deltaLatitude = toRadians(latitude2 - latitude1);
	var deltaLongitude = toRadians(longitude2 - longitude1);
	latitude1 = toRadians(latitude1);
	latitude2 = toRadians(latitude2);
	var a = Math.sin(deltaLatitude / 2) *
		Math.sin(deltaLatitude / 2) +
		Math.cos(latitude1) *
		Math.cos(latitude2) *
		Math.sin(deltaLongitude / 2) *
		Math.sin(deltaLongitude / 2);
	var c = 2 * Math.atan2(Math.sqrt(a),
		Math.sqrt(1 - a));
	var d = R * c;
	return d;
}
/**
 * 格式化时间
 */
const format = (obj) => {
	const date = new Date(obj);
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	let Hours = date.getHours();
	let Minutes = date.getMinutes();
	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;
	Hours = Hours > 9 ? Hours : '0' + Hours;
	Minutes = Minutes > 9 ? Minutes : '0' + Minutes;
	return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes;
}
module.exports = {
	formatTime: formatTime,
	formatLocation: formatLocation,
	accMul: accMul,
	accAdd: accAdd,
	accDiv: accDiv,
	dateCompare: dateCompare,
	getDate: getDate,
	getDaysBetween: getDaysBetween,
	getdiffdate: getdiffdate,
	getData: getData,
	distance: distance,
	days: days,
	startCountdown: startCountdown
}