$(function() {
	// 提交数据保存信息
	$(".interimbtn").click(function() {
		saveTemporayVisitorForm();
	});
});

function uploadImage(base64, clearBase64) {
	$.ajax({
				url : $("#ctx").val() + "/wechat/visitor/validateImage",
				type : "POST",
				dataType : "json",
				data : {
					imageUrl : clearBase64
				},
				timeout : 100000,
				beforeSend : function(xhr) {
					showLoading("上传图片中");
				},
				success : function(data) {
					closeLayer();
					if (data.flag) {
						var html = "";
						html += "<img src=" + data.url + " />";
						$(".cancelicon").show();
						$(".uploadpicture").html(html);
						$("#imageUrl").val(data.imageUrl);
					} else {
						showMessage(data.desc);
						var html = "";
						html += "<input id='uploadImage' type='file' pictype='30010003' data-role='none' accept='image/*' onchange='selectFileImage(this)' />";
						$(".uploadpicture").html(html);
						$(".cancelicon").hide();
					}
				},
				error : function(xhr, textStatus) {
					showMessage("请求超时！");
					$("#imageUrl").val("");
					html += "<input id='uploadImage' type='file' pictype='30010003' data-role='none' accept='image/*' onchange='selectFileImage(this)'/>";
					$(".uploadpicture").html(html);
					$(".cancelicon").hide();
				}
			});
}

// 保存信息方法
function saveTemporayVisitorForm() {
	var userPhone = $("#userPhone").val();
	if (validateisNotNull(userPhone)) {
		focus("userPhone");
		showMessage("请输入被访人手机号码");
		return;
	}
	if (validatePhone(userPhone)) {
		focus("userPhone");
		showMessage("被访人手机号码有误 ，请重填");
		return;
	}
	var visitorName = $("#visitorName").val();
	if (validateisNotNull(visitorName)) {
		showMessage("请输入姓名");
		focus("visitorName");
		return;
	}
	if (validateNameLength(visitorName)) {
		showMessage("姓名长度必须在1-14之间");
		focus("visitorName");
		return;
	}
	var visitorPhone = $("#visitorPhone").val();
	if (validateisNotNull(visitorPhone)) {
		showMessage("请输入您的手机号码");
		focus("visitorPhone");
		return;
	}
	if (validatePhone(visitorPhone)) {
		showMessage("您的手机号码有误 ，请重填");
		focus("visitorPhone");
		return;
	}
	var imageUrl = $("#imageUrl").val();
	if (validateisNotNull(imageUrl)) {
		showMessage("请上传头像");
		return;
	}
	var imageFacePlusUrl = $("#imageFacePlusUrl").val();
	var imageFacePlusId = $("#imageFacePlusId").val();
	var visitorOpenId = $("#openId").val();
	$.ajax({
		url : $("#ctx").val() + "/wechat/visitor/submitTemporayVisitor",
		type : "POST",
		dataType : "json",
		data : {
			userPhone : userPhone,
			visitorName : visitorName,
			visitorPhone : visitorPhone,
			imageUrl : imageUrl,
			visitorOpenId : visitorOpenId
		},
		beforeSend : function(xhr) {
			showLoading("提交信息中");
		},
		success : function(data) {
			closeLayer();
			if (data.flag) {
				showMessage(data.desc);
				WeixinJSBridge.call("closeWindow");
				// location.href=$("#ctx").val()+"/wechat/visitor/getVisitorInfoList?visitorOrderId="+data.visitorOrderId+"&type="+data.type;
			} else {
				showMessage(data.desc);
			}
		},
		error : function(xhr, textStatus) {
			showMessage("请求超时！");
			$("#imageUrl").val("");
			$("#imageFacePlusUrl").val("");
			$("#imageFacePlusId").val("");
		}
	});
}

function selectFileImage(fileObj) {
	var file = fileObj.files['0'];
	var Orientation = null;

	if (file) {
		console.log("正在上传,请稍后...");
		var rFilter = /^(image\/jpeg|image\/png)$/i; // 检查图片格式
		if (!rFilter.test(file.type)) {
			showMessage("图片格式有误！");
			return;
		}
		EXIF.getData(file, function() {
			EXIF.getAllTags(this);
			Orientation = EXIF.getTag(this, 'Orientation');
		});

		var oReader = new FileReader();
		oReader.onload = function(e) {
			var image = new Image();
			image.src = e.target.result;
			image.onload = function() {
				var expectWidth = this.naturalWidth;
				var expectHeight = this.naturalHeight;

				if (this.naturalWidth > this.naturalHeight
						&& this.naturalWidth > 800) {
					expectWidth = 800;
					expectHeight = expectWidth * this.naturalHeight
							/ this.naturalWidth;
				} else if (this.naturalHeight > this.naturalWidth
						&& this.naturalHeight > 1200) {
					expectHeight = 1200;
					expectWidth = expectHeight * this.naturalWidth
							/ this.naturalHeight;
				}
				var canvas = document.createElement("canvas");
				var ctx = canvas.getContext("2d");
				canvas.width = expectWidth;
				canvas.height = expectHeight;
				ctx.drawImage(this, 0, 0, expectWidth, expectHeight);
				var base64 = null;
				var clearBase64 = null;
				var mpImg = new MegaPixImage(image);
				mpImg.render(canvas, {
					maxWidth : 800,
					maxHeight : 1200,
					quality : 0.6,
					orientation : Orientation
				});
				base64 = canvas.toDataURL("image/jpeg", 0.6);
				if (base64 != null) {
					clearBase64 = base64.substr(base64.indexOf(',') + 1)
				}
				uploadImage(base64, clearBase64);
			};
		};
		oReader.readAsDataURL(file);
	}
}

function delImage() {
	var html = "";
	html += "<input id='uploadImage' type='file' pictype='30010003' data-role='none' accept='image/*' onchange='selectFileImage(this)' />";
	$(".uploadpicture").html(html);
	$("#imageFacePlusUrl").val("");
	$("#imageFacePlusId").val("");
	$(".cancelicon").hide();
}
