$(function() {

	var focusId;
	$('#barcode').focus();
	//如不使用延迟执行, 有时候会无效
	setTimeout(function() {
		$('#barcode').focus();
	}, 1000);

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

});

function submitBarcode() {
	var barcode = $("#barcode").val();
	var customerCode = $("#customerCode").val();
	var msgBody = {};
	msgBody.barcode = barcode;
	msgBody.customerCode = customerCode;

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

	var appRequest = $appRequest; //复制请求封装对象
	appRequest.opType = OP_TYPE_MENU.CHECK_PRODUCT;
	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) {
				playSound('../raw/error.wav');
				mui.toast(appResponse.msg);
				return;
			}
			var list = appResponse.extend; //ProductBarcodelist
			if(list.length == 0){
				playSound('../raw/error.wav');
				mui.toast('该条码未查到商品信息');
				return;
			}
				
			//先判断是否已经选择了货主,如果选择了并且该货主也存在结果中,不能清空,不然每次扫描条码都要选货主
			var oldCustomerCode = $("#customerCode").val();
			
			$("#customerCode").empty();
			
			//重新设置select
			for(var i = 0; i < list.length; i++) {
				var pb = list[i];
				$("#customerCode").append("<option value='" + pb.customerCode + "'>" + pb.customerCode + "</option>"); //添加option	
			}
			$("#customerCode").val(oldCustomerCode);
			
			countPicture();			
			return;
		},
		error: function() {
			playSound('../raw/error.wav');
			mui.toast('网络断开或服务器发生异常');
		}
	});
}

//看条码+货主是否已经有图片,避免重复拍照
function countPicture() {
	var barcode = $("#barcode").val();
	var customerCode = $("#customerCode").val();
	var msgBody = {};
	msgBody.barcode = barcode;
	msgBody.customerCode = customerCode;

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

	var appRequest = $appRequest; //复制请求封装对象
	appRequest.opType = OP_TYPE_MENU.COUNT_PRODUCT_PICTURE;
	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) {
				return;
			}
			
			var msg = appResponse.msg;
			$("#tips").text(msg);
		},
		error: function() {
			mui.toast('网络断开或服务器发生异常');
		}
	});
}

//提交拍照
function submitUpload() {
	var barcode = $("#barcode").val();
	var customerCode = $("#customerCode").val();
	
	var msgBody = {};
	msgBody.barcode = barcode;
	msgBody.customerCode = customerCode;
	msgBody.imgArray = imgArray; //图片base64数据          
	
	if(barcode == null || barcode == ''){
		mui.toast('商品条码不能为空');
		return false;
	}
	if(customerCode == null || customerCode == ''){
		mui.toast('货主代码不能为空');
		return false;
	}	
	if(imgArray == null){
		mui.toast('图片不能为空');
		return false;
	}
	
	//序列化成字符串
	var msgBodyStr = JSON.stringify(msgBody)

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

	mui.toast('开始上传图片');
	plus.nativeUI.showWaiting();
	$.ajax({
		type: 'POST',
		url: $appRequestUrl,
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(appRequest),
		success: function(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() {
	$(".allowClear").each(function() {
		$(this).val("")
	})
		
	imgArray.splice(0, imgArray.length);
	document.getElementsByClassName("showimg")[0].innerHTML = null;

	$("#barcode").focus();
}