$(function() {

	var focusId;
	$('#barcode').focus();
	//如不使用延迟执行, 有时候会无效
	setTimeout(function() {
		$('#barcode').focus();
	}, 1000);
	
	$("#length").keyup(function(event){
		if(event.keyCode != "13") {
			return false;
		}
		setFocus('width')
	})
	$("#width").keyup(function(event){
		if(event.keyCode != "13") {
			return false;
		}
		setFocus('height')
	})
	$("#height").keyup(function(event){
		if(event.keyCode != "13") {
			return false;
		}
		setFocus('shelfLifeDays')
	})
	
	$("#shelfLifeDays").keyup(function(event){
		if(event.keyCode != "13") {
			return false;
		}
		setFocus('grossWeight')
	})

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

});

var singleBarcodeEntity;
var singleBarcodeId=-1;
var singleBarcodeMap = {};

function submitBarcode() {
	
	$(".allowClear").each(function() {
		if($(this).attr('name') != 'barcode'){
			$(this).val("")
		}
	})
	$("#customerList").html("")
	
	
	
	singleBarcodeId = -1;
	
	var barcode = $("#barcode").val();
	var msgBody = {};
	msgBody.barcode = barcode;
	
	//序列化成字符串
	var msgBodyStr = JSON.stringify(msgBody)

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

    console.log("req:"+JSON.stringify(appRequest))
    console.log('$appRequestUrl:'+$appRequestUrl)
   
	$.ajax({
		type: 'POST',
		url: $appRequestUrl,
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(appRequest),
		success: function(appResponse) {
			console.log("appResponse:"+JSON.stringify(appResponse))
			if(!appResponse.success || appResponse.extend ==null || appResponse.extend.length == 0) {
				playSound('../raw/error.wav');
				mui.toast(appResponse.msg);
				return;
			}
			
			singleBarcodeMap = {};
			
			//
			var productBarcodeList = appResponse.extend
			if(productBarcodeList.length==1){
				singleBarcodeEntity = productBarcodeList[0];
				fillBarcodeDetail(singleBarcodeEntity);
				setFocus('length')
			}
			//组装成下拉框
			var listCustomer=[];
			
			for (var i =0 ;i < productBarcodeList.length ; i++) {
				var productBarcodeSingle = productBarcodeList[i]
				singleBarcodeMap[productBarcodeSingle['id']] = productBarcodeSingle;
				console.log('this[id]:'+productBarcodeSingle['id'])
				console.log('this[customerCode]:'+productBarcodeSingle['customerCode'])
				listCustomer.push('<option value="'+productBarcodeSingle['id']+'">'+productBarcodeSingle['customerCode']+'</option>')
			}
			

			var listCustomerValue = listCustomer.join('');
			if(listCustomer.length>1){
				listCustomerValue='<option value="-1">请选择货主</option>'+listCustomerValue;
				mui.toast("该条码在系统中存在多条记录,请选择其中一条进行操作");
			}
			$('#customerList').html(listCustomerValue);
			if(listCustomer.length>1){
				//customerList
				$("#customerList").trigger("click")
			}
			
		},
		error: function() {
			console.log('异常')
			playSound('../raw/error.wav');
			mui.toast('网络断开或服务器发生异常');
		}
	});
}

function selectSingleCustomer(obj){
	singleBarcodeId = $(obj).val();
	if(singleBarcodeId == -1){
		$(".allowClear").each(function() {
		if($(this).attr('name') != 'barcode'){
			$(this).val("")
		}
	   })
		return ;
	}
	singleBarcodeEntity = singleBarcodeMap[singleBarcodeId];
	if(singleBarcodeEntity !=null && singleBarcodeEntity){
		fillBarcodeDetail(singleBarcodeEntity)
	}
	setFocus('length')
}

/**
 * 填充商品信息
 * @param {Object} singleBarcodeEntity
 */
function fillBarcodeDetail(singleBarcodeEntity){
	singleBarcodeId = singleBarcodeEntity.id;
	fillForm(singleBarcodeEntity,['length','width','height','shelfLifeDays','grossWeight','sku'])
}

function fillForm(entity,fileds){
	for (var i =0 ; i <fileds.length ; i++) {
		var filedSingle = fileds[i];
		console.log("filedSingle:"+filedSingle)
		var filedValue=entity[filedSingle]
		console.log("filedValue:"+filedValue)
		
		if(filedSingle=='grossWeight' && filedValue != null && filedValue != undefined){
			filedValue = filedValue*1000.0
		}
		
		if(filedValue == null || filedValue == undefined)
		    filedValue='';
		$("[name='"+filedSingle+"']").val(filedValue)
	}
}

function getEntity(fileds){
	var reqObj = {};
	for (var i =0 ; i <fileds.length ; i++) {
		var filedSingle = fileds[i];
		reqObj[filedSingle] = $("[name='"+filedSingle+"']").val();
	}
	return reqObj;
}


//提交
function submitMaintain() {
    if(singleBarcodeId == -1){
		mui.toast("条码在系统中不存在或未选择货主");
		return
	}
	
	var msgBody = getEntity(['length','width','height','shelfLifeDays','grossWeight','sku','customerCode']);
	 
	var grossWeight = msgBody.grossWeight
	
	if(grossWeight != null && grossWeight != undefined && grossWeight!=0){
		msgBody.grossWeight = grossWeight/1000.0;
	}
	 
	msgBody.customerCode = singleBarcodeMap[msgBody.customerCode].customerCode;
	//序列化成字符串
	var msgBodyStr = JSON.stringify(msgBody)

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

	plus.nativeUI.showWaiting();
	$.ajax({
		type: 'POST',
		url: $appRequestUrl,
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(appRequest),
		success: function(appResponse) {
			console.log("msgBodyStr:"+msgBodyStr)
			console.log("appResponse:"+JSON.stringify(appResponse))
			plus.nativeUI.closeWaiting();
			if(!appResponse.success) {
				playSound('../raw/error.wav');
				mui.toast(appResponse.msg);
				return;
			}
			mui.toast(appResponse.msg);

			cleanAll();

		},
		error: function() {
			plus.nativeUI.closeWaiting();
			playSound('../raw/error.wav');
			mui.toast('网络断开或服务器发生异常');
		}
	});
}

//下一个转移任务
function cleanAll() {
	singleBarcodeId = -1;
	$(".allowClear").each(function() {
		$(this).val("")
	})
	$("#customerList").html("")
	$("#barcode").focus();
}