$(document).ready(function() {
	$("#goods").change(function (){
		var goodssn=$(this).val();
		sgoods(goodssn);
	});
});
function search(){
	var chu_id = $("#chu_id").val();//出库单位
	var chu_type = $("#chu_type").val();//出库单位类型
	var keyword=$('#keyword').val().trim();//商品输入框
	if (keyword=='' || keyword==0 || keyword=='undefined'){
      $.showErr('请填写商品编号或商品名称',0,'hc');
      return false;  
    }
    if (chu_id=='' || chu_id==0 ){
      $.showErr('请先选择出库单位',0,'hc');
      return false;  
    }
	if (chu_type=='' || chu_type==0 ){
      $.showErr('请先选择出库单位类型',0,'hc');
      return false;  
    }
	
	var query= new Object();
	query.keyword = keyword;  //商品关键词
	query.chu_type = chu_type;  //供货商/仓库
	query.chu_id = chu_id;   //出库单位id
	
	$.ajax({
		type: "post",
		url: APP+"Shop/PurOrder/search_dinghuo_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');
			}
			return false;
		}
	});
}
function sgoods(goodssn){
	if (goodssn=='' || goodssn==0){
		$.showErr('请选择商品',2,'hc');
		return false;  
	}
	var chu_id = $("#chu_id").val();//出库单位
	var chu_type = $("#chu_type").val();//出库单位类型
	var goods_attr_sn=$('#goods').val();//商品属性编号
	var attr_sn = $("input[name = 'infos[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;//商品属性编号
	query.chu_type = chu_type;  //供货商/仓库
	query.chu_id = chu_id;   //出库单位id
	$.ajax({
		url: "Shop/PurOrder/dinghuo_table.html",
		data:query,
		type:"post",
		dataType:"json",
		success: function(data){
			if(data.status == 1){
				$('#tab_d').append(data.tr);
			}else{
				$.showErr(data.msg,2,'hc');
			}
		}
	});  
}
// 输入入库数量change事件
function change_loss(obj){
	var num = parseInt($(obj).val());//盘点数量
	if (isNaN(num) || num < 0) {
		$(obj).val(stock);
		num = stock;
	};
	var max = $(obj).attr('max');
	if(num > max){
		$.showErr('库存不足',2,'hc');
		$(obj).val(max);
		num = max;
	}
	var min = $(obj).attr('min');
	if(num < min){
		$.showErr('订货数量必须不小于起订量',2,'hc');
		$(obj).val(min);
		num = min;
	}
	var priceobj = $(obj).parent().next().find("input");
	var price = parseFloat(priceobj.val());//本店价
	var sum_price = num*price;
	$(obj).parent().next().next().html(sum_price);
}
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);
		return false;
	}
}
function chu_id(){
	var chu_id = $('#select_chu_id option:selected').val();
	$chu_id = $('#chu_id').val();
	if($chu_id){
		$.showErr('只能选一个供货商',3,'hc');
		$('#select_chu_id').attr("disabled",true);
	}else{
		$('#chu_id').val(chu_id);
	}
	
}
function add_dinghuo(t,is){
	var chu_id = $('#chu_id').val();
	if(chu_id == 0 || chu_id ==''){
		$.showErr('请选择供货商',0,'hc');
		return false;
	}
	if(is == 1){
		var is_print = $('#is_print').val(1);
	}
	var goods_id = $("input[name = 'infos[goods_id][]']");
	if(goods_id.length == 0){
		$.showErr('请选择商品',0,'hc');
		return false;
	}
	$(".btn3").attr('disabled',true);

	if(t==4){//生成配货单
		var r=confirm("是否确认生成配货单？");
	}
  	if (r==true){
		var form = $("form[name='myform']");
		var query = $(form).serialize();
		var url = APP+"Shop/PurOrder/sub_dinghuo";
		$.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;
  	}
}
function zyd_ru(is,id){
	$("#ru_tj").attr('disabled',true);
	if(id == '' || id == 0){
		$.showErr('参数错误！',0,'hc');
		return false;
	}
	if(is == 1){//入库提示
		var r=confirm("是否确认收货并入库？");
	}
	if (r==true){
		var query = new Object();
		query.id = id
		var url = "Shop/PurOrder/z_ru";
		$.ajax({
			url:url,
			data:query,
			type:"post",
			success:function(data){
				if(data.status == 1){
					$.showSuccess(data.msg,3,'hc');
					location.reload();
					// window.open(data.url); 
				}else{
					$.showErr(data.msg,3,'hc');
				}
				return false;
			}
		});
	}else{
		$("#ru_tj").attr('disabled',false);
   		return false;
  	}
}
function noship_ru(is,id){
	$("#noship_ru").attr('disabled',true);
	if(id == '' || id == 0){
		$.showErr('参数错误！',0,'hc');
		return false;
	}
	if(is == 1){//入库提示
		var r=confirm("是否确认入库？");
	}
	if (r==true){
		var query = new Object();
		query.id = id
		var url = "Shop/PurOrder/z_noship_ru";
		$.ajax({
			url:url,
			data:query,
			type:"post",
			success:function(data){
				if(data.status == 1){
					$.showSuccess(data.msg,3,'hc');
					location.reload();
					// window.open(data.url); 
				}else{
					$.showErr(data.msg,3,'hc');
				}
				return false;
			}
		});
	}else{
		$("#noship_ru").attr('disabled',false);
   		return false;
  	}
}