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

	//当失去焦点按回车时,焦点回到之前焦点位置
	$(window).keydown(function(event){
	 	if((event.keyCode==13)) {
	 		if(document.activeElement.id == null || document.activeElement.id ==''){
	 			//回到上次焦点地方
		 		//$("#"+focusId).focus();
	 		}
	 	}  
	});	

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

    $('#locationCode').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#locationCode").val(trimStr($("#locationCode").val()));
    	if($("#locationCode").val() !='' ){
    		//播放扫描声音
        	playSound('../raw/scan.wav');
				        	
        	submitLocationCode();
    	}
    });
    
    $('#barcode').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#barcode").val(trimStr($("#barcode").val()));
    	if($("#barcode").val() !='' ){
    		//播放扫描声音
        	playSound('../raw/scan.wav');
        	
    		submitBarcode();
    	}
    });
	
    $('#batchNo').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	$("#batchNo").val(trimStr($("#batchNo").val()));
    	if($("#batchNo").val() !='' ){
    		var a = $("#batchNo").val();
    		var b = a.toUpperCase();
			$("#batchNo").val(b);
			
    		//播放扫描声音
        	playSound('../raw/scan.wav');
        	
    		submitBatchNo();
    	}
    });	
	
    $('#checkQty').keyup(function (event) {
    	if (event.keyCode != "13") {return false;}
    	if($("#checkQty").val() !='' ){
    		//提交上架
    		submitInventoryCheck();
    	}
    });
});

//提交入库单号
function submitCheckCode(){
   var inventoryCheckCode = $('#inventoryCheckCode').val();
   
   var appRequest = $appRequest;//复制请求封装对象
   appRequest.opType = OP_TYPE_MENU.INVENTORY_PLAN_CHECK_1;
   appRequest.msg = inventoryCheckCode;//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('inventoryCheckCode');  }); 
    			return;
    		}
        	
        	mui.toast(appResponse.msg);
        	
        	var check = appResponse.extend;
        	
        	$("#inventoryCheckId").val(check.id);
        	$("#warehouseCode").val(check.warehouseCode);
        	$("#customerCode").val(check.customerCode);
        	$("#customerId").val(check.customerId);
        	
        	nextLocation();
        	
        	getNextLocationCode();
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常');  
        }
    });
}

function submitLocationCode(){
	var inventoryCheckId = $("#inventoryCheckId").val();
	var warehouseCode = $("#warehouseCode").val();
	var customerCode = $("#customerCode").val();
	var customerId = $("#customerId").val();
	var locationCode = $("#locationCode").val();
	
	var formData = {
		inventoryCheckId:inventoryCheckId,
		warehouseCode:warehouseCode,
		customerCode:customerCode,
		customerId:customerId,
		locationCode:locationCode
	};
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.INVENTORY_PLAN_CHECK_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() {  setSelect('locationCode');  }); 
    			return;
    		}
        	//mui.toast(appResponse.msg);
        	
        	//返回收货记录 可能是多条
        	var list = appResponse.extend;
        	if(list.length == 0 ){
        		//2019-05-10改为库位只要存在就行
        		
	        	$('#barcode').val("");
    	    	$('#barcode').focus();
					        		
        		//playSound('../raw/error.wav');
        		//mui.alert('后台返回list长度为0', '警告', function() {  setSelect('locationCode');  }); 
        		return;
        	} 
        	
        	refreshItems();
        	
			//默认显示第一个商品条码        	
        	//var checkDetail = list[0];
        	//$("#barcode").val(checkDetail.barcode);//条码
						 
        	$('#barcode').val("");
        	$('#barcode').focus();
        	
        	//跳到条码并选中
        	//setSelect('barcode');
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常'); 
        }
    });
}

//提交商品条码
function submitBarcode(){
    var inventoryId = $("#inventoryId").val();//库存记录id
    var barcodeShow = $("#barcodeShow").val();
    var barcode = $('#barcode').val();
	    
    if(barcodeShow != barcode){
    	//展示条码和当前扫描的条码不一样,说明换商品了.要不要提交展示条码的盘点数量submitInventoryCheck?
    	//若自动提交,适合拿一件扫一件的情况,需考虑叠加的问题
    	exeSubmitBarcode();
    	return;
    }
		
	var checkQty = $("#checkQty").val();
	if(checkQty == null || checkQty == ''){checkQty = 0;}
	$("#checkQty").val((parseInt(checkQty)+1));
	
	//回到条码输入框,用户应当继续扫描条码
	$("#barcode").val('');//清空条码,用户只需要看展示条码,最后手工点击提交
	setSelect('barcode');
}

function exeSubmitBarcode(){
	var inventoryCheckId = $("#inventoryCheckId").val();
	var warehouseCode = $("#warehouseCode").val();
	var customerCode = $("#customerCode").val();
	var customerId = $("#customerId").val();
	var locationCode = $("#locationCode").val();
	var barcode = $("#barcode").val();
	if(barcode == ''){
		return;
	}
	var formData = {
		inventoryCheckId:inventoryCheckId,
		warehouseCode:warehouseCode,
		customerCode:customerCode,
		customerId:customerId,
		locationCode:locationCode,
		barcode:barcode
	};
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.INVENTORY_PLAN_CHECK_3;
   	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;
    		}
        	
        	$("#barcodeShow").val(barcode);
        	$("#barcode").val("");
        	$('#checkQty').val(1);//盘点数量
    		    	
        	//mui.toast(appResponse.msg);
        	//返回收货记录 可能是多条
        	var list = appResponse.extend;
        	
        	//2019-05-09改为可以输入该库位原本没有的商品条码
        	if(list.length == 0 ){
				//去输入批次号码
        		mui.toast("该库位,商品条码未找到现有库存,请手工输入批次号码");
        		$('#sysQty').val("0");//系统数量
        		$("#batchNo").val("");
        		$("#batchNo").removeAttr("readonly");
        		$("#batchNo").focus();
        		
        		//系统自动输入批次?
				$("#qualityName").val("");
				$("#qualityCode").val("0");
				$('#qualityName').hide();//隐藏原品质
				$('#qualityCode').show();//显示可选批次
				
        		//playSound('../raw/error.wav');
        		//mui.alert('后台返回list长度为0', '警告', function() {  setFocus('barcode');  }); 
        		return;
        	}
        	
        	
        	//暂先只处理第一条.. 后续可改为界面选择一条
        	//提醒客户这个条码有几个批次
        	mui.toast("该库位,条码下有"+list.length+"个批次待盘点,请注意分批次盘点");
        	
        	var checkDetail = list[0];
        	$("#inventoryCheckDetailId").val(checkDetail.id);//
        	$("#inventoryId").val(checkDetail.inventoryId);//库存记录id
        	$("#inOrderNo").val(checkDetail.inOrderNo);
        	$("#batchNo").val(checkDetail.batchNo);//批次号码
        	
        	$('#qualityName').val(checkDetail.qualityName);//品质
        	$('#qualityCode').val(checkDetail.qualityCode);//品质
			$('#qualityCode').hide();//隐藏可选批次
			$('#qualityName').show();//显示原品质
			$("#batchNo").attr("readonly","readonly");
				
        	$('#sysQty').val(checkDetail.sysQty);//系统数量
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常'); 
        }
    });
}

//提交批次号码
function submitBatchNo(){
    var barcodeShow = $("#barcodeShow").val();
    if(barcodeShow == ''){
    	return;
    }
	//提示选择库存品质
	//setSelect('qualityCode');
	$("#qualityCode").focus();
	
	//去条码输入框
	//setSelect('barcode');
}

var isSubmintIng = false;//是否正在提交. 防止按回车太快
function submitInventoryCheck(){
	if(isSubmintIng){return;}
	isSubmintIng = true;
	
	var inventoryCheckCode = $("#inventoryCheckCode").val();
	var inventoryCheckId = 	$("#inventoryCheckId").val();
	
	var warehouseCode = $("#warehouseCode").val();
	
	var customerCode = 	$("#customerCode").val();
	var customerId = $("#customerId").val();
	
	var inventoryCheckDetailId = $("#inventoryCheckDetailId").val();
	var inventoryId = $("#inventoryId").val();
	var locationCode = $("#locationCode").val();
	var barcode = $("#barcode").val();
	var barcodeShow = $("#barcodeShow").val();
	barcode = barcodeShow;
	
	var inOrderNo = $("#inOrderNo").val();
	var batchNo = $("#batchNo").val();//批次号码
	var qualityName = $("#qualityName").val();
	var qualityCode = $("#qualityCode").val();
	
	var sysQty = $("#sysQty").val();
	var checkQty = $("#checkQty").val();
	
	if(inventoryCheckId == ''){
		mui.toast('请先提交盘点单号'); 
		isSubmintIng = false;
		return;
	}
	if(locationCode == ''){
		mui.toast('库位不能为空'); 
		isSubmintIng = false;
		return;
	}
	if(checkQty == '' || checkQty < 0){
        mui.toast('盘点数量不能小于0'); 
		isSubmintIng = false;
		return;
	}
	if(barcodeShow == ''){
        mui.toast('请先在商品条码输入框按回车'); 
		isSubmintIng = false;
		return;
	}
	
	var formData = {
		inventoryCheckCode:inventoryCheckCode,
		inventoryCheckId:inventoryCheckId,
		barcode:barcode,
		inventoryCheckDetailId:inventoryCheckDetailId,
		inventoryId:inventoryId,
		locationCode:locationCode,
		sysQty:sysQty,
		checkQty:checkQty,
		inOrderNo:inOrderNo,
		batchNo:batchNo,
		qualityName:qualityName,
		qualityCode:qualityCode
	};
	
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.INVENTORY_PLAN_CHECK_4;
   	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');
		        	
        	//下一个库位
        	nextLocation();
        	
        	//获取下一个库位
        	getNextLocationCode();
        	
    		//整单完成. 下一个盘点单
        	if(appResponse.extend){cleanAll();}
        },
        error: function () {
        	isSubmintIng = false;
        	nwaiting.close(); 
        	
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常'); 
        }
	});
}

function getNextLocationCode(){
	var inventoryCheckId = 	$("#inventoryCheckId").val();
		
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.INVENTORY_PLAN_CHECK_NEXT_LOCATION;
   	appRequest.msg = inventoryCheckId;//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;
			        	
			$("#prosess").html(appResponse.extend);
        	//$("#nextLocation").val(locationCode);//推荐库位
        	$("#nextLocation").html(locationCode);
        }
    });
	return locationCode;
}


//下一个条码
function nextLocation(){
	
	$("#inventoryCheckDetailId").val("");
	$("#inventoryId").val("");
	
	$("#locationCode").val("");
	$("#barcode").val("");
	$("#barcodeShow").val("");
	
	$("#inOrderNo").val("");
	$("#barcode").val("");
	$("#batchNo").val("");//批次号码
	
	$("#qualityName").val("");
	$("#qualityCode").val("0");
	$('#qualityCode').hide();//隐藏可选批次
	$('#qualityName').show();//显示原品质
	$("#batchNo").attr("readonly","readonly");
	
	$("#sysQty").val("");
	$("#checkQty").val("");
    
	$("#locationCode").focus();
}

//下一个订单
function cleanAll(){
	$("#inventoryCheckCode").val("");
	$("#inventoryCheckId").val("");
	
	$("#warehouseCode").val("");
	
	$("#customerCode").val("");
	$("#customerId").val("");
		
	$("#inventoryCheckDetailId").val("");
	$("#inventoryId").val("");
	$("#locationCode").val("");
	$("#barcode").val("");
	$("#barcodeShow").val("");
	
	$("#inOrderNo").val("");
	$("#barcode").val("");
	$("#batchNo").val("");//批次号码
	
	$("#qualityName").val("");
	$("#qualityCode").val("0");
	$('#qualityCode').hide();//隐藏可选批次
	$('#qualityName').show();//显示原品质
	$("#batchNo").attr("readonly","readonly");
	
	$("#sysQty").val("");
	$("#checkQty").val("0");
	
	$("#prosess").html("");
	//$("#nextLocation").val("");//推荐库位
    $("#nextLocation").html("");//推荐库位
    
	$("#inventoryCheckCode").focus();
}

//打开和关闭待盘点明细展示
var isShowDetails = false;
function showDetails(){
	if(!isShowDetails){
		$("#showDetails").show();
		isShowDetails = true;
		$("#showIco").attr("class", "fa fa-angle-double-up");
		
		//刷新明细
		refreshItems();
	}else{
		$("#showDetails").hide();
		isShowDetails = false;
		$("#showIco").attr("class", "fa fa-angle-double-down");
	}
	//回到上次焦点地方
	if(focusId!=null && focusId != ''){
		$("#"+focusId).focus();		
	}
}
function refreshItems(){
	var inventoryCheckId = $("#inventoryCheckId").val();
	var warehouseCode = $("#warehouseCode").val();
	var customerCode = $("#customerCode").val();
	var customerId = $("#customerId").val();
	var locationCode = $("#locationCode").val();
	if(inventoryCheckId == '' || locationCode == ''){return;}
	
	
	$("#itemList").empty();
		
	var formData = {
		inventoryCheckId:inventoryCheckId,
		warehouseCode:warehouseCode,
		customerCode:customerCode,
		customerId:customerId,
		locationCode:locationCode
	};
		
	var appRequest = $appRequest;//复制请求封装对象
   	appRequest.opType = OP_TYPE_MENU.INVENTORY_PLAN_CHECK_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');
    			return;
    		}
        	var list = appResponse.extend;
        	if(list.length == 0 ){
        		playSound('../raw/error.wav');
        		return;
        	}
    		for(var index = 0; index < list.length; index ++){
    			var item = list[index];
    			handleNullFiled(item);
    			
    			var tr = "<tr id='item_"+item.id+"'>";
	    			tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+item.barcode+"</div></th>";
	    			tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+item.batchNo+"</div></th>";
	    			tr += "<th><div style='width:100%;overflow: hidden;font-size: 9pt;text-align: left;'>"+item.sysQty+"</div></th>";	
    			tr += "</tr>";
    			
    			$("#itemList").append(tr);
    		}
        },
        error: function () {
        	playSound('../raw/error.wav');
        	mui.toast('网络断开或服务器发生异常'); 
        }
    });
}

function selectQuality(){
	var qualityCode = $("#qualityCode").val();
	if(qualityCode != '0'){
		//去条码输入框
		setSelect('barcode');		
	}
}
