$(function(){
	
	var focusId;
	$("input").focus(function(){
		//记住焦点位置
		focusId = this.id;
		//this.scrollIntoView(true);//回到顶部
		//window.setTimeout("myScrollIntoView()",100);  
	});	
	
	setTimeout(function() {
		$('#orderNo').focus();
	}, 1000);

	//同步手工改数量
	var last_qty = $("#_quantity").val();
	function setQuantity(){
		var _qty = $("#_quantity").val();
		if(_qty < 0){
			$("#_quantity").val(0);
			return;
		}
		if(_qty == last_qty){
			return;		
		}
		last_qty = _qty;
		$("#quantity").val(_qty);
	}
	window.setInterval(setQuantity,500);
		
	//当失去焦点按回车时,焦点回到之前焦点位置
	$(window).keydown(function(event){
	 	if((event.keyCode==13)) {
	 		if(document.activeElement.id == null || document.activeElement.id ==''){
	 			//回到上次焦点地方
		 		//$("#"+focusId).focus();
	 		}
	 	}  
	});	

    $('#orderNo').keyup(function (event) {
        if (event.keyCode != "13") {return false;}
        $("#orderNo").val(trimStr($("#orderNo").val()));
        if($('#orderNo').val()!=''){
        	//播放扫描声音
        	playSound('../raw/scan.wav');
        	//player('../raw/scan.wav');
        	submitOrderNo();
        }
    });
	
    $('#barcode').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#barcode").val(trimStr($("#barcode").val()));
    	if($("#barcode").val() !='' ){
    		//播放扫描声音
        	playSound('../raw/scan.wav');
        	
    		submitBarcode();
    	}
    });
    
    $('#locationCode').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#locationCode").val(trimStr($("#locationCode").val()));
    	if($("#locationCode").val() !='' ){
    		//播放扫描声音
        	playSound('../raw/scan.wav');
        	
    		setFocus('quantity');
    	}
    });
    
    $('#quantity').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	if($("#quantity").val() !='' ){
    		//提交上架
    		submitInShelf();
    	}
    });
});

//提交入库单号
function submitOrderNo(){
   var orderNo = $('#orderNo').val();
   
   var appRequest = $appRequest;//复制请求封装对象
   appRequest.opType = OP_TYPE_MENU.IN_SHELF_1;
   appRequest.msg = orderNo;//msg是消息内容主体
   
   $.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('orderNo');  }); 
    			return;
    		}
        	
        	mui.toast(appResponse.msg);
        	
        	var order = appResponse.extend;
        	
        	$("#inOrderId").val(order.id);
        	$("#inOrderType").val(order.typeCode);
        	
        	$("#warehouseCode").val(order.warehouseCode);
        	$("#customerCode").val(order.customerCode);
        	$("#customerId").val(order.customerId);
        	
        	
        	next();
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常');  
        }
    });
}


//提交商品条码
function submitBarcode(){
	var inOrderId = $('#inOrderId').val();
	
	var orderNo = $('#orderNo').val();
	var barcode = $('#barcode').val();
	var formData = {inOrderId:inOrderId,orderNo:orderNo,barcode:barcode};
	
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.IN_SHELF_2;
   	appRequest.msg = JSON.stringify(formData);//msg是消息内容主体
   	
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('barcode');  }); 
    			return;
    		}
        	
        	mui.toast(appResponse.msg);
        	//返回收货记录 可能是多条
        	var list = appResponse.extend;
        	if(list.length == 0 ){
        		playSound('../raw/error.wav');
        		mui.alert('后台返回list长度为0', '警告', function() {  setFocus('barcode');  }); 
        		return;
        	}
        	//暂先只处理第一条.. 后续可改为界面选择一条
        	var inReceipt = list[0];
        	$("#inReceiptId").val(inReceipt.id);//收货记录id
        	$("#batchNo").val(inReceipt.batchNo);//批次号码
        	$("#receiptQuantity").val(inReceipt.quantity);//收货数量
        	$('#receiptQualityCode').val(inReceipt.qualityName);//选中收货品质
        	$('#shelfQualityCode').val(inReceipt.qualityCode);
        	
        	//获取推荐库位
        	getRecommendLocationCode();
        	
        	//跳到库位
        	$('#locationCode').focus();
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常'); 
        }
    });
}

//获取推荐库位
function getRecommendLocationCode(){
	var recommendRuleCode = $("#recommendRuleCode").val();
	var warehouseCode = $("#warehouseCode").val();
	var barcode = $("#barcode").val();
	var qualityCode = $("#shelfQualityCode").val();
	var customerId = $("#customerId").val();
	var inOrderId = $("#inOrderId").val();
	var inOrderType = $("#inOrderType").val();
	
	
	var _inReceipt ={};//只取部分参数
	_inReceipt.recommendRuleCode = recommendRuleCode; 
	_inReceipt.warehouseCode = warehouseCode; 
	_inReceipt.barcode = barcode; 
	_inReceipt.qualityCode = qualityCode; 
	_inReceipt.customerId = customerId;
	_inReceipt.inOrderId = inOrderId;
	_inReceipt.inOrderType = inOrderType;
	
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.GetRecommendLocationCode;
   	appRequest.msg = JSON.stringify(_inReceipt);//msg是消息内容主体
	
	var locationCode = "";
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        async:false,
        success: function (appResponse) {
			if(!appResponse.success){
    			return;
    		}
        	locationCode = appResponse.msg; 
        	$("#recommendLocationCode").val(locationCode);//推荐库位
        }
    });
	return locationCode;
}

//提交上架
var isSubmintIng = false;//是否正在提交. 防止按回车太快
function submitInShelf(){
	if(isSubmintIng){return;}
	isSubmintIng = true;
	
	var inOrderId = $("#inOrderId").val()+'';
	var orderNo = $("#orderNo").val();
	
	var barcode = $("#barcode").val();
	var inReceiptId = $("#inReceiptId").val();
	
	var qualityCode = $("#shelfQualityCode").val();//上架品质 -- 已控制跟收货品质相同
	var quantity = $("#quantity").val();
	
	var locationCode = $("#locationCode").val();//实际上架库位
	
	var customerCode = $("#customerCode").val();
	var warehouseCode = $("#warehouseCode").val();
	
	if(inReceiptId == ''){
        mui.toast('请先提交商品条码'); 
		isSubmintIng = false;
		return;
	}
	
	if(inOrderId == '' || locationCode == '' || barcode == ''){
		isSubmintIng = false;
		return;
	}
	
	var formData = {
		inOrderId:inOrderId,
		orderNo:orderNo,
		barcode:barcode,
		inReceiptId:inReceiptId,
		locationCode:locationCode,
		qualityCode:qualityCode,
		quantity:quantity
	};
	
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.IN_SHELF_3;
   	appRequest.msg = JSON.stringify(formData);//msg是消息内容主体
	
	var nwaiting = plus.nativeUI.showWaiting('正在提交中...');
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	isSubmintIng = false;
        	nwaiting.close(); 
			
        	if(!appResponse.success){
        		playSound('../raw/error.wav');
        		mui.alert(appResponse.msg, '警告', function() {  setFocus('locationCode');  }); 
    			return;
    		}
        	
        	mui.toast(appResponse.msg);
        	
        	//播放成功声音
        	playSound('../raw/ok.wav');
        	
        	//下一个条码
        	next();
        	
    		//整单完成. 下一个入库单
        	if(appResponse.extend){cleanAll();}
        },
        error: function () {
        	isSubmintIng = false;
        	nwaiting.close(); 
        	
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常'); 
        }
	});
}

//下一个条码
function next(){
	$("#barcode").val("");
	$("#inReceiptId").val("");
	
	$("#batchNo").val("");//批次号码
	$("#receiptQuantity").val("");//收货数量
	
	$("#quantity").val("");
	$("#_quantity").val("0");
	
	$("#recommendLocationCode").val("");
	$("#locationCode").val("");
	
    $("#receiptQualityCode").val('');
    $("#shelfQualityCode").val("");
    
	$("#barcode").focus();
}

//下一个订单
function cleanAll(){
	$("#inOrderId").val("");
	$("#inOrderType").val("");
	
	$("#orderNo").val("");
	
	$("#warehouseCode").val("");
	$("#customerCode").val("");
	
	$("#barcode").val("");
	$("#inReceiptId").val("");
	
	$("#batchNo").val("");//批次号码
	$("#receiptQuantity").val("");//收货数量
	
	$("#quantity").val("");
	$("#_quantity").val("0");
	$("#recommendLocationCode").val("");
	$("#locationCode").val("");

    $("#receiptQualityCode").val('');
    $("#shelfQualityCode").val("");
    
	$("#orderNo").focus();
}

