export function setCookie(name, value) {
	if (value) {
		var days = 1; //定义一天
		var exp = new Date();
		exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
		// 写入Cookie, toGMTString将时间转换成字符串
		document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
	}
}

export function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if (cval != null) {
		document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
	}
}

export function getCookie(cookie_name) {
	var allcookies = document.cookie;
	//索引长度，开始索引的位置
	var cookie_pos = allcookies.indexOf(cookie_name);
	// 如果找到了索引，就代表cookie存在,否则不存在
	if (cookie_pos != -1) {
		// 把cookie_pos放在值的开始，只要给值加1即可
		//计算取cookie值得开始索引，加的1为“=”
		cookie_pos = cookie_pos + cookie_name.length + 1;
		//计算取cookie值得结束索引
		var cookie_end = allcookies.indexOf(";", cookie_pos);
		if (cookie_end == -1) {
			cookie_end = allcookies.length;
		}
		//得到想要的cookie的值
		var value = unescape(allcookies.substring(cookie_pos, cookie_end));
	}
	return value;
}

export function getUrlParam(paraName) {
	var url = document.location.toString();
	var arrObj = url.split("?");
	if (arrObj.length > 1) {
		var arrPara = arrObj[1].split("&");
		var arr;
		for (var i = 0; i < arrPara.length; i++) {
			arr = arrPara[i].split("=");
			if (arr != null && arr[0] == paraName) {
				return arr[1];
			}
		}
		return "";
	} else {
		return "";
	}
}

//时间戳转时间
export function timestampToTime(timestamp) {
	//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	if (timestamp.toString().length == 10) { 
		timestamp = timestamp * 1000;
	}
	var date = new Date(timestamp);
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + ':';
	var s = date.getSeconds();
	return Y + M + D + h + m + s;
}

//时间转年月日
export function timeToDate(time){
	if(time!=""&&time!=null&&time!=undefined){
		let date=new Date(time);
		let y = date.getFullYear();
		let m = date.getMonth() + 1;
		let d = date.getDate();
		m = m < 10 ? "0" + m : m;
		d = d < 10 ? "0" + d : d;
		return y + "-" + m + "-" + d;
	}
	return "";
}

// $com的赋值地方
export const common = {
	dev: process.env.NODE_ENV === "development",
	fmeaId: getUrlParam("fmeaId"),
	deriveId: getUrlParam("deriveId") == "undefined" ? "" : getUrlParam("deriveId"),
	from: getUrlParam("from"),
	history: getUrlParam("history"),
	setCookie: setCookie,
	getCookie: getCookie,
	delCookie: delCookie,
	getUrlParam: getUrlParam,
	timestampToTime: timestampToTime,
	timeToDate:timeToDate
}
