//初始化函数
$(function(){
	//加载其他诊断名称鼠标滑过显示全部文本
	//loadQtip('.otherDiagName');
	//加载病案信息
	loadRecord();
	//加载住院次数
	loadAdmissTimes();
	//加载iframe
	$("#iframe").prop("src",path+"/commom/showRecordIframeBlood?patientId="+$("#patientId").val()+"&flag=file_path");
})
//封装其他诊断名称鼠标滑过显示全部文本
function loadQtip(selector){
	$(selector).qtip({
		content: {text:$('#otherDiagName').val()},
		show: {
			effect: function() {
				$(this).slideDown();
			}
		},
		hide: {
			effect: function() {
				$(this).slideUp();
			}
		}
	});
}

//加载病案信息
function loadRecord(){
	$.ajax({
		type:'get',
		url:path+'/commom/getRecord',
		data:{patientId:$("#patientId").val(),flag:$("#flag").val()},
		async:false,
		dataType:'json',
		success:function (data) {
			if(null != data && data != ''){
				$("#inpatientNo").val(data.inpatientNo);
				$("#name").val(data.name);
				$("#admissTimes1").val(data.admissTimes);
				$("#inpatientNo1").val(data.inpatientNo);
				$("#name1").val(data.name);
				$("#admissTimesValue").val(data.admissTimes);
				var disDate = data.disDate;
				disDate = covertDate(disDate);
				$("#disDate").val(disDate);
				$("#disDeptName").val(data.disDept);
				$("#diagName").val(data.mainDiagName);
				var delCount = data.delCount;
				//不为空，有已删除的图片，显示恢复图片按钮
				if(delCount != null){
					$("#recoverPic").show();
				}
				//判断哪家医院赋值扫描图片和新上传图片的根目录集合
				var flag = $("#flag").val();
				var rootPaths = "";
				if(flag != '' && flag == 'home_addr'){
					//脐血库
					rootPaths = data.homeAddr + ",";
				}else if(flag != '' && flag == 'file_path'){
					//祈福医院
					rootPaths = data.filePath + ",";
				}
				if(rootPaths == 'undefined,'){
				    rootPaths = 'null,';
                }
				//拼接新上传图片根目录
				if(data.newPath != null && data.newPath != ''){
					rootPaths += data.newPath;
				}
				$("#rootPaths").val(rootPaths);
				$("#otherDiagName").val(data.otherDiagName);
				//重新加载其他诊断名称鼠标滑过显示全部文本
				loadQtip('.otherDiagName');
			}
		}
	})
}

//加载住院次数
function loadAdmissTimes(){
	$.ajax({
		type:'get',
		url:path+'/commom/getAdmissTimes',
		data:{name:$("#name").val(),inpatientNo:$("#inpatientNo").val()},
		dataType:'json',
		success:function (data) {
			if(null != data && data != ''){
				$("#admissTimes").empty();
				var html = '';
				for (var i = 0; i < data.length; i++) {
					if($("#admissTimesValue").val() == (data[i].admissTimes)){
						html += '<option value="'+data[i].admissTimes+'" selected>'+data[i].admissTimesName+'</option>';
					}else{
						html += '<option value="'+data[i].admissTimes+'">'+data[i].admissTimesName+'</option>';
					}
				}
				$("#admissTimes").append(html);
			}
		}
	})
}
//住院次数变更事件
$("#admissTimes").change(function () {
	var val = $(this).val();
	$(".admissTimes").val(val);
	var inpatientNo = $("#inpatientNo").val();
	var name = $("#name").val();
	//变更基本信息，出院时间，出院科室，主要诊断，其他诊断
	$.ajax({
		type:'post',
		url:path+"/commom/getOtherDiagNameByAdmissTimesAndInpatientNo",
		data:{name:name,admissTimes:val,inpatientNo:inpatientNo,startTime2:$("#disDate").val()},
		dataType:'json',
		success:function(d){
			if(d != null){
				$("#patientId").val(d.patientId);
				$("#iframe").prop("src",path+"/commom/showRecordIframeBlood?patientId="+d.patientId);
				var disDate = d.disDate;
				disDate = covertDate(disDate);
				$("#disDate").val(disDate);
				$("#disDeptName").val(d.disDept);
				$("#diagName").val(d.mainDiagName);
				$("#otherDiagName").val(d.otherDiagName);
				//重新加载其他诊断名称鼠标滑过显示全部文本
				loadQtip('.otherDiagName');
				//变更收藏信息
				$("#collectId").val("");
				$("#collectTime").val(currentDate);
				clearCollect();
				$.ajax({
					type:'post',
					url:path+"/collect/getCollectByUserIdAndPatientIdAndAdmissTimes",
					data:{patientId:d.patientId,admissTimes:$("#admissTimes").val()},
					dataType:'json',
					success:function(data){
						if(data != null){
							$("#collectId").val(data.id);
							$("#collectTime").val(data.collectTime);
							$("#title").val(data.title);
							$("#parentId").val(data.parentId);
							$("#content").val(data.content);
						}
					}
				})
			}
		}
	})
})
/*****************************模态框2******************************/
	//当前时间
var currentDate = getNowFormatDate();
//加载收藏类别
function loadCollectType() {
	$.ajax({
		type:'get',
		url:path+'/collect/getCollectType',
		dataType:'json',
		async:false,
		success:function (data) {
			if(null != data && data != ''){
				$("#parentId").empty();
				var html = '';
				for (var i = 0; i < data.length; i++) {
					html += '<option value="'+data[i].id+'">'+data[i].collectTypeName+'</option>';
				}
				$("#parentId").append(html);
			}
		}
	})
}
//新增图片资料按钮方法
function addPic(){
	loadPicAssort();
	//清楚选中图片
	$(".up-section").remove();
}

//高拍仪跳转地址
function openUrl(){
	var patientId = $("#patientId").val();
	var url='http://172.18.0.227:91/GPYstart.aspx?patientid='+patientId
	window.open(url,'_blank');
}
//加载收藏对象
function loadCollect() {
	$.ajax({
		type:'get',
		url:path+'/collect/getCollectById',
		data:{patientId:$("#patientId").val()},
		dataType:'json',
		async:false,
		success:function (data) {
			$("#collectId").val('');
			$("#collectForm")[0].reset();
			if(null != data && data != ''){
				$("#collectId").val(data.id);
				$("#title").val(data.title);
				$("#collectTime").val(data.collectTime);
				$("#parentId").val(data.parentId);
				$("#content").val(data.content);
				$("#title").val(data.title);
			}
		}
	})
}
//点击收藏按钮功能
function collect(){
	//加载收藏类别
	loadCollectType();
	//加载收藏对象
	loadCollect();
	setFormToken();
	//未收藏过给收藏时间赋值当前时间
	if($("#collectTime").val() == ''){
		$("#collectTime").val(currentDate);
	}
}
//添加收藏
function saveCollect(){
	var parentId = $("#parentId").val();
	if(parentId != '' && parentId != null){
		$.ajax({
			type:'post',
			url:path+"/collect/addCollect",
			data:{
				id:$("#collectId").val(),
				title:$("#title").val(),
				collectTime:$("#collectTime").val(),
				parentId:$("#parentId").val(),
				content:$("#content").val(),
				admissId:$("#admissId").val(),
				inpatientNo:$("#inpatientNo").val(),
				admissTimes:$("#admissTimes").val(),
				index:$("#iframe").contents().find("#iframe1").contents().find("#pageNumber").val(),
				patientId:$("#iframe").contents().find("#patientId").val(),
				assortIds:$("#iframe").contents().find("#assortIds").val(),
				name:$("#name1").val()
			},
			dataType:'json',
			success:function(data){
				if(data.code == 0){
					toastr.success("收藏成功！")
					$('#myModal2').modal('hide')
				}else{
					toastr.error(data.msg);
				}
			}
		})
	}else{
		toastr.warning("收藏失败，收藏类别不能为空，如若没有为添加收藏类别请先到个人收藏页面添加类别！");
	}
}
//清空收藏表单
function clearCollect(){
	$("#title").val("");
	$("#parentId").val("");
	$("#content").val("");
}
//bootstap时间插件
$('.datepicker').datepicker({
	format: 'yyyy-mm-dd',
	autoclose: true
})
//获取当前时间
function getNowFormatDate() {
	var date = new Date();
	var seperator1 = "-";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}
//加载分段
function loadPicAssort(){
	$.ajax({
		type:'get',
		url:path+'/commom/getAssortList',
		dataType:'json',
		async:false,
		success:function (data) {
			if(data.code == 0){
				data = data.data;
				$("#assortId").empty();
				var html = '<option value="">请选择分类</option>';
				for (var i = 0; i < data.length; i++) {
					html += '<option value="'+data[i].assortId+'">'+data[i].assortName+'</option>';
				}
				$("#assortId").append(html);
				$("#assortId").val('C7C73CD034B440F6B33A79E382A5610F')
			}else{
				toastr.warning(data.msg);
			}
		}
	})
}
//保存图片
function save(){
	var formData = new FormData();
	var assortId = $("#assortId").val();
	if(assortId != ''){
		if(file1.length > 0){
			for (var i = 0; i < file1.length; i++) {
				formData.append('files', file1[i]);
			}
			formData.append('patientId',$("#patientId").val());
			formData.append('assortId', assortId);
			formData.append('name',$("#name").val());
			formData.append('flag', $("#flag").val());
			$.ajax({
				url:path+'/commom/updatePic',
				data:formData,
				type :  "post",
				processData: false,
				contentType: false,
				dataType:'json',
				success : function(data){
					if(data.code == 0){
						toastr.success("上传成功");
						$('#myModal3').modal('hide');
					}else{
						toastr.error(data.msg);
					}
				}
			})
		}else{
			toastr.warning("请选择图片!")
		}
	}else{
		toastr.warning("请选择分类!")
	}
}
//监听关闭模态框刷新事件
$('#myModal3').on('hide.bs.modal', function () {
	window.location.reload();
});
