//模态框保存操作
function save(){
	$("#applyState").val(1);
	saveMethod(1);
}
//模态框提交操作
function add(){
	$("#applyState").val(2);
	saveMethod(2);
}

//封装模态框保存公共方法，applyState:提交状态1保存未提交2已提交.msg:操作后提示信息
function saveMethod(applyState){
	var inpatientNo = $("#inpatientNo").val();
	var name = $("#name").val();
	if($("#effeTime").val() != ''){
		if($("#applyType").val() != '') {
			if (inpatientNo != '') {
				if (name != '') {
					$.ajax({
						type: 'post',
						url: path + '/approve/addApplyBlood',
						data: $("#form3").serialize(),
						dataType: 'json',
						success: function (data) {
							if (data != null && data.code == 0) {
								if (applyState == 1) {
									toastr.success("保存成功!");
								} else if (applyState == 2) {
									toastr.success("申请成功，目前审核中，请耐心等待!");
								}
								setTimeout(function () {
									window.location.reload();
								}, 500)
								/*refreshTable();
                                $('#myModal3').modal('hide');
                                if($("#approveId").val() != ''){
                                    $('#mytab').bootstrapTable('selectPage', pageNumber);
                                }*/
							} else {
								toastr.warning(data.msg);
							}
						}
					})
				} else {
					toastr.warning("姓名不能为空!");
					$("#name").focus();
				}
			} else {
				toastr.warning("脐血编号不能为空!");
				$("#inpatientNo").focus();
			}
		}else{
			toastr.warning("申请类型不能为空!");
			$("#applyType").focus();
		}
	}else{
		toastr.warning("有效时间不能为空!");
		$("#effeTime").focus();
	}
}
/*****************************************输入框边输入内容边搜索功能方法*************************************************/
//级联病案号，且长度超过4个字节才查询
function getInpatientNoContents(id) {
	var keyword = document.getElementById(id).value;
	if (keyword.length > 4 || $("#name").val()!='') {
		var ajax = new XMLHttpRequest();
		var url = path+"/approve/getInpatientNosByName?inpatientNo=" + keyword +"&name="+$("#name").val();
		ajax.open("GET", url, true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					var result = ajax.responseText;
					insertContent(result,id);
				}
			}
		}
	} else {
		clearContent(id);
		return;
	}
}
//级联患者姓名内容
function getNameContents(id) {
	var keyword = document.getElementById(id).value;
	if ($("#name").val()!='' || $("#inpatientNo").val() != '') {
		var ajax = new XMLHttpRequest();
		var url = path+"/approve/getNameByInpatientNo?inpatientNo=" + $("#inpatientNo").val() +"&name="+keyword;
		ajax.open("GET", url, true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					var result = ajax.responseText;
					insertContent(result,id);
				}
			}
		}
	} else {
		clearContent(id);
		return;
	}
}
//添加内容，tr td
function insertContent(content,id) {
	clearContent(id);
	setLocation(id);
	var json = JSON.parse(content);
	for (key in json) {
		var tr = document.createElement("tr");
		var td1 = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");
		var td4 = document.createElement("td");
		tr.setAttribute("bgcolor", "#FFFAFA");
		tr.setAttribute("border", "0");
		td1.innerHTML = "病案号:";
		td2.innerHTML = key;
		td3.innerHTML = "姓名:";
		td4.innerHTML = json[key];
		tr.appendChild(td1);
		tr.appendChild(td2);
		tr.appendChild(td3);
		tr.appendChild(td4);
		document.getElementById(id+"Content").appendChild(tr);
		tr.onmouseover = function() {
			this.className = 'mouseOver';
			if(id == 'inpatientNo'){
				document.getElementById(id).value=$(this).children("td").eq(1).html();
			}else if(id == 'name') {
				document.getElementById(id).value=$(this).children("td").eq(3).html();
			}
		};
		tr.onmouseout = function() {
			this.className = 'mouseOut';
		};
	}
}
//清楚内容方法
function clearContent(id) {
	var popNode = document.getElementById(id+"Div");
	popNode.style.border = "none";
	var contentNode = document.getElementById(id+"Content");
	var len = contentNode.childNodes.length;
	for (var i = len - 1; i >= 0; i--) {
		contentNode.removeChild(contentNode.childNodes[i]);
	}
}
//设置样式
function setLocation(id){
	var inputNode = document.getElementById(id);
	var width = inputNode.offsetWidth;
	var left = inputNode["offsetLeft"];
	var top = inputNode.offsetHeight+inputNode["offsetTop"];
	var popNode = document.getElementById(id+"Div");
	popNode.style.border = "gray 0.5px solid";
	popNode.style.width = width+"px";
	popNode.style.top = top+"px";
	popNode.style.left = left+"px";
	document.getElementById(id+"Content_table").style.width=width+"px";
}