 $(function(){
	var focusId;
	$("input").focus(function(){
		//记住焦点位置
		focusId = this.id;
		//this.scrollIntoView(true);//回到顶部
		//window.setTimeout("myScrollIntoView()",100);  
	});	
	
	setTimeout(function() {
		genPacketNo();
	}, 1000);
	
	
	//当失去焦点按回车时,焦点回到之前焦点位置
	$(window).keydown(function(event){
	 	if((event.keyCode==13)) {
	 		$(".messager-body").window('close');
	 		if(document.activeElement.id == null || document.activeElement.id ==''){
	 			//回到上次焦点地方
		 		//$("#"+focusId).focus();
	 		}
	 	}  
	});	
	
	//提交托盘号码
    $('#packetNo').keyup(function (event) {
        if (event.keyCode != "13") {return false;}
        $("#packetNo").val(trimStr($("#packetNo").val()));
        //播放扫描声音
    	playSound('../raw/scan.wav');
        submitPacketNo();
    });
	
	    
    //订单号
    $('#orderNo').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#orderNo").val(trimStr($("#orderNo").val()));
    	//播放扫描声音
    	playSound('../raw/scan.wav');
    	submitOrderNo();
    }); 
    
    //提交箱号
    $('#boxNo').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#boxNo").val(trimStr($("#boxNo").val()));
    	//播放扫描声音
    	playSound('../raw/scan.wav');
    	submitBoxNo();
    });
	 
});

var isSubmitIng = false;//是否正在提交

function submitPacketNo(){
	var packetNo = $('#packetNo').val();
   	if(packetNo == null || packetNo == ''){
   		return;
   	}
	$("#orderNo").focus();
}

var totalBoxQty = 0;//每个订单的箱子个数
function submitOrderNo(){
	if(isSubmitIng){return;}
	isSubmitIng = true;
	
	var orderNo = $('#orderNo').val();
   	if(orderNo == ''){return;}
		
	var packetNo = $('#packetNo').val();
	var orderNo = $('#orderNo').val();
	
	if(packetNo=='' || packetNo==null){
	   mui.toast('托盘号不能为空');  
	   isSubmitIng = false;
	   return;
	}
	if(orderNo=='' || orderNo==null){
	   mui.toast('订单号不能为空');
	   isSubmitIng = false;
	   return;
	}
	var formData = {packetNo:packetNo,orderNo:orderNo};
	
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.BIND_PACKET_SUBMIT_ORDER_NO;
   	appRequest.msg = JSON.stringify(formData);//msg是消息内容主体
   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	isSubmitIng = false;
        	
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('orderNo');  }); 
    			return;
    		}
        	
			//List<OutBoxed> list = 
			var boxlist = appResponse.extend;
		
	      	nextBox();
			
			var boxQty = 0;
			var boxedQty = 0;
			for(var i = 0; i< boxlist.length; i++){
    	    	var box = boxlist[i];
    	    	handleNullFiled(box);
    	    	if(box.packetNo == null || box.packetNo == ''){
    	    		boxQty ++;
    	    	} else {
    	    		boxedQty ++;
    	    	}
         	}
			
			mui.toast('该订单有'+boxQty+'个箱号待绑定托盘,'+boxedQty+'个箱号已绑定托盘'); 
			
			totalBoxQty = boxQty;
			
			if(boxlist.length == 1){
         		var box = boxlist[0];
         		$("#boxNo").val(box.boxNo);
             	submitBoxNo();         		
         	}
    	},
    	error: function () {
    		isSubmitIng = false;
      		mui.toast('网络断开或服务器发生异常');  
	    }
  	});	
}

//提交装箱
function submitBoxNo(){
	if(isSubmitIng){return;}
	isSubmitIng = true;
	
	var packetNo = $('#packetNo').val();
	
	var orderNo = $('#orderNo').val();
	var boxNo = $('#boxNo').val();
	
	var addOrSub =$('input:radio[name="addOrSub"]:checked').val();//1表示加,2表示减
	
	
	if(packetNo=='' || packetNo==null){
	   mui.toast('托盘号不能为空');  
	   isSubmitIng = false;
	   return;
	}
	if(orderNo=='' || orderNo==null){
	   mui.toast('订单号不能为空');
	   isSubmitIng = false;
	   return;
	}
	if(boxNo=='' || boxNo==null){
	   mui.toast('箱号不能为空');  
	   isSubmitIng = false;
	   return;
	}
	
 	//绑定
	if(addOrSub == 1){
		binding();
		return;
	} else if(addOrSub == 2){
		unBinding();
		return;
	} else {
		isSubmitIng = false;	
	}
	
}

function binding(){
	var packetNo = $('#packetNo').val();
	var orderNo = $('#orderNo').val();
	var boxNo = $('#boxNo').val();
	var formData = {packetNo:packetNo,orderNo:orderNo,boxNo:boxNo};
	
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.BIND_PACKET;
   	appRequest.msg = JSON.stringify(formData);//msg是消息内容主体
   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	isSubmitIng = false;
        	
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('boxNo');  }); 
    			return;
    		}
        	
        	//播放成功声音
        	playSound('../raw/ok.wav'); 
        	
        	totalBoxQty = totalBoxQty - 1;
			
			if(totalBoxQty <= 0){
		      	//完成所有箱子则下一个订单
		      	nextOrder();
		      	return;
			}
	      	nextBox();
    	},
    	error: function () {
    		isSubmitIng = false;
      		mui.toast('网络断开或服务器发生异常');  
	    }
  	});	
}

function unBinding(){
	var packetNo = $('#packetNo').val();
	var orderNo = $('#orderNo').val();
	var boxNo = $('#boxNo').val();
	var formData = {packetNo:packetNo,orderNo:orderNo,boxNo:boxNo};
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.UN_BIND_PACKET;
   	appRequest.msg = JSON.stringify(formData);//msg是消息内容主体
   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	isSubmitIng = false;
        	
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('boxNo');  }); 
    			return;
    		}
        	
        	//播放成功声音
        	playSound('../raw/ok.wav'); 
        	
        	totalBoxQty = totalBoxQty + 1;
	      	nextBox();
    	},
    	error: function () {
    		isSubmitIng = false;
      		mui.toast('网络断开或服务器发生异常');  
	    }
  	});		
}  
function cleanAll(){
	isSubmitIng = false;
	$("#packetNo").val("");
	$("#orderNo").val("");
	$("#boxNo").val("");
	
	$('#packetNo').focus();
}

//下一个箱子
function nextBox(){
	$("#boxNo").val('');
	$("#boxNo").focus();
}

function nextOrder(){
	$("#boxNo").val('');
	$("#orderNo").val('');
	$("#orderNo").focus();
}

function finishPacket(){
	cleanAll();
	genPacketNo();
}

function genPacketNo(){
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.GEN_PACKET_NO;
   	appRequest.msg = '';//msg是消息内容主体
	   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	if(!appResponse.success){
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('packetNo');  }); 
    			return;
    		}
        	var packetNo = appResponse.msg;
        	$("#packetNo").val(packetNo);
        	$('#packetNo').focus();
    	},
    	error: function () {
      		mui.toast('网络断开或服务器发生异常');  
	    }
  	});
	
}