$(document).ready(function() {
	$("#goods").change(function (){
		var goodssn=$(this).val();
		sgoods(goodssn);
	});
});
function search(){
	var query= new Object();
	query = $("form[name='search-form']").serialize();
	$.ajax({
		type: "post",
		url: APP+"Shop/PurOrder/search_goods.html",
		data: query,
		success: function(data){
			var html = "<option value='0'>请选择</option>";
			if(data.status==1){
				for(var i=0;i<data.info.length;i++){
					if(data.info[i]['attr_name']){
						html += "<option value='"+data.info[i]['attr_key']+"'>"+data.info[i]['goods_name']+"("+data.info[i]['goods_sn']+")"+data.info[i]['attr_name']+"</option>";
					}else{
						html += "<option value='"+data.info[i]['attr_key']+"'>"+data.info[i]['goods_name']+"("+data.info[i]['goods_sn']+")"+"</option>";
					}
				}
				$("#goods").html(html);
			}else{
				$("#goods").html(html);
				$.showErr(data.msg,3,'hc');
			}
		}
	});
}
function sgoods(goodssn){
	if (goodssn=='' || goodssn==0){
		$.showErr('请选择商品',2,'hc');
		return false;  
	}
	var goods_attr_sn=$('#goods').val();//商品属性编号
	var attr_sn = $("input[name = 'goods_type[goods_attr_sn][]']");
	var flag = 0;
	if(attr_sn.length > 0){
		attr_sn.each(function(){
			var id = $(this).val();
			if(goodssn == id){
				$.showErr('商品已存在！',2,'hc');
				flag = 1;
				return false; 
			}
		});
	}
	if (flag == 1) {
		return false;
	};
	var query = new Object();
	query.goods_attr_sn = goods_attr_sn;//商品属性编号
	$.ajax({
		url: "Shop/PurOrder/create_goods_table.html",
		data:query,
		type:"post",
		dataType:"json",
		success: function(data){
			if(data.status == 1){
				$('#tab_d').append(data.tr);
				sum_heji();//计算合计
			}else{
				$.showErr(data.msg,2,'hc');
			}
		}
	});  
}
// 盘点数量change事件
function change_loss(obj){
	var stock = parseInt($(obj).parent().prev().html());//仓库数量
	var num = parseInt($(obj).val());//盘点数量
	if (isNaN(num) || num < 0) {
		$(obj).val(stock);
		num = stock;
	};
	var loss = num - stock;
	var  profit_loss= $(obj).parent().next().find("input");
	profit_loss.val(loss);//盈利盈亏
	sum_heji();
}
//计算合计
function sum_heji(){
	var stock = $(".stock");//库存
	var stock_sum = 0;//库存总数
	var pan_sum = 0;//盘点总数
	var y_sum = 0;//盘盈总数
	var k_sum = 0;//盘亏总数

	stock.each(function(){
		 stock_sum += parseInt($(this).html());
		 pan_sum += parseInt($(this).next().children(':first').val());
		 var y_k = parseInt($(this).next().next().children(':first').val());
		 if (y_k > 0) {
			y_sum += y_k;
		 }else{
			k_sum += y_k;
		 }
	});
	$("#stock_sum").html(stock_sum);
	$("#pan_sum").html(pan_sum);
	$("#y_sum").html(y_sum);
	$("#k_sum").html(k_sum);
}
function delCurrentRow(obj){ 
	if(confirm("确定要删除这个商品吗?")){
		var tr=$(obj).parent().parent();
		var num=parseInt(tr.attr('index'));
		var type=$("#type").val();
		if(type == 0){

			var tpl='<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>&nbsp;</td>';   
		}else if(type == 1){
			var tpl='<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>&nbsp;</td>';
		}
		tr.remove();
		htmls='<tr>'+tpl+'</tr>';
		$('#tab_d').append(htmls);
		sum_heji();//计算合计
		return false;
	}
}
function add_stock(t,is){
	var goods_id = $("input[name = 'goods_type[goods_id][]']");
	var flag = 0;
	if(goods_id.length == 0){
		$.showErr('请选择商品',0,'hc');
		return false;
	}
	if(is == 1){
		var is_print = $('#is_print').val(1);
	}
	$(".btn3").attr('disabled',true);

	if(t==1){//生成盘点单
		var r=confirm("是否确认生成盘点单？");
	}
  	if (r==true){
		var form = $("form[name='myform']");
		var query = $(form).serialize();
		var url = APP+"Shop/PurOrder/create_stocktaking";
		$.ajax({
			url:url,
			data:query,
			type:"post",
			success:function(data){
				if(data.status == 1){
					if(is == 1){
						window.open(data.url); 
					}else{
						$.showSuccess(data.msg,3,'hc');
					}
				}else{
					$.showErr(data.msg,3,'hc');
				}
				return false;
			}
		});
	}else{
		$(".btn3").attr('disabled',false);
   		return false;
  	}
}