import {
	formatTimestamp
} from '@/common/util/utils'

export const getYear = value => { //时间戳转年份过滤器
	return formatTimestamp(+value)[0]
}

export const getMonth = value => { //时间戳转月份过滤器
	const month = formatTimestamp(+value)[1] + ''
	return month.length === 1 ? '0' + month : month //月份只有一位数字时补零
}

export const getDay = value => { //时间戳转周过滤器
	return formatTimestamp(+value)[2]
}

export const getDate = value => { //时间戳转 日 过滤器
	const date = formatTimestamp(+value)[3] + ''
	return date.length === 1 ? '0' + date : date //日期份只有一位数字时补零
}

export const getHousr = value => { //时间戳转 时 过滤器
	const hour = formatTimestamp(+value)[4] + ''
	return hour.length === 1 ? '0' + hour : hour // 小时只有一位时补零
}

export const getMinute = value => { //时间戳转 分 过滤器
	const minute = formatTimestamp(+value)[5] + ''
	return minute.length === 1 ? '0' + minute : minute // 分钟只有一位时补零
}

export const getSecond = value => { //时间戳转 秒 过滤器
	return formatTimestamp(+value)[6]
}

export const formatTime = value => { //格式化时间
	return `${getYear(value)}-${getMonth(value)}-${getDate(value)} ${getHousr(value)}:${getMinute(value)}`
}

export const formatDate = value => { //格式化日期
	return `${getYear(value)}-${getMonth(value)}-${getDate(value)}`
}

export const formatHourMin = value => { //格式化时分
	return `${getHousr(value)}:${getMinute(value)}`
}

export const formatActivity = value => { //格式化时分
	return `${getMonth(value)}月${getDate(value)}日 ${getHousr(value)}:${getMinute(value)}`
}

export const numDot = num => {
	if (!num) return 0
	return (num + '').replace(/(?=(?:\d{3})+(?!\d))/g, ',').replace(/^,/, '') || ''
}

export const getOrderStatus = val => {
	if (+val === 0) {
		return '待付款'
	} else if (+val === 1) {
		return '待发货'
	} else if (+val === 2) {
		return '配送中'
	} else if (+val === 3) {
		return '已完成'
	} else if (+val === 4) {
		return '已取消'
	} else {
		return ''
	}
}

export const getAddressString = val => {
	return val ? `${val.provice}-${val.city}-${val.area}-${val.address}` : ''
}

export const DistanceNow = t => { // 计算动态距离现在的发布时间
	t = (t + '').length === 10 ? `${t}000` : t
	const d = Date.now() - Number(t)
	if (d >= 0 && d < 10800000) {
		return '刚刚发表'
	} else if (d >= 10800000 && d < 86400000) {
		return `${Math.floor(d / 3600000)}小时前`
	} else if (d >= 86400000) {
		return Math.floor(d / 86400000) > 9 ? formatDate(t) : `${Math.floor(d / 86400000)}天前`
	}
}

export function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return decodeURI(r[2]);
	return null;
}

export function dateFormatter(timestamp, fmt) {
	if (!timestamp) return ""
	timestamp = typeof timestamp === 'string' ? timestamp.replace(/-/g, '/') : timestamp
	fmt = fmt || "yyyy-MM-dd";
	const $this = new Date(timestamp);
	const o = {
		"M+": $this.getMonth() + 1,
		"d+": $this.getDate(),
		"h+": $this.getHours(),
		"m+": $this.getMinutes(),
		"s+": $this.getSeconds(),
		"q+": Math.floor(($this.getMonth() + 3) / 3),
		S: $this.getMilliseconds()
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(
			RegExp.$1,
			($this.getFullYear() + "").substr(4 - RegExp.$1.length)
		);
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(
				RegExp.$1,
				RegExp.$1.length === 1 ?
				o[k] :
				("00" + o[k]).substr(("" + o[k]).length)
			);
		}
	}
	return fmt;
}
export const remove = val => {
	if (!val) {
		return false;
	}
	return val.replace(/&nbsp;/ig, '').replace(/&mdash;/ig, '').replace(/&ldquo;/ig, '').replace(/&lsquo;/ig, '')
		.replace(/&rdquo;/ig, '');
}


/*店铺使用*/
export const blanceFmt = val => { //分转化为元
	if (!val) {
		return '0.00'
	}
	var num = Number(val);
	if (!num) { //等于0
		return num + '.00';
	} else { //不等于0
		num = Math.round((num) * 100) / 10000;
		num = num.toFixed(2);
		num += ''; //转成字符串
		var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g; //千分符的正则
		return num.replace(reg, '$1,') //千分位格式化
	}

};
export const getName = (val, num) => { //店铺名字截取
	if (!val) return '';
	if (num) {
		return val.length > num ? val.slice(0, num) + '...' : val;
	} else {
		return val.length > 10 ? val.slice(0, 10) + '...' : val;
	}

}

export const hideMiddle = (val, num) => { //隐藏号码
	if (val === null || !val) {
		return '';
	} else {
		return `${val.substring(0, num)}************${val.substring(val.length - num)}`;
	}
}
export const getStatus = val => { //显示订单的状态
	switch (val) {
		case 0:
			return '待付款';
			break;
		case 1:
			return '待发货';
			break;
		case 2:
			return '待收货';
			break;
		case 3:
			return '待评价';
		default:
			break;
	}
}
export const formatRichText = html => {
	//显示富文本信息控制小程序中图片大小
	if (html) {
		let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
			match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
			match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
			match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
			return match;
		});
		newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
			match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
				'max-width:100%;');
			return match;
		});
		newContent = newContent.replace(/<br[^>]*\/>/gi, '');
		newContent = newContent.replace(/\<img/gi,
			'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
		return newContent;
	}
}
//总金额
export const totalPrice = data => {
	let totalPrice = 0;
	data.map(i => {
		i.sku.map(j => {
			totalPrice += j.num * j.price;
		});
	});
	return totalPrice;
}
//总数量
export const totalNum = data => {
	let totalNum = 0;
	data.map(i => {
		i.sku.map(j => {
			totalNum += j.num;
		});
	});
	return totalNum;
}
//小计数量
export const subNum = data => {
	let subNum = 0;
	data.map(i => {
		subNum += i.num;
	});
	return subNum;
}

//小计金额
export const subtotal = data => {
	let subtotal = 0;
	data.map(j => {
		subtotal += j.num * j.price;
	});
	return subtotal;
}
/*店铺使用end*/
