import store from '../store/index.js'
var formatTime = function(t) {
		var e = t.getFullYear(),
			a = t.getMonth() + 1,
			r = t.getDate(),
			n = t.getHours(),
			o = t.getMinutes(),
			i = t.getSeconds();
		return [e, a, r].map(formatNumber).join("/") + " " + [n, o, i].map(formatNumber).join(":");
	},
	formatNumber = function(t) {
		return (t = t.toString())[1] ? t : "0" + t;
	};

function validTime(t, e) {
	var a = t.split("-"),
		r = e.split("-"),
		n = new Date(parseInt(a[0]), parseInt(a[1]) - 1, parseInt(a[2]), 0, 0, 0),
		o = new Date(parseInt(r[0]), parseInt(r[1]) - 1, parseInt(r[2]), 0, 0, 0);
	return !(n.getTime() >= o.getTime()) || (console.log("结束日期不能小于开始日期", this), !1);
}

function validTime1(t, e) {
	var a = t.split("-"),
		r = e.split("-"),
		n = new Date(parseInt(a[0]), parseInt(a[1]) - 1, parseInt(a[2]), 0, 0, 0),
		o = new Date(parseInt(r[0]), parseInt(r[1]) - 1, parseInt(r[2]), 0, 0, 0);
	return !(n.getTime() > o.getTime()) || (console.log("结束日期不能小于开始日期", this), !1);
}

function getRandomNum() {
	for (var t = "" + Math.round(1e6 * Math.random()); t.length < 6;) t = "0" + t;
	return console.info("randomNum is ========", t), t;
}

function in_array(t, e) {
	for (var a = 0; a < e.length; a++) {
		if (e[a] == t) return 1;
	}
	return 2;
}

function getDistance(t, e, a, r) {
	e = e || 0, a = a || 0, r = r || 0;
	var n = (t = t || 0) * Math.PI / 180,
		o = a * Math.PI / 180,
		i = n - o,
		s = e * Math.PI / 180 - r * Math.PI / 180;
	return 12756274 * Math.asin(Math.sqrt(Math.pow(Math.sin(i / 2), 2) + Math.cos(n) * Math.cos(o) * Math.pow(Math.sin(
		s / 2), 2)));
}

function getNowFormatDate() {
	var t = new Date(),
		e = t.getMonth() + 1,
		a = t.getDate();
	return 1 <= e && e <= 9 && (e = "0" + e), 0 <= a && a <= 9 && (a = "0" + a), t.getFullYear() + "/" + e + "/" + a +
		" " + t.getHours() + ":" + t.getMinutes() + ":" + t.getSeconds();
}

function xctsfm(t, e) {
	var a = new Date(t.replace(/-/g, "/")),
		r = new Date(e),
		n = parseInt((r.getTime() - a.getTime()) / 1e3),
		o = Math.floor(n / 86400 / 365);
	n %= 31536e3;
	var i = Math.floor(n / 86400 / 30);
	n %= 2592e3;
	var s = Math.floor(n / 86400);
	n %= 86400;
	var m = Math.floor(n / 3600);
	n %= 3600;
	var u = Math.floor(n / 60),
		g = n %= 60;
	console.log(o, i, s, m, u, g);
	var l = {};
	return l.day = s, l.hour = m, l.minute = u, l;
}

function ormatDate(t) {
	var e = new Date(1e3 * t);
	return e.getFullYear() + "-" + a(e.getMonth() + 1, 2) + "-" + a(e.getDate(), 2) + " " + a(e.getHours(), 2) + ":" +
		a(e.getMinutes(), 2) + ":" + a(e.getSeconds(), 2);

	function a(t, e) {
		for (var a = "" + t, r = a.length, n = "", o = e; o-- > r;) n += "0";
		return n + a;
	}
}

//打开对应模块属性项
function c(arr, index) {
	arr.forEach((res, i) => {
		if (index == i) {
			res.o = true;
		} else {
			res.o = false;
		}
	})
}
// 图片路径
function icon(img) {
	return require('@/static/' + img);
}

//列表-打开对应模块属性项
function openAttr(arr, index) {
	console.log("this", arr)
	let that = this;
	arr.forEach((res, i) => {
		if (index == i && res.link.g) {
			// console.log("res.link", res.link)
			if (res.link.n == 'openVideo') {
				// #ifdef MP-WEIXIN
				if (res.link.id && res.link.feedId) {
					wx.openChannelsActivity({
						finderUserName: res.link.id,
						feedId: res.link.feedId,
						success: () => {
							console.log("打开成功")
						}
					})
				}else {
					wx.openChannelsUserProfile({
						finderUserName: res.link.id,
						success: () => {
							console.log("打开成功")
						}
					})
				}
				// #endif

				// #ifdef MP-ALIPAY
				uni.showToast({
					title: "支付宝不支持喔！",
					icon: "none"
				})
				// #endif

			} else if (res.link.n == 'openVideoRood') {
				// #ifdef MP-WEIXIN
			 	uni.navigateTo({
			 		url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=' + res.link.id
			 	})
				// #endif
				// #ifdef MP-ALIPAY
				uni.showToast({
					title: "支付宝不支持喔！",
					icon: "none"
				})
				// #endif
			 } else if (res.link.p == '积分商品') {
				uni.navigateTo({
					url: res.link.n + '?id=' + res.link.i
				})
			} else if (res.link.p == '跳转小程序') {
				// #ifdef MP-WEIXIN
				var appid = res.link.w;
				// #endif
				// #ifdef MP-ALIPAY
				var appid = res.link.a;
				// #endif
				
				var userInfo = store.state.userInfo;
				var mobile = userInfo.mobile,path='',
					type = userInfo.mobile ? 1 : 2;
					if('wx394da23b82f9863a' == appid){
						 path =`${res.link.s}?mobile=${mobile}&type=${type}`; //跳转智豆商圈
					}else{
						path = res.link.s;
					}
				uni.navigateToMiniProgram({
					appId: appid,
					path: path,
					success(res) {
						// 打开成功
					}
				})

			} else if (res.link.p == "商品分类") {
				// console.log(res.link.id,"id")
				store.commit('SET_CATE_ID', res.link.id || '')
				uni.switchTab({
					url: "../../pages/index/index"
				})
			} else if (res.link.p == "文章详情") {
			 	// console.log(res.link.id,"id")
			 	uni.navigateTo({
			 		url: `/shop/richText/index?id=${res.link.id}`
			 	})
			 }
			 else if (res.link.m == '自定义链接' && res.link.t == 4) {
				uni.setStorageSync("web", res.link.n)
				uni.navigateTo({
					url: "/pages/web/web"
				})
			} else {
				console.log("openAttr", res.link.p)
				if (res.link.p == '堂食下单') {
					store.commit('SET_TEEN_INDEX', 1)
					uni.switchTab({
						url: res.link.n
					})
				} else if (res.link.p == '外卖下单') {
					store.commit('SET_TEEN_INDEX', 2)
					uni.switchTab({
						url: res.link.n
					})
				} else if (res.link.p == '自提下单') {
					let systemInfo = store.state.systemInfo;
					if (systemInfo.pay_type == 2 && systemInfo.is_ziti == 1) {
						uni.showToast({
							icon: "none",
							title: '餐后付款不能自提!'
						})
						return
					}
					store.commit('SET_TEEN_INDEX', 3)
					// console.log(store.state.systemInfo)
					uni.switchTab({
						url: res.link.n
					})
				} else if (res.link.p == '订单中心') {
					store.commit('SET_TABACTIVE', 1)
					uni.switchTab({
						url: res.link.n
					})
				} else if (res.link.p == '个人中心') {
					uni.switchTab({
						url: res.link.n
					})
				} else {
					uni.navigateTo({
						url: res.link.n
					})
				}
			}
		}
	})
}

// 单图-打开对应模板
function openOnly(link) {
	// console.log(link)
	if (link.g) {
		console.log("link", link)
		if (link.n == 'openVideo') {
			// #ifdef MP-WEIXIN
			if (link.id && link.feedId) {
				wx.openChannelsActivity({
					finderUserName: link.id,
					feedId: link.feedId,
					success: () => {
						console.log("打开成功")
					}
				})
			} else {
				wx.openChannelsUserProfile({
					finderUserName: link.id,
					success: () => {
						console.log("打开成功")
					}
				})
			}
			// #endif

			// #ifdef MP-ALIPAY
			uni.showToast({
				title: "支付宝不支持喔！",
				icon: "none"
			})
			// #endif

		}else if (link.n == 'openVideoRood') {
			 	// #ifdef MP-WEIXIN
				uni.navigateTo({
			 		url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=' + link.id
			 	})
				// #endif
				// #ifdef MP-ALIPAY
				uni.showToast({
					title: "支付宝不支持喔！",
					icon: "none"
				})
				// #endif
				
		} else if (link.p == '积分商品') {
			uni.navigateTo({
				url: link.n + '?id=' + link.i
			})
		} else if (link.p == "商品分类") {
			// console.log(res.link.id,"id")
			store.commit('SET_CATE_ID', link.id || '')
			uni.switchTab({
				url: "../../pages/index/index"
			})
		} 
		else if (link.p == "文章详情") {
			uni.navigateTo({
				url: `/shop/richText/index?id=${link.id}`
			})
		} 
		else if (link.p == '跳转小程序') {
			// #ifdef MP-WEIXIN
			var appid = link.w;
			// #endif
			// #ifdef MP-ALIPAY
			var appid = link.a;
			// #endif
			
			var userInfo = store.state.userInfo;
			
			var mobile = userInfo.mobile,path='',
			
				type = userInfo.mobile ? 1 : 2;
				
				if('wx394da23b82f9863a' == appid){
					 path =`${link.s}?mobile=${mobile}&type=${type}`; //跳转智豆商圈
				}else{
					path = link.s;
				}
				
				uni.navigateToMiniProgram({
					appId: appid,
					path:path ,
					success(res) {
						// 打开成功
					}
				})
				
		} else if (link.m == '自定义链接' && link.t == 4) {
			uni.setStorageSync("web", link.n)
			uni.navigateTo({
				url: "/pages/web/web"
			})
		} else {
			// console.log("openAttr",link.p)
			if (link.p == '堂食下单') {
				store.commit('SET_TEEN_INDEX', 1)
				uni.switchTab({
					url: link.n
				})
			} else if (link.p == '外卖下单') {
				store.commit('SET_TEEN_INDEX', 2)
				uni.switchTab({
					url: link.n
				})
			} else if (link.p == '自提下单') {
				let systemInfo = store.state.systemInfo;
				if (systemInfo.pay_type == 2 && systemInfo.is_ziti == 1) {
					uni.showToast({
						icon: "none",
						title: '餐后付款不能自提!'
					})
					return
				}
				store.commit('SET_TEEN_INDEX', 3)
				// console.log(store.state.systemInfo)
				uni.switchTab({
					url: link.n
				})
			} else if (link.p == '订单中心') {
				store.commit('SET_TABACTIVE', 1)
				uni.switchTab({
					url: link.n
				})
			} else if (link.p == '个人中心') {
				uni.switchTab({
					url: link.n
				})
			} else {
				uni.navigateTo({
					url: link.n
				})
			}
		}
	}
}

function openGoods(arr, index) {
	arr.forEach((res, i) => {
		if (index == i) {
			// 跳转商品默认堂食
			store.commit('SET_TEEN_INDEX', 1)
			uni.navigateTo({
				url: "/menber/goods_detail?id=" + res.cm.id
			})
		}
	})
}


function openMsGoods(arr, index) {
	arr.forEach((res, i) => {
		if (index == i) {
			console.log(res)
			uni.navigateTo({
				url: "/miaosha/detail?id=" + res.id
			})
		}
	})
}

function openYyGoods(arr, index) {
	arr.forEach((res, i) => {
		if (index == i) {
			console.log(res)
			uni.navigateTo({
				url: "/subscribe/shopDetail?id=" + res.id
			})
		}
	})
}

function openPtGoods(arr, index) {
	arr.forEach((res, i) => {
		if (index == i) {
			console.log(res)
			uni.navigateTo({
				url: "/pintuan/detail?goods_id="+res.id+"&collage_id="+ res.collage_id
			})
		}
	})
}


function time_range(beginTimes, endTimes, nowTime) { //判断是否在缴费查询时间

 
	let beginTime = beginTimes + ':00',
		endTime = endTimes + ':00';

	var strb = beginTime.split(":");
	if (strb.length != 3) {
		return false;
	}

	var stre = endTime.split(":");
	if (stre.length != 3) {
		return false;
	}

	var strn = nowTime.split(":");
	if (stre.length != 3) {
		return false;
	}
	var b = new Date();
	var e = new Date();
	var n = new Date();

	b.setHours(strb[0]);
	b.setMinutes(strb[1]);
	b.setSeconds(strb[2]);
	e.setHours(stre[0]);
	e.setMinutes(stre[1]);
	e.setSeconds(stre[2]);
	n.setHours(strn[0]);
	n.setMinutes(strn[1]);
	n.setSeconds(strn[2]);
	if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
		console.log("当前时间是：" + n.getHours() + ":" + n.getMinutes() + ":" + n.getSeconds() + "，在该时间范围内！");
		return true;
	} else {
		console.log("当前时间是：" + n.getHours() + ":" + n.getMinutes() + ":" + n.getSeconds() + "，不在该时间范围内！");
		return false;
	}
}
function setTimeList(e) {
    function t(e) {
      return e <= 0 && (e = 0), e < 10 ? "0" + e : e
    }
    var o = "00",
      n = "00",
      a = "00",
      i = 0,
      r = "",
      s = "",
      c = "";
    if (86400 <= e && (i = parseInt(e / 86400), e %= 86400, r += i + "天", s += i + "天", c += i + "天"), e < 86400) {
      var p = parseInt(e / 3600);
      e %= 3600, r += (a = t(p)) + "小时", s += a + ":", c = 0 < i || 0 < p ? c + a + ":" : ""
    }
    return e < 3600 && (n = t(parseInt(e / 60)), e %= 60, r += n + "分", s += n + ":", c += n + ":"), e < 60 && (r += (o = t(e)) + "秒", s += o, c += o), {
      d: i,
      h: a,
      m: n,
      s: o,
      content: r,
      content_1: s,
      content_ms: c
    }
  }
  
module.exports = {
	time_range,
	icon: icon,
	formatTime: formatTime,
	getRandomNum: getRandomNum,
	in_array: in_array,
	getDistance: getDistance,
	validTime: validTime,
	validTime1: validTime1,
	getNowFormatDate: getNowFormatDate,
	xctsfm: xctsfm,
	ormatDate: ormatDate,
	openAttr: openAttr,
	openOnly: openOnly,
	openGoods: openGoods,
	setTimeList,
	openMsGoods,
	openPtGoods,
	openYyGoods,
};
