var flag = true,param = null,pStatus = true,goodStatus = true,shareOpenid = '';

mui.ready(function() {
	var nowUrl = location.href;
	var productId = getParam("productId"); //
	param = nowUrl.split("?")[1];
	// 获取产品信息
	selectProductInfo(productId);
	// 添加购物车
	mui('body').on('tap', '#add-cart-div', function() {
		if(!pStatus) {
			mui.alert("只有在售商品才可以添加购物车!");
			return false;
		}
		if(!goodStatus) {
			mui.alert("该商品暂时缺货,不能购买!");
			return false;
		}
		var btnArray = ['否', '是'];
		mui.ajax('/iamberry/wechat/cart/addProductToCart?' + param + '&dates=' + new Date().getTime(), {
			dataType: 'json',
			type: 'GET',
			timeout: 15000,
			success: function(dt) {
				if(dt.status) {
					window.location.href = '/iamberry/wechat/wxCart/goCart';
				} else {

					mui.confirm(dt.message + '是否刷新页面？', '爱贝源-提示', btnArray, function(e) {
						if(e.index == 1) {
							location.reload(true);
						}
					});
				}
			},
			error: function(xhr, type, errorThrown) {
				errorfn(xhr);
			}
		});
	});

	mui('body').on('tap', '#click-product-desc', function() {
		if(flag) {
			// 获取产品信息
			selectProductImageText(productId);
			flag = false;
		}
	});
	mui('body').on('tap', '.video-right', function() {
		mui.openWindow({
			url: base_path + '/indexVideo'
		});
	});
	mui('body').on('tap', '.icon-guanbi', function() {
		document.querySelector('.go-video').style.display = 'none';
	});
	mui('body').on('tap', '.go_gwc', function() {
		//打开购物车
		mui.openWindow({
			url: base_path + '/wxCart/goCart'
		});
	});

	mui('body').on('tap', '#shopp-div', function() {
		if(!pStatus) {
			mui.alert("该商品已经下架,请重新挑选商品购买!");
			return false;
		}
		if(!goodStatus) {
			mui.alert("该商品暂时缺货,不能购买!");
			return false;
		}
		var btnArray = ['否', '是'];
		mui.ajax('/iamberry/wechat/cart/addProductToCart?' + param + '&dates=' + new Date().getTime(), {
			dataType: 'json',
			type: 'GET',
			timeout: 15000,
			success: function(dt) {
				if(dt.status) {
					window.location.href = '/iamberry/pay/settlement?cartId=' + dt.data + "&tiemstamp=" + new Date().getTime();
				} else {
					mui.confirm(dt.message, '爱贝源-提示', btnArray, function(e) {
						if(e.index == 1) {
							//location.reload(true);
						}
					});
				}
			},
			error: function(xhr, type, errorThrown) {
				errorfn(xhr);
			}
		});
	});
	var url = domanUrl + "/iamberry/wechat/product/desc?productId=" + productId + "&shareOpenid=" + shareOpenid;
	//添加分享
	window.repleFlag = false;
	window.wxFriend = {
		"appId": "",
		"imgUrl": domanUrl + '/iamberry/common/shop/images/index.jpg',
		"link": url,
		"desc": "智能冲奶机，让全中国的妈妈睡个好觉！点击进入更多活动，粉丝福利，积分返现....", //描述
		"title": "爱贝源，一家主打中国母婴智能硬件产品官方商城", //标题
		"netError": "您的网络异常，请刷新重试!如多次刷新无效，请向我们反应 ~",
		"shareTimeSuccess": "分享成功", //分享朋友圈后回调
		"shareTimeCancel": "已取消分享", //取消分享朋友圈后回调
		"shareAppSuccess": "分享成功!", //分享好友后回调
		"shareAppCancel": "已取消分享" //取消分享好友后回调
	};
	getConfig();
	// 数据加载完成,hide 弹出层
	document.querySelector('.loading-bg').style.display = 'none';
});

// 产品基本信息
function selectProductInfo(productId) {
	var btnArray = ['否', '是'];
	mui.ajax('/iamberry/wechat/product/getProductByProductId?productId=' + productId + '&dates=' + new Date().getTime(), {
		dataType: 'json',
		async: false,
		type: 'GET',
		timeout: 15000,
		success: function(dt) {
			//console.log(dt)
			if(dt.status) {
				console.log(dt.data.product);
				goodStatus = false;
//				if(dt.data.product.productId == 28) {
//					goodStatus = false;
//				}

				shareOpenid = dt.data.shareOpenid;
				var imageNote = "<div class='mui-slider-item mui-slider-item-duplicate'>" + "<a href='#'>" + "<img src='" + dt.data.product.pictureList[0].pictureUrl + "'>" + "</a>" + "</div>";
				for(var i = 0; i < dt.data.product.pictureList.length; i++) {
					imageNote += "<div class='mui-slider-item'>" + "<a href='#'>" + "<img src='" + dt.data.product.pictureList[i].pictureUrl + "'>" + "</a>" + "</div>";
					if(i == 0) {
						$("#active-div").append("<div class='mui-indicator mui-active'></div>");
					} else {
						$("#active-div").append("<div class='mui-indicator'></div>");
					}
				}
				imageNote += "<div class='mui-slider-item mui-slider-item-duplicate'>" + "<a href='#'>" + "<img src='" + dt.data.product.pictureList[0].pictureUrl + "'>" + "</a>" + "</div>";
				$("#group-images-div").append(imageNote);
				var banner = mui('#detail-slider');
				banner.slider({
					interval: 5000
				});

				$("#product-name-font").html(dt.data.product.productName);
				$("#product-price-span").html("¥" + accDiv(dt.data.product.productDiscount, 100));
				$("#product-price-s").html("¥" + accDiv(dt.data.product.productPrice, 100));
				$("#product-access-span").html(dt.data.product.productSalesnum + "人已购买");
				$("#product-param-img").html("<img style='width:100%;height: 100%' src='" + dt.data.product.productParameterImg + "'>");
				if(dt.data.product.productStatus == 1) {
					pStatus = true;
				} else {
					pStatus = false;
				}
			} else {
				mui.confirm(dt.message + '是否重新加载页面？', '爱贝源-提示', btnArray, function(e) {
					if(e.index == 1) {
						location.reload(true);
					}
				});
			}
		},
		error: function(xhr, type, errorThrown) {
			errorfn(xhr);
		}
	});
}
// 获取产品详情
function selectProductImageText(productId) {
	var btnArray = ['否', '是'];
	mui.ajax('/iamberry/wechat/product/getPictureByProductId?productId=' + productId, {
		dataType: 'json',
		type: 'POST',
		timeout: 15000,
		success: function(dt) {
			if(dt.status) {
				//console.log(dt.data)
				if(dt.data != null) {
					$("#item2").html(dt.data.imagetextContext);
				} else {
					$("#item2").empty();
					mui.alert('商品详情获取失败');
				}
			} else {
				mui.confirm(dt.message + '是否刷新页面？', '爱贝源-提示', btnArray, function(e) {
					if(e.index == 1) {
						location.reload(true);
					}
				});
			}
		},
		error: function(xhr, type, errorThrown) {
			mui.confirm('您的网络开小差了，是否重新加载页面？', '爱贝源-提示', btnArray, function(e) {
				if(e.index == 1) {
					location.reload(true);
				}
			});
		}
	});
}
var mask = mui.createMask(function() {}); //callback
mui('body').on('tap', '.share_wechat', function() {
	mask.show(); //显示遮罩
});