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

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

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

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


var barcodeBoxListMap;
function submitWaveNo() {
	if($("#waveNo").val() == '') {
		mui.alert('波次号不能为空!', '注意');
		return;
	}
	var appRequest = $appRequest; //复制请求封装对象
	appRequest.opType = OP_TYPE_MENU.BATCH_OUT_SHELF_PICK_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;
			}
			mui.toast(result.msg);
			
			var waveExtend = result.extend;
			$("#warehouseCode").val(waveExtend.warehouseCode);
			$("#customerCode").val(waveExtend.customerCode);
			$("#statusName").val(waveExtend.statusName);
			
			barcodeBoxListMap = waveExtend.barcodeBoxListMap;
			
			$('#barcode').focus();
		},
		error: function() {
			mui.alert('网络断开或服务器发生异常', '注意');
		}
	});
}

function submitSortting() {
	var waveNo = $("#waveNo").val();
	var barcode = $('#barcode').val();
	
	if(waveNo == '') {
		mui.alert('波次号不能为空!', '注意');
		return;
	}
	if(barcode == '') {
		mui.alert('商品条码不能为空!', '注意');
		return;
	}
	
	var boxList = barcodeBoxListMap[barcode]; //取出条码对应的每个箱子列表
	if(!boxList) {
		playSound('../raw/error.wav');
		$("#barcode").val('');
		mui.alert('该条码不存在', '警告', function() {  setFocus('barcode');  }); 
		return
	}
	
	//mui.alert(JSON.stringify(boxList));
	
	//循环该条码的箱子列表
	for(var i = 0;i < boxList.length;i++){
		var box = boxList[i];//每个箱子
		var boxNo = box['boxNo'];//箱子号-下标
		var qty = box['qty'];//应分拣数量
		var alreadyQty = box['alreadyQty'];//已分拣数量
		
		if(qty > alreadyQty){
			//分拣给此箱号
			
			alreadyQty++;
			box['alreadyQty'] = alreadyQty;
			boxList[i] = box;//js的map 不像Java map可以引用

			playSoundByBoxNo(boxNo);//播放语言
			mui.toast('请放入第' + boxNo +'号箱');
			
			$("#index").val(boxNo);
			
			if(qty == alreadyQty && (i+1) == boxList.length){
				nextBarcode();//下一个条码		
				mui.toast('该条码已分拣完成');
			}
			$('#barcode').val("");
			$('#barcode').focus();
			break;
		}
	}
	barcodeBoxListMap[barcode] = boxList;
	checkFinish(barcodeBoxListMap,barcode);
}

function checkFinish(barcodeBoxListMap,barcode){
	var isFinished = true;
	
	var qtyTotal = 0;
	var alreadyQtyTotal = 0;
	
	for(var key in barcodeBoxListMap){
		var boxList = barcodeBoxListMap[key];
		
		for(var i = 0;i < boxList.length;i++){
			var box = boxList[i];//每个箱子
			var boxNo = box['boxNo'];//箱子号-下标
			var qty = box['qty'];//应分拣数量
			var alreadyQty = box['alreadyQty'];//已分拣数量
			if(qty > alreadyQty){
			 	isFinished = false;
			}
			if(key == barcode){
				qtyTotal+=qty;
				alreadyQtyTotal+=alreadyQty;				
			}
		}
		
	}
	
	$("#qtyTotal").val(qtyTotal);
	$("#alreadyQtyTotal").val(alreadyQtyTotal);
	
	if(isFinished){
		cleanAll();	
	}
}

function nextBarcode() {
	$(".allowClear").each(function() {
		$(this).val("")
	})
	$('#barcode').focus();
}

function cleanAll() {
	$("input").each(function() {
		$(this).val("")
	})
	setTimeout(function(){
		//播放成功声音
    	playSound('../raw/ok.wav');		
	},1000);	
	$('#waveNo').focus();
}

function playSoundByBoxNo(boxNo){
	if(boxNo < 10){
		playSound1_9(boxNo);
		return;
	}
	if(boxNo == 10){
		playSound10(boxNo);
		return;
	}
	var a = boxNo / 10;
	var b = boxNo % 10;
	if(a > 1){
		playSound1_9(a);
		playSound10(10);
		setTimeout(function(){
			playSound1_9(b);		
		},500);
		return;
	}
	
	playSound10(10);
	setTimeout(function(){
		playSound1_9(b);		
	},500);	
};

function playSound1_9(boxNo){
	if(boxNo == '1'){
		playSound('../raw/one.wav');	
	}
	if(boxNo == '2'){
		playSound('../raw/two.wav');	
	}
	if(boxNo == '3'){
		playSound('../raw/three.wav');	
	}
	if(boxNo == '4'){
		playSound('../raw/four.wav');	
	}
	if(boxNo == '5'){
		playSound('../raw/five.wav');	
	}
	if(boxNo == '6'){
		playSound('../raw/six.wav');	
	}
	if(boxNo == '7'){
		playSound('../raw/seven.wav');	
	}
	if(boxNo == '8'){
		playSound('../raw/eight.wav');	
	}
	if(boxNo == '9'){
		playSound('../raw/nine.wav');	
	}
}

function playSound10(boxNo){
	if(boxNo == '10'){
		playSound('../raw/ten.wav');	
	}
}

