/**
 *	购物车的操作 
 */ 
$(function(){
	
	function sku_open(){
		$(".buyContainer").show().animate({'bottom':"0"});;
		$(".buyContainerBg").show(); 
		$(".add_chose").hide();
		$(".add_sure").show();
	}
	function sku_close(){ 
		$(".buyContainer").hide().css({'bottom':"-500px"});
		$(".buyContainerBg").hide(); 
		$(".sku dl dd a").removeClass('selected');
	}
	function sku_null(){ 
		sku_type = $("input#sku_id").attr('data-sku'); 
		if(sku_type == '1'){
			$("input#sku_id").val('');
		}
	}
	//　关闭SKU
	$(document).on("click", '.skuClose', function(e){ 
		sku_close(); 
	}); 
	// 单独选中操作
	$(".sku_ck").click(function(){
		sku_open();
		sku_null();
		$(".add_chose").show();
		$(".add_sure").hide();
	});
	
	// 规格参数显示
	ck_sku_value = '';
	$(".sku_name_key").each(function(){
		ck_sku_value += $(this).attr('data-value')+',';
	});
	ck_sku_value = ck_sku_value.substring(0,ck_sku_value.length-1)
	$(".sku_ck_value").html(ck_sku_value); 
	
	// 计算全部价格
	total_price();
	
	// 选择规格 
	$(document).on("click", '.sku dl dd a', function(e){ 
		$this = $(this); 
		if($this.hasClass('selected')){ 
			$this.removeClass('selected');
		}else{
			$this.addClass("selected").siblings().removeClass('selected');
		} 
		var sku_img = $(this).find('img').attr('data-img');
		if(sku_img != 'undefined'){ 
			$(".sku_show .img img").attr('src',sku_img);  
		} 
		
		var _val = '';
		$('.sku dl dd a.selected').each(function(){
			var val =  $(this).attr("data-id");  
			_val+=_val!=""?",":"";
			_val+=val; 
		}); 
		if(SKU_DATA[_val] == undefined ||SKU_DATA[_val] == ''){ 
			$('#sku_id').val('');
		}else{
			sku_pr = SKU_DATA[_val]['price'];  
			sku_act_pr = SKU_DATA[_val]['act_price'];  
			sku_grp_pr = SKU_DATA[_val]['grp_price'];  
			sku_sk = SKU_DATA[_val]['stock']; 
			sku_id = SKU_DATA[_val]['sku_id'];
			// IS DO
			if(sku_sk == '0'){
				$('.addcart').attr("disabled", "disabled").css({'background':'#f8f8f8','color':'#999'}); 
				$('.nowbuy').attr("disabled", "disabled").css({'background':'#f8f8f8','color':'#999'}); 
			}else{
				$('.addcart').attr("disabled",false).css({'background':'','color':'#fff'}); 
				$('.nowbuy').attr("disabled",false).css({'background':'','color':'#DE2323'}); 
			}
			$('.value_price').html(sku_pr); 			// 实际价格 
			$('.value_price').html(sku_act_pr);			// 活动价格 
			post_type = $("a.sure_button").attr('data-type');	// 只有2才为拼团价格 
			if(post_type == '2'){
				$('.value_price').html(sku_grp_pr);			// 拼团价格
			}
			//  其他页面的2才为拼团价格 
			post_type = $("a.sure_order").attr('data-type'); 
			if(post_type == '2'){
				$('.value_price').html(sku_grp_pr);
			}
			
			$('.stock_num span').html(sku_sk); 			// 库存数量
			$("#plus_button").attr('max-num',sku_sk); 	// 最大数量
			$('#sku_id').val(sku_id);  					// 选中的SKU值 
			
			// 选中值的处理
			ck_value = ''; 
			$(".sku dl dd a.selected").each(function(){ 
				ck_value +=$(this).find('em').html()+','; 
			});
			ck_value = ck_value.substring(0,ck_value.length-1);
			$('.sku_ck_value').html(ck_value);
		} 
		
	});
	
	// 点击购买
	$(document).on("click", '.buy_add,.sure_button', function(e){ 
		if($("a").hasClass('buy_add')){
			sku_id = $('#sku_id').val();   
			buy_num = $("#buy_num").val(); 
			var type = $(this).attr('data-type');
			if(type == '1' || type == '2'){
				if(type == '2'){
					post_method = 'group';	  
				}else{
					post_method = 'direct'; 
				} 
				var share_id = $('#share_id').val(); 
				if(sku_id == '' || sku_id == 'undefind'){ 
					if($(".buyContainerBg").is(':hidden')){ 
						sku_open();
						$(".sure_button").attr('data-type',type);
					}else{ 
						$.alert('请选择规格'); 	
					} 
				}else{ 
					$.ajax({
						type:'post',
						url :'index.php?m=ajax&a=check_user_status', 
						async:false,
						success:function(result){ 
							data = jQuery.parseJSON(result); 
							sku_close();
							if(data.status == '1'){
								$.postPage(
									'wap.php?m=cart&a=settle', 
									{post_method:post_method, sku_id:sku_id, buy_num:buy_num, share_id:share_id}, 
									function(){ 
										sku_null();
									}
								);
							}else{	// 未登录
								location.href="wap.php?m=member&a=login&returnUrl="+encodeURIComponent(window.location.href);
							}
						} 
					}); 
				}
			} else if(type == '0'){
				// 加入购物车专有参数
				gid = $("#goods_id").val();
				shop_id = $("#shop_id").val();
				$.ajax({
					type:'post',
					data:{gid:gid, shop_id:shop_id, sku_id:sku_id, buy_num:buy_num},
					url:'index.php?m=cart&a=add',
					success:function(result){ 
						data = jQuery.parseJSON(result); 
						if(data.error == '0'){ 
							$.get('index.php?m=cart&a=num',function(result){
								data = jQuery.parseJSON(result); 
								if(data.error == '0'){
									$(".cart_num").html(data.num);
								} 
							});
							sku_close();
							sku_null();
							$.alert('成功加入购物车');
						}else if(data.error == '2'){ 
							if($(".buyContainerBg").is(':hidden')){ 
								sku_open();
								$(".sure_button").attr('data-type',type);
							}else{ 
								$.alert('请选择规格'); 	
							} 
						}else{
							$.alert('加入购物车失败');	
						}
					},error:function(){
						alert('系统繁忙');
					}
				});
			}else{
				alert('提交错误');	
			} 		
		 
		}else{
			e.preventDefault(); 
		} 
	}); 
	
	
});
 

// 调节数量 goods/detail 页面的
$(document).on('click',"#minus_button",function(){  
	var now_num = $("#buy_num").val();
	var num = parseInt(now_num)-parseInt('1');
	if(num <= 0){
		$.alert('最少为一个数量'); 
		return false;
	}
	$("#buy_num").val(num); 
})

//goods plus
$(document).on('click',"#plus_button",function(){ 
	max_value = $(this).attr('max-num'); 
	now_num = $("#buy_num").val();
	num = parseInt(now_num)+parseInt('1'); 
	max_num = parseInt(max_value); 
	if(max_value < num){ 
		$.alert('超出购买的数量');	return false;
	}
	$("#buy_num").val(num); 
})

// 购物车页面的 cart/index 
$(document).on('click',".cart_plus",function(){ 
	$this = $(this);
	var cartid = $this.attr('data-cartid');
	var now_num = $("#cart_num_"+cartid).val(); 
	var num = parseInt(now_num)+parseInt('1');
	if(num > '0'){ 
		$("#cart_num_"+cartid).val(num); 
		cart_num(cartid, num);
	} 
	total_price(); 
});

$(document).on('click',".cart_minus",function(){ 
	$this = $(this);
	var cartid = $this.attr('data-cartid');
	var now_num = $("#cart_num_"+cartid).val();
	var num = parseInt(now_num)-parseInt('1');
	if(num > '0'){
		$("#cart_num_"+cartid).val(num); 
		cart_num(cartid, num);
	} 
	total_price(); 
});

// 输入改变
$(document).on('blur','.num',function(){
	var $this = $(this);
	var total = $this.val();
	if(total >= 1){
		var num = total;
	}else{
		var num = 1;
	}
	$this.val(num);
	var cartid = $(this).attr('cartid');
	cart_num(cartid, num);
	total_price();							  
});  
scart_num();
function scart_num(){
	$.get('index.php?m=cart&a=num',function(result){ 
		data = jQuery.parseJSON(result); 
		if(data.error == '0'){
			$("#cart_num").html(num);
			$(".rmcart span").html(num);
		}
	});
}
function cart_num(cartid, num){
	$.post('index.php?m=cart&a=edit_num',{cartid:cartid, num:num}); 
}
// 购物车移除
$(document).on('click',".delCart",function(){
	var cartid =$(this).attr('data-cartid'); 
	$.post('index.php?m=cart&a=del',{cartid:cartid},function(result){
		data = jQuery.parseJSON(result);
		if(data.error == '0'){
			$(".cart_detail_"+cartid).remove();
			location.reload();
		}else{
			alert(data.msg);
		}
	});
	total_price(); 
}); 

/* 购物车点击选择 */
$(function(){
	$(".ckbox_cartid").change(function(){
		total_price();
	}); 			   
});
 
// 购物车 产品总价格
function total_price(){ 
	var total_price = 0;
	var total_num = 0;
	var chk_num = 0;
	$(".cgone").each(function(){
		$this = $(this);
		num = $(".num input", this).val();
		now_price = $(".price span i", this).html(); 
		// 计算当前产品的价格
		//goods_price = parseInt(num) * parseFloat(now_price);
		// $(".cart_total_price span",$(this)).html(goods_price.toFixed(2));  
		if($("input[name='cartid[]']",$this).is(':checked')){
			total_price += parseInt(num) * parseFloat(now_price); 
			total_num += parseInt(num);
			chk_num += 1;
		} 
	});
	if(chk_num <=0){
		$(".gopay").attr('disabled',true);
	}else{
		$(".gopay").attr('disabled',false);
	}
	$("#total_price i").html(total_price.toFixed(2));		// (YES)  html
	$("#input_total_price").val(total_price.toFixed(2)); 	// (YES)  hide 
}

/////////////////////////////////////////////////////////////////////////////////////
// 收藏
$(function(){
	// 移入收藏
	$("a.rmCollect").click(function(){
		var gid = $(this).attr('data-gid'); 
		$.post("index.php?m=ajax&a=collect_goods",{gid:gid},function(result){
			data = jQuery.parseJSON(result); 
			if(data.error == '0'){
				$.alert('关注成功');
			}else if(data == '2'){
				sys_login(); 
			}else{
				$.alert(data);	
			}
		}); 								
	});
	$(".collect_goods").click(function(){
		var gid = $("#goods_id").val();
		$.ajaxSetup({ async: false }); 
		$.post("index.php?m=goods&a=collect_goods",{gid:gid},function(data){ 
			if(data == '1'){
				$.alert('收藏成功');
			}else if(data == '2'){
				sys_login(); 
			}else{
				$.alert(data);	
			}
		}); 
	});
	$(".collect_shop").click(function(){
		var spid = $("#shop_id").val();
		$.ajaxSetup({ async: false }); 
		$.post("index.php?m=goods&a=collect_shop",{spid:spid},function(data){ 
			if(data == '1'){
				$.alert('收藏成功');
			}else if(data == '2'){
				sys_login(); 
			}else{
				$.alert(data);	
			} 
		}); 
	});
});
 
 

 		 
 