// 购物车的操作 
$(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'); 
		sku_null();
	}
	
	function sku_null(){ 
		data_sku = $("input#sku_id").attr('data-sku');
		if(data_sku == '1'){
			$("input#sku_id").val('');
		}
	}
	
	//　关闭SKU
	$(document).on("click", '.skuClose', function(e){ 
		sku_close(); 
	});
	
	// 规格参数显示
	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); 
 
	// 选择规格 
	$(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);			// 拼团价格
			} 
			$('.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')){
			var shop_id = $('#shop_id').val(); 
			var sku_id  = $('#sku_id').val();   
			var buy_num = $("#buy_num").val(); 
			var type = $(this).attr('data-type');
			if(type == '1'){
				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:URI+'ajax/check_login', 
						async:false,
						success:function(result){
							data = jQuery.parseJSON(result); 
							if(data.error == '0'){
								$.post(URI+'cart/create_settle', {post_method:'direct', sku_id:sku_id, buy_num:buy_num}, function(result){ 
									sku_close();
									data = jQuery.parseJSON(result);
									if(data.error == '0'){
										location.href=WAP+"cart/settle";
									}else{
										$.alert('创建交易失败');	
									}
								}); 
							}else if(data.error == '4'){	// 未登录
								location.href=WAP+"member/login?returnUrl="+encodeURIComponent(window.location.href);
							}else{
								$.alert(data.msg);	
							}
						} 
					}); 
				}
			}else if(type == '0'){			// 加入购物车专有参数 
				gid = $("#goods_id").val(); 
				$.ajax({
					type:'post',
					data:{gid:gid, sku_id:sku_id, shop_id:shop_id, buy_num:buy_num},
					url:URI+'cart/add',
					success:function(result){ 
						data = jQuery.parseJSON(result); 
						if(data.error == '0'){ 
							cart_total_num();
							sku_close(); 
							$.alert('成功加入购物车');
						}else if(data.error == '2'){ 
							if($(".buyContainerBg").is(':hidden')){ 
								sku_open();
								$(".sure_button").attr('data-type',type);
							}else{
								$.alert('请选择规格'); 	
							} 
						}else{
							$.alert(data.msg);	
						}
					},error:function(){
						alert('系统繁忙');
					}
				});
			}else{
				alert('提交错误');	
			} 		
		 
		}else{
			e.preventDefault(); 
		} 
	}); 
	
	
	
}); 

// 统计购物车数量
function cart_total_num(){ 
	$.get(URI+'cart/num',function(result){
		data = jQuery.parseJSON(result); 
		if(data.error == '0'){
			$(".cart_num").html(data.num);
		}
	});
}

// 改变数量 
$(document).on('click', "a.cart_plus", function(){ 
	$this = $(this); 
	stock = $this.attr('data-stock');
	cartid = $this.attr('data-cartid');
	now_num = $("#cart_num_"+cartid).val(); 
	num = parseInt(now_num)+parseInt('1');
	if(num < stock){
		limit_num = $this.attr('data-buy-limit');
		if(limit_num != 0){
			if(num > limit_num){ 
				$.alert('每次限购'+limit_num+'个'); return false;
			} 
		}
		$("#cart_num_"+cartid).val(num);
		update_num(cartid, num); 
		total_price(); 
	}else{
		$.alert('库存不足');	
	} 
});

$(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); 
		update_num(cartid, num);
	} 
	total_price(); 
});

// 输入改变
$(document).on('blur', 'input.cnum',function(){
	var $this = $(this);
	var total = $this.val();
	if(total >= 1){
		var num = total;
	}else{
		var num = 1;
	}
	$this.val(num);
	cartid = $this.attr('cartid');
	update_num(cartid, num);
	total_price();							  
});

// 删除购物车
$(document).on('click', ".delCart",function(){
	var cartid =$(this).attr('data-cartid'); 
	$.post(URI+'cart/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 update_num(cartid, num){
	$.post(URI+'cart/edit_num',{cartid:cartid, num:num}, function(result){ }); 
}

// 选中
$(document).on('change', ".ckbox_cartid", function(){ 
	total_price();
});

total_price();
// 购物车 产品总价格
function total_price(){ 
	var total_price = 0;
	var total_num = 0;
	var chk_num = 0; 
	$(".cgone").each(function(){
		$this = $(this);
		num = $this.find(".num input").val();
		now_price = $this.find(".price .pr i").html(); 
		if($this.find("input[name='cartid[]']").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(){ 
	// 全部选择
	$(document).on('click', '.checked_cartid_all', function(){
		if(this.checked){
			$(".checked_cartid:checkbox,.checked_cartid_shop").attr("checked", true);  
			$(".checked_cartid_all").attr('checked', true); 
		}else{    
			$(".checked_cartid:checkbox,.checked_cartid_shop").attr("checked", false); 
			$(".checked_cartid_all").attr('checked', false);  
		}
		total_price();
	});
	// 店铺选择
	$(document).on('click', '.checked_cartid_shop', function(){
		$this = $(this);
		if(this.checked){
			$this.parents(".spbox").find(".gsbox .checked_cartid:checkbox").attr("checked", true); 
		}else{    
			$this.parents(".spbox").find(".gsbox .checked_cartid:checkbox").attr("checked", false); 
		}
		cart_check_sp(); 
		total_price();
	});
	// 产品选择
	$(document).on('click', 'input.checked_cartid', function(){
		$this = $(this).parents(".spbox");
		var num = 0;
		var loop = 0;
		$this.find("input.checked_cartid").each(function(){
			if($(this).attr("checked")){
				num++;
			}
			loop++;
		}); 
		if(num == loop){ 
			$this.find(".checked_cartid_shop").attr("checked", true);
		}else{
			$this.find(".checked_cartid_shop").attr("checked", false);
		}
		cart_check_sp(); 
		total_price();
	}); 
	
	// 店铺选中情况 
	function cart_check_sp(){
		var spnum = 0;
		var sploop = 0; 
		$("input.checked_cartid_shop").each(function(){
			if($(this).attr("checked")){
				spnum++;
			}
			sploop++;
		}); 		
		if(spnum == sploop){ 
			$(".checked_cartid_all").attr("checked", true);
		}else{
			$(".checked_cartid_all").attr("checked", false);
		}
	}
	total_price();

});