 import dayjs from 'dayjs';

export function formatSeconds(seconds, type) {
	let hours = Math.floor(seconds / 3600);
	let minutes = Math.floor((seconds - (hours * 3600)) / 60);
	let secs = seconds - (hours * 3600 + minutes * 60);

	hours = hours == 0 ? '' : hours.toString().padStart(2, '0');
	minutes = hours == 0 && minutes == 0 ? '' : minutes.toString().padStart(2, '0');
	secs = hours == 0 && minutes == 0&&secs == 0 ? '' : secs.toString().padStart(2, '0');
	// 汉字1，：隔开0
	if(type == 1) {
		return `${hours&&hours+'时'}${minutes&&minutes+'分'}${secs&&secs+'秒'}`
	} else {
		return `${hours&&hours+':'}${minutes&&minutes+':'}${secs}`
	}
};

//新增
var friendlyDate=function(timestamp) {
  	var formats = {
  		'year': '%n% 年前',
  		'month': '%n% 月前',
  		'day': '%n% 天前',
  		'hour': '%n% 小时前',
  		'minute': '%n% 分钟前',
  		'second': '%n% 秒前',
  	};
  	var now = Date.now();
  	var seconds = Math.floor((now - timestamp) / 1000);
  	var minutes = Math.floor(seconds / 60);
  	var hours = Math.floor(minutes / 60);
  	var days = Math.floor(hours / 24);
  	var months = Math.floor(days / 30);
  	var years = Math.floor(months / 12);
  
  	var diffType = '';
  	var diffValue = 0;
  	if (years > 0) {
  		diffType = 'year';
  		diffValue = years;
  	} else {
  		if (months > 0) {
  			diffType = 'month';
  			diffValue = months;
  		} else {
  			if (days > 0) {
  				diffType = 'day';
  				diffValue = days;
  			} else {
  				if (hours > 0) {
  					diffType = 'hour';
  					diffValue = hours;
  				} else {
  					if (minutes > 0) {
  						diffType = 'minute';
  						diffValue = minutes;
  					} else {
  						diffType = 'second';
  						diffValue = seconds === 0 ? (seconds = 1) : seconds;
  					}
  				}
  			}
  		}
  	}
  	return formats[diffType].replace('%n%', diffValue);
}
  
 
//  /**
//   * 格式化时间
//   * @param {Object} 时间字符串
//   */
 
export function formatDate(dateStr) {
 	let formatStr = "";
 	let formatDate=null;
 	if(dateStr==null){
 		return formatStr;
 	}
 	let date = dayjs(dateStr,'YYYY-MM-DD HH:mm:ss');
 	let today = dayjs();
 	if (date.year() != today.year()) {
 		formatDate = date.format('YYYY-MM-DD HH:mm');
 	} 
 	else if (date.month() === today.month()) {
 		switch (date.date() - today.date()) {
 			case 0:
 				formatDate = date.format('今天 HH:mm');
 				break;
 			case -1:
 				formatDate = date.format('昨天 HH:mm');
 				break;
 			case 1:
 				formatDate = date.format('明天 HH:mm');
 				break;
 			default:
 				formatDate = date.format('MM-DD HH:mm');
 				break;
 		}
 	} 
 	else if (date.month() - today.month() === 0 && date.date() === 1) {
 		formatDate = date.format('明天 HH:mm');
 	} 
 	else {
 		formatDate = date.format('MM-DD HH:mm');
 	}
 	return formatDate;
 }
 
 
// /**
//  * 格式化时间
//  * @param {Object} 时间字符串
//  */



var formatHour=function(dateStr) {
	let date = dayjs(dateStr,'YYYY-MM-DD HH:mm:ss');
	let formatHour = date.format('HH:mm');
	return formatHour;
}


// /**
//  * 计算时间差值
//  * @param {startTime} 开始时间
//  * @param {endTime} 结束时间
//  * @param {type} 类型
//  */
var calculateDiffTime=function(startTime, endTime,type=4) {
	// let timeStarts = startTime.getTime(); //开始时间,转换成时间戳
	// let timeEnds = endTime.getTime(); //结束时间,转换成时间戳
	
	let timer = endTime - startTime  //将时间戳进行相减
	
    let year = parseInt((timer / 1000 / 60 / 60 / 24 / 30 / 12)); 
	let month = parseInt((timer / 1000 / 60 / 60 / 24 / 30));
	let day = parseInt((timer / 1000 / 60 / 60 / 24));
	let hour = parseInt((timer / 1000 / 60 / 60 % 24)); 
	let minute = parseInt((timer / 1000 / 60 % 60));  
	let second = parseInt((timer / 1000 % 60)); 

	if(hour>0){
		return (hour>10?hour:'0'+hour) + ':' + (minute>10?minute:'0'+minute) + ':' + (second>10?second:'0'+second);
	}else if(minute>0){
		return (minute>=10?minute:'0'+minute) + ':' + (second>=10?second:'0'+second);
	}else if(second>0){
		return  '00:'+(second>=10?second:'0'+second);
	}
	
 //   if (type === 1) { // 返回相差年数
	// 	return year + '年'
	// } else if (type === 2) { // 返回相差年数月数
	// 	return year + '年' + month + '月'
	// } else if (type === 3) { // 返回相差年数月数天数
	// 	return year + '年' + month + '月' + day + '日'
	// } else { // 返回相差年数月数天数时分秒
	// 	return year + '年' + month + '月' + day + '日' + hour + '时' + minute + '分' + second + '秒'
	// }
	
}


 