 import dayjs from 'dayjs';
 
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 + '秒'
	// }
	
}


export function formatSeconds(seconds) {  
    const hours = Math.floor(seconds / 3600);
	const minutes = Math.floor((seconds % 3600) / 60);
	const secs = seconds % 60;
	if(seconds<60){
		return [minutes,secs].map(unit => String(unit).padStart(2, '0')).join(':');
	}
	else if(seconds>=60 && seconds<3600){
		return [minutes, secs].map(unit => String(unit).padStart(2, '0')).join(':');
	}else{
		return [hours, minutes, secs].map(unit => String(unit).padStart(2, '0')).join(':');
	}
 };
 
 
export function caculateTimeago(dateTimeStamp,type) {
    const minute = 1000 * 60;      // 把分，时，天，周，半个月，一个月用毫秒表示
    const hour = minute * 60;
    const day = hour * 24;
    const week = day * 7;
    const now = new Date().getTime();   // 获取当前时间毫秒
    const diffValue = now - dateTimeStamp;// 时间差
    let result = '';
  
    if (diffValue < 0) {
      return;
    }
    const minS = diffValue / 1000;  // 计算时间差的秒
    const minC = diffValue / minute;  // 计算时间差的分，时，天，周，月
    const hourC = diffValue / hour;
    const dayC = diffValue / day;
    const weekC = diffValue / week;
    if (weekC >= 1 && weekC <= 4) {
      result = ` ${parseInt(`${weekC}`, 10)}周前`;
    } else if (dayC >= 1 && dayC <= 6) {
      result = ` ${parseInt(`${dayC}`, 10)}天前`;
    } else if (hourC >= 1 && hourC <= 23) {
      result = ` ${parseInt(`${hourC}`, 10)}小时前`;
    } else if (minC >= 1 && minC <= 59) {
      result = ` ${parseInt(`${minC}`, 10)}分钟前`;
    } else if (diffValue >= 0 && diffValue <= minute) {
      // result = '刚刚';
 	 const num = parseInt(`${minS}`, 10)
 	 if(num == 0) {
 		result = '刚刚';
 	 } else {
 		result = ` ${parseInt(`${minS}`, 10)}秒前`;
 	 }
    } else {
  	  if(type==1) {
  		result = '';  
  	  } else {
  		const datetime = new Date();
  		datetime.setTime(dateTimeStamp);
  		const Nyear = datetime.getFullYear();
  		const Nmonth = datetime.getMonth() + 1 < 10 ? `0${datetime.getMonth() + 1}` : datetime.getMonth() + 1;
  		const Ndate = datetime.getDate() < 10 ? `0${datetime.getDate()}` : datetime.getDate();
  		result = `${Nyear}-${Nmonth}-${Ndate}`; 
  	  }
    }
    return result;
  }
  
  /**
   * 格式销售数量
   * @param {Number} 数字
   */
export function formatSalesNum(saleNum) {
  	const num = Number(saleNum || 0)
  	if (num < 1000) {
  		return num
  	}else if (num < 10000 && num >= 1000) {
  		let numArr = ((num / 1000) + '').split('.')
  		if (numArr.length > 1) {
  			if (numArr[1][0] === '0') {
  				return numArr[0] + '000+'
  			} else {
  				return numArr[0] + numArr[1][0] + '00+'
  			}
  		} else {
  			return (numArr[0] + '000+')
  		}
  	} else if (num < 10000000 && num >= 10000) {
  		let numArr = ((num / 10000) + '').split('.')
  		if (numArr.length > 1) {
  			if (numArr[1][0] === '0') {
  				return numArr[0] + '万+'
  			} else {
  				return numArr[0] + '.' + numArr[1][0] + '万+'
  			}
  		} else {
  			return (numArr[0] + '万+')
  		}
  	} else if (num > 100000000 && num >= 10000000) {
  		let numArr = ((num / 10000000) + '').split('.')
  		if (numArr.length > 1) {
  			if (numArr[1][0] === '0') {
  				return numArr[0] + '千万+'
  			} else {
  				return numArr[0] + '.' + numArr[1][0] + '千万+'
  			}
  		} else {
  			return (numArr[0] + '千万+')
  		}
  	} else {
  		let numArr = ((num / 100000000) + '').split('.')
  		if (numArr.length > 1) {
  			if (numArr[1][0] === '0') {
  				return numArr[0] + '亿+'
  			} else {
  				return numArr[0] + '.' + numArr[1][0] + '亿+'
  			}
  		} else {
  			return (numArr[0] + '亿+')
  		}
  	}
  }