$(function(){
 
	var focusId;
 	$('#locationCode').focus();
	//如不使用延迟执行, 有时候会无效
	setTimeout(function() {
    	var $warehouseCode = localStorage.getItem('$warehouseCode');
		$("#warehouseCode").val($warehouseCode);
		
		$('#locationCode').focus();
	}, 1000);

    //提交库位
    $('#locationCode').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#locationCode").val(trimStr($("#locationCode").val()));
    	if($("#locationCode").val() !='' ){
    		//播放扫描声音
        	playSound('../raw/scan.wav');
        	$("#barcode").focus();
    	}
    });

    //提交商品条码
    $('#barcode').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#barcode").val(trimStr($("#barcode").val()));
    	if($("#barcode").val() !='' ){
    		//播放扫描声音
        	playSound('../raw/scan.wav');
    		submitBarcode();
    	}
    });
     
});

 
function submitBarcode() {
	var locationCode = $("#locationCode").val();
	var barcode = $("#barcode").val();

	var msgBody = {};
	msgBody.warehouseCode = $("#warehouseCode").val();
	msgBody.locationCode = locationCode;
	msgBody.barcode = barcode;

	//序列化成字符串
	var msgBodyStr = JSON.stringify(msgBody)

	var appRequest = $appRequest; //复制请求封装对象
	appRequest.opType = OP_TYPE_MENU.SKU_INVENTORY;
	appRequest.msg = msgBodyStr; //msg是消息内容主体

	console.info(appRequest);
	$("#itemList").empty();
	$.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.toast(appResponse.msg);
				return;
			}
			var listInventory = appResponse.extend;
			if(listInventory.length <=0 ){
				mui.toast('没有查到库存');
				return;
			}
			var customerCode = '';
			var sku = '';
			var totalQty = 0;
			for(var i = 0; i < listInventory.length; i++) {
				var inventorySingle = listInventory[i];
				customerCode = inventorySingle.customerCode;
				sku = inventorySingle.sku;
				totalQty += inventorySingle.qtyTotal;
				
    			var tr = "<tr id='item_"+inventorySingle.id+"'>";
	    			tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+inventorySingle.locationCode+"</div></th>";
	    			tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+inventorySingle.barcode+"</div></th>";
	    			tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+inventorySingle.qtyTotal+"</div></th>";	
    			tr += "</tr>";
    			$("#itemList").append(tr);
			}
			
			$("#customerCode").val(customerCode);
			$("#sku").val(sku);
			$("#totalQty").val(totalQty);
		},
		error: function() {
			playSound('../raw/error.wav');
			mui.toast('网络断开或服务器发生异常');
		}
	});
}


//下一个转移任务
function cleanAll() {
	$("#itemList").empty();
	
	$(".allowClear").each(function() {
		$(this).val("")
	})
	$("#locationCode").focus();
}