
$(document).ready(function() {
    $("#prevTrIndex").val("-1"); //默认-1        
	resettr();
	clickTr(0);
	$("#goods").change(function (){
		var goodssn=$(this).val();
		sgoods(goodssn);
	});

	$("#supplier").change(function(event) {
		if ($('#supplier').val() != '' && $('#storage_id').val() != '') {
			var query = new Object();
			query.supplier_id = $('#supplier').val();//供货商id
			query.storage_id = $('#storage_id').val();//仓库id
		    $.ajax({
			    url: APP+"Storage/storage_goods",
				data:query,
				type:"post",
			    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,0,'hc');
			      	}
					return false;
			    }
		  	});
		}
		$('#c_dw').val($(this).val());
	});
})
	
$(document).keydown(function(event){
	if(event.keyCode==13){
		$(':focus').parent().next().find('button').triggerHandler('click');
		return false;
	}
}); 




function resettr(){
    $(".gridtable tr").each(function(i) { //初始化 id 和 index 属性
        $(this).attr("id", "tr_" + i).attr("index", i);
	}).click(function() { //鼠标单击
		clickTr($(this).attr("index"));
	});
}
function clickTr(currTrIndex) {
    var prevTrIndex = $("#prevTrIndex").val();
    if (currTrIndex > -1) {
        $("#tr_" + currTrIndex).addClass("over");
    }
	$("#tr_" + prevTrIndex).removeClass("over");
    $("#prevTrIndex").val(currTrIndex);
}
function storage_goods(){

	var storage_id = $("#storage_id").val();
	var supplier_id = $("#supplier").val();
	var goods_sn=$('#goods_sn').val();//商品名称或编号
    if (goods_sn==''){
      $.showErr('关键字不能为空',0,'hc');
      return false;  
    } 
  	var query = new Object();
	query.goods_sn = goods_sn;  //商品关键词
	query.storage_id = storage_id;     //仓库id
	query.supplier_id = supplier_id;   //供货商id
  	$.ajax({
	    url: APP+"Storage/storage_goods",
		data:query,
		type:"post",
		dataType:"json",
	    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,0,'hc');
	      	}
			return false;
	    }
  	}); 
}


function searru(){
	//供货商/仓库
	var storage_id = $("#storage_id").val();
    if (storage_id==''){
      return false;  
    }
    // alert(storage_id);
	var supplier_name = $("#supplier_name").val();
    // alert(supplier_name);

	var query = new Object();
	query.chu_id = storage_id;
	query.chu_name = supplier_name;
	if (supplier_name==''){
		$.showErr('关键字不能为空',0,'hc');
      return false;  
    }
    $.ajax({
	    url: APP+"Storage/searchu",
		data:query,
		type:"post",
	    success: function(data){
	      	var html = "<option value='0'>==请选择==</option>";
	      	if(data.status == 1){ 
              	for(var i=0;i<data.info.length;i++){
              		html += "<option value='"+data.info[i].id+"'>"+data.info[i].name+"</option>";
              	}
	            $("#supplier").html(html);
	      	}else{
	      		$("#supplier").html(html);
	      		$.showErr(data.msg,0,'hc');
	      	}
			return false;
	    }
  	}); 
}


// 盘点数量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 sgoods(goodssn){
	if (goodssn=='' || goodssn==0){
		$.showErr('请选择商品',0,'hc');
      	return false;  
    }
	var storage_id=$('#storage_id').val();//仓库id
	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('商品已存在！',0,'hc');
            	flag = 1;
      			return false; 
            }
        });
	}
	if (flag == 1) {
		return false;
	};
  	var query = new Object();
	query.storage_id = storage_id;//仓库id
	query.goods_attr_sn = goods_attr_sn;//商品属性编号
	
  	$.ajax({
	    url: APP+"Storage/create_goods_table",
		data:query,
		type:"post",
		dataType:"json",
	    success: function(data){
	      	if(data.status == 1){ 
		        var thisnum=parseInt($('#thisTrIndex').val());
		        var supplier_id=parseInt($('#supplier_id').val());
		        if(supplier_id){
		        	var n=0;
		        }else{
		        	var n=4;
		        }
		        if (thisnum > n) {
		           $('#tab_d').append(data.tr);
		        }else{
		           re=/^<(\/?)tr>/g;
		           data.tr=data.tr.replace(re,'');
		           $('#tr_'+thisnum).html(data.tr);
		        }      
		        thisnum=thisnum + 1;       
		        $('#thisTrIndex').val(thisnum);
		        clickTr(0);
		        resettr();
		        sum_heji();//计算合计
	      	}else{
	      		$.showErr(data.msg,0,'hc');
	      	}
	    }
  	});  
}
//计算合计
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 add_stock(t){
	var goods_id = $("input[name = 'goods_type[goods_id][]']");
	var flag = 0;
	if(goods_id.length == 0){
		$.showErr('请选择商品',0,'hc');
		return false;
	}
	$(".btn3").attr('disabled',true);

	if(t==1){//订货单
		var r=confirm("是否确认生成盘点单？");
	}
  	if (r==true){
		var form = $("form[name='info']");
		var query = $(form).serialize();
		var url = APP+"Storage/create_stocktaking";
		$.ajax({
			url:url,
			data:query,
			type:"post",
			success:function(data){
				if(data.status == 1){
					window.open(data.url); 
				}else{
					$.showErr(data.msg,0,'hc');
				}
				return false;
			}
		});
	}else{
		$(".btn3").attr('disabled',false);
   		return false;
  	}
}
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);  
      	var thisnum=parseInt($('#thisTrIndex').val());  
      	thisnum=thisnum - 1;    
      	$('#thisTrIndex').val(thisnum);
      	resettr();
      	sum_heji();//计算合计
      	return false;
    }
}