$(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');
            console.log('locationCode:')
            setFocus("barcode")
        }
    });
 
    //提交商品条码
    $('#barcode').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#barcode").val(trimStr($("#barcode").val()));
    	if($("#barcode").val() !='' ){
    		//播放扫描声音
        	playSound('../raw/scan.wav');
        	console.log('barcode:')
    		submitBarcode();
    	}
    });
});

 

//提交商品条码
var inventoryMap = {};
var outShelfInventoryId = -1;
function submitBarcode() {
	outShelfInventoryId = -1;
	
	var locationCode = $("#locationCode").val();
	var barcode = $("#barcode").val();

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

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

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

	$.ajax({
		type: 'POST',
		url: $appRequestUrl,
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(appRequest),
		success: function(appResponse) {
			if(!appResponse.success || appResponse.extend.length==0) {
				playSound('../raw/error.wav');
				mui.toast("该库位和商品条码未查到库存");
				return;
			}
			$("#barcodeShow").val(barcode);
            
            console.log('respnse:'+appResponse)

			var listInventory = appResponse.extend;

			if(listInventory.length > 1) { //出现多条记录时选择
				var listLi = [];
				for(var i = 0; i < listInventory.length; i++) {
					var inventorySingle = listInventory[i];
					inventoryMap[inventorySingle.id] = inventorySingle;
					listLi.push('<li class="mui-table-view-cell" onclick="selectInventory(\'' + inventorySingle.id + '\')">批次:' + inventorySingle.batchNo + ';	商品品质:' + inventorySingle.qualityName + ';	溯源:' + inventorySingle.inOrderNo1 + '; 可用数量:' + inventorySingle.qtyAvail + '  </li>');
				}
				var listLiStr = listLi.join('')
				$("#inventoryListDetail").html(listLiStr);
				mui("#inventoryListShow").popover('show'); //show hide toggle
				return
			}

			var inventorySingle = listInventory[0];
			$("#inOrderNo").val(inventorySingle.inOrderNo1);
			$("#batchNo").val(inventorySingle.batchNo)
			$("#qualityName").val(inventorySingle.qualityName)
			$("#sysQty").val(inventorySingle.qtyAvail)
			$("#qtyTotal").val(inventorySingle.qtyTotal)
			$("#expiryDate").val(fmtDate(inventorySingle.expiryDate))

			outShelfInventoryId = inventorySingle.id;

			console.log("outShelfInventoryId:" + outShelfInventoryId)

		},
		error: function() {
			playSound('../raw/error.wav');
			mui.toast('网络断开或服务器发生异常');
		}
	});
}
//选择一个库存
function selectInventory(inventoryId){
	outShelfInventoryId = inventoryId;
	 
	var inventorySingle=inventoryMap[outShelfInventoryId]
	$("#inOrderNo").val(inventorySingle.inOrderNo1);
	$("#batchNo").val(inventorySingle.batchNo)
	$("#qualityName").val(inventorySingle.qualityName)
	$("#sysQty").val(inventorySingle.qtyAvail)
	$("#qtyTotal").val(inventorySingle.qtyTotal)
	$("#expiryDate").val(fmtDate(inventorySingle.expiryDate))
    
    mui('#inventoryListShow').popover('toggle');
    setFocus('qty');
}




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