//波次单号回车
$('#waveNo').keyup(function(event) {
	if(event.keyCode != "13") {
		return false;
	}
	$("#waveNo").val(trimStr($("#waveNo").val()));
	
	//播放扫描声音
    playSound('../raw/scan.wav');
	submitWaveNo()
});

$("#waveNo").blur(function() {
	var waveNo = trimStr($("#waveNo").val());
	if(waveNo == '') {
		$('#waveNo').focus();
	}
});

setTimeout(function() {
	$('#waveNo').focus();
}, 1000);

// 库位号回车
// $('#locationCode').keyup(function(event) {
// 	if(event.keyCode != "13") {
// 		return false;
// 	}
// 	$("#locationCode").val(trimStr($("#locationCode").val()));

//     //播放扫描声音
//     playSound('../raw/scan.wav');
// 	$('#barcode').focus();
// });

// 条码回车
$('#barcode').keyup(function(event) {
	if(event.keyCode != "13") {
		return false;
	}
	$("#barcode").val(trimStr($("#barcode").val()));

    //播放扫描声音
    playSound('../raw/scan.wav');
	$("#productQty").focus();
});

// 提交
$('#productQty').keyup(function(event) {
	if(event.keyCode != "13") {
		return false;
	}
	$("#productQty").val(trimStr($("#productQty").val()));

    //播放扫描声音
    playSound('../raw/scan.wav');
	submitOutShelf();
});

function submitWaveNo() {
	if($("#waveNo").val() == '') {
		mui.alert('波次号或运单号不能为空!', '注意');
		return;
	}
	var appRequest = $appRequest; //复制请求封装对象
	appRequest.opType = OP_TYPE_MENU.BATCH_OUT_SHELF_1;
	appRequest.msg = $("#waveNo").val(); //msg是消息内容主体
	
	// 提交
	$.ajax({
		type: 'POST',
		url: $appRequestUrl,
		data: JSON.stringify(appRequest),
		contentType: "application/json",
		success: function(result) {
			if(!result.success) {
				playSound('../raw/error.wav');
				$("#waveNo").val("");
        		mui.alert(result.msg, '警告', function() {  setFocus('waveNo');  }); 
				return;
			}
			var wave = result.extend;
			
			//刷新待下架列表
			showUnOutShelfDetails();
			
			$("#warehouseCode").val(wave.warehouseCode);
			$("#customerCode").val(wave.customerCode);
			$("#statusName").val(wave.statusName);
			
			$('#barcode').focus();
		},
		error: function() {
			mui.alert('网络断开或服务器发生异常', '注意');
		}
	});
}

//提交下架
var isSubmintIng = false; // 是否正在提交
function submitOutShelf() {
	if(isSubmintIng) {
		return;
	}
	if($("#waveNo").val() == '') {
		mui.alert('波次号不能为空!', '注意');
		return;
	}
	// if($("#locationCode").val() == '') {
	// 	mui.alert('货位号不能为空!', '注意');
	// 	return;
	// }
	if($("#barcode").val() == '') {
		mui.alert('商品条码不能为空!', '注意');
		return;
	}
	if($("#productQty").val() == '') {
		mui.alert('商品数量不能为空!', '注意');
		return;
	}
	isSubmintIng = true;

	var waveNo = $("#waveNo").val();
	// var locationCode = $('#locationCode').val();
	var barcode = $('#barcode').val();
	var quantity = $("#productQty").val();

	var jsonData = {};
	jsonData.waveNo = waveNo;
	jsonData.quantity = quantity;
	// jsonData.locationCode = locationCode;
	jsonData.barcode = barcode;

	var appRequest = $appRequest; //复制请求封装对象
	appRequest.opType = OP_TYPE_MENU.BATCH_OUT_SHELF_2;
	appRequest.msg = JSON.stringify(jsonData); //msg是消息内容主体

	mui(document.getElementById("subButton")).button('loading'); //切换为loading状态
	// 提交
	$.ajax({
		type: 'POST',
		url: $appRequestUrl,
		data: JSON.stringify(appRequest),
		contentType: "application/json",
		success: function(result) {
			console.log(JSON.stringify(result))
			isSubmintIng = false;
			mui(document.getElementById("subButton")).button('reset'); //重置button

			if(!result.success) {
        		playSound('../raw/error.wav');
        		mui.alert(result.msg, '警告', function() {  setFocus('barcode');  }); 				
				return;
			}
			mui.toast(result.msg);
        	//播放成功声音
        	playSound('../raw/ok.wav');
        	
			//刷新下架列表
			refreshUnOutShelfItems();
			refreshAlreadyOutShelfItems();
			
        	nextBarcode(); // 下一个库位
        	
			if(result.extend.flag == '3') { // 波次完成下架
				cleanAll();
			}
		},
		error: function() {
			isSubmintIng = false;
			mui(document.getElementById("subButton")).button('reset'); //重置button

			mui.alert('网络断开或服务器发生异常', '注意');
		}
	});
}

function nextBarcode() {
	// 下架完之后清空
	$(".allowClear").each(function() {
		$(this).val("")
	})
	$('#barcode').focus();
}

function cleanAll() {
	$("input").each(function() {
		$(this).val("")
	})
	$('#waveNo').focus();
}


//下架列表
var isShowUnOutShelfDetails = false;
function showUnOutShelfDetails(){
	if(!isShowUnOutShelfDetails){
		$("#showUnOutShelfDetails").show();
		isShowUnOutShelfDetails = true;
		$("#showUnOutShelfIco").attr("class", "fa fa-angle-double-up");
		
		//刷新明细
		refreshUnOutShelfItems();
	}else{
		$("#showUnOutShelfDetails").hide();
		isShowUnOutShelfDetails = false;
		$("#showUnOutShelfIco").attr("class", "fa fa-angle-double-down");
	}
	//回到上次焦点地方
	// if(focusId!=null && focusId != ''){
	// 	$("#"+focusId).focus();		
	// }
}

//显示待下架明细
function refreshUnOutShelfItems(){
	var waveNo = $('#waveNo').val();
   
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.BATCH_OUT_SHELF_3;
   	appRequest.msg = waveNo;//msg是消息内容主体
	
	$("#unOutShelfItemList").empty();
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	if(!appResponse.success){
    			return;
    		}
			
        	var waitQtyTotal = 0;
    		var itemList = appResponse.extend;
			console.log(JSON.stringify(itemList));
			
			var unOutShelfList = itemList.unOutShelfList;
    		for(var index = 0; index < unOutShelfList.length; index ++){
				var unOutShelf = unOutShelfList[index];
				var tr = "<tr id='item_"+unOutShelf.id+"'>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+unOutShelf.barcode+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+unOutShelf.productName+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+unOutShelf.locationCode+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+unOutShelf.quantity+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+unOutShelf.extend4+"</div></th>";
					tr += "</tr>";
				
				$("#unOutShelfItemList").append(tr);
    		}
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常');  
        }
	});
}

//已下架列表
var isAlreadyOutShelfDetails = false;
function alreadyOutShelfDetails(){
	if(!isAlreadyOutShelfDetails){
		$("#alreadyOutShelfDetails").show();
		isAlreadyOutShelfDetails = true;
		$("#showAlreadyOutShelfIco").attr("class", "fa fa-angle-double-up");
		
		//刷新明细
		refreshAlreadyOutShelfItems();
	}else{
		$("#alreadyOutShelfDetails").hide();
		isAlreadyOutShelfDetails = false;
		$("#showAlreadyOutShelfIco").attr("class", "fa fa-angle-double-down");
	}
	//回到上次焦点地方
	/* if(focusId!=null && focusId != ''){
		$("#"+focusId).focus();		
	} */
}

//显示已下架明细
function refreshAlreadyOutShelfItems(){
	var waveNo = $('#waveNo').val();
   
   	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.BATCH_OUT_SHELF_3;
   	appRequest.msg = waveNo;//msg是消息内容主体
	
	$("#alreadyOutShelfItemList").empty();
	$.ajax({
        type: 'POST',
        url: $appRequestUrl,
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(appRequest),
        success: function (appResponse) {
        	if(!appResponse.success){
    			return;
    		}
			
        	var waitQtyTotal = 0;
    		var itemList = appResponse.extend;
			console.log(JSON.stringify(itemList));
			
			var alreadyOutShelfList = itemList.alreadyOutShelfList;
    		for(var index = 0; index < alreadyOutShelfList.length; index ++){
				var alreadyOutShelf = alreadyOutShelfList[index];
				var tr = "<tr id='item_"+alreadyOutShelf.id+"'>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+alreadyOutShelf.barcode+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+alreadyOutShelf.productName+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+alreadyOutShelf.locationCode+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+alreadyOutShelf.quantity+"</div></th>";
					tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+alreadyOutShelf.extend4+"</div></th>";
					tr += "</tr>";
				
				$("#alreadyOutShelfItemList").append(tr);
    		}
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常');  
        }
	});
}