
//当文档载入完毕初始化
$(function(){
//	displayAllInsuranceType();
	displayGroupInfo("1");
	displayClasscodeTable("1");
});

//获取选择条件
var selectType = "hospitalName";

//根据选择的查询类型变换输入框,1医院名称输入框.2医院所在省下拉选择框
function getSelectType(element){
	var path=location.href.substring(0,location.href.lastIndexOf('/'));
	path=path.substring(0,path.lastIndexOf('/'));
	selectType=element.id;
	 if(selectType=="hospitalName"){
		 $(".inputSelect").html("<input type=\"text\" class=\"input i_s_240 f float_l\" id=\"keyword\" onkeydown=\"enterQuery()\"/>");
	 }else{
		 $(".inputSelect").html("<input type=\"hidden\" name=\"hosProvince\" id=\"hosProvinceChoose\"/>"+
                       "<td width=\"30%\"><div id=\"divhosProvinceChoose\" class=\"selectChoose i_s_240 float_l\" " +
                       "style=\"cursor:pointer; padding-right:25px; height:28px; border:1px solid #adadad; " +
                       "border-radius:2px; background:url('"+path+"/resources/img/icon_sel.gif') no-repeat right center;" +
                       		" behavior:url(../../js/PIE.htc)/*CSS3*/;\">"+ 
                       "<span class=\"sel_text\" id=\"provinceChoose\">请选择省份</span>"+
                         "<div class=\"sel_list i_ul_265\" style=\"display:none; height: 300px; overflow: scroll;\" >"+
                          " <ul style=\"\" id=\"provinceContextChoose\"></ul>"+
                        " </div>"+
                       "</div></td>");
		 $.ajax({
				
			 type:"POST",
			 url:"getProvince.do",
			 data:"",
			 error:function(){
				 alert("获取省份失败");
				 return;
			 },
			 success:function(data){
				 
				 for(var i=0;i<data.length;i++){
					 var province = data[i];
					 $("#provinceContextChoose").append("<li id="+province.cityCode+" onclick=\"getProvinceChoosedCode(this)\">"+province.cityName+"</li>");
				 }
				 getSelectContent();
			 }
			 
		 });
		 
	 }
}

function getSelectContent(){
	 $(".selectChoose").hover( function () {
			$(this).addClass("sel_over");
		},function () {
			$(this).removeClass("sel_over");
		}).on('click',function(){
			var _this = $(this),
				_bthis = $(this).find(".sel_list");
			if ( _this.attr('isopen') ) {
				_bthis.slideUp(0 , function(){
					_this.removeAttr('isopen');
				});			
			} else {
				_bthis.slideDown(0,function(){
					_this.attr('isopen',"1");
				});	
			}
		}).on('mouseleave',function(){
			var _thisa = $(this).find(".sel_list"),
				_this = $(this);
			_thisa.hide();
			_this.removeAttr('isopen');
		}),$(".selectChoose").find(".sel_list li").hover( function (){
			$(this).addClass("sel_hov");		
		},function (){
			$(this).removeClass("sel_hov");	
		}).on('click',function(){
			var _this = $(this).html();
			
			var _thisText = $(this).closest(".selectChoose").find(".sel_text");
			_thisText.html(_this);
			
		});
	 
}


//获取查询条件选择省份
var ChoosedProvinceCode = null;
function getProvinceChoosedCode(element){
	 ChoosedProvinceCode = element.id;
}

//查询
function queryHospitalInfo(f){
	
	$("#check-all-hospital").removeClass("check_yes").addClass("check_no");
	checkSe();
	var params = {};
	if (selectType == "0") {
		return;
	}
	if (selectType == "hospitalName") {
		params['hospitalName'] = $("#keyword").val();
	} else if (selectType == "hosProvince") {
		params['hosProvince'] = ChoosedProvinceCode;
	}
	var keyword = $("#keyword").val();
	params['pageNumber'] = f;
	//如果没有输入查询关键词就提示
	if(keyword==""){
		alert("请输入查询关键词!");
		return;
	}
	

	$.ajax({
		type : "POST",
		url : "findDesignatedHospital.do",
		data : params,
		error : function() {
			alert("查询失败");
			return;
		},
		success : function(data) {
			$("#hospital-context").empty();
			var designatedHospitalVoList = data.designatedHospitalVoList;
			if(designatedHospitalVoList != null && designatedHospitalVoList != ""){
				$("#hospital-total").val(data.total);
				$("#hospital-currentPage").val(data.pageNumber);
				
				$("#hospital-nav").empty();
				$("#hospital-context").empty();
				$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"pre(this,'hospital')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&lt;</span></a>");
				
				
				for ( var i = 0; i < designatedHospitalVoList.length; i++) {
					var hospital = designatedHospitalVoList[i];
					$("#hospital-context").append("<tr style=\"cursor:pointer\"><td><span name='icheckbox' onclick='changeCheckBox(this)' is-update=\""+hospital.isEdit+"\" class=\"check_no\" id=\""+getIsNull(hospital.hospitalCode)+ "\"></span></td><td>"+getIsNull(hospital.hospitalName)+"</td><td>"+getIsNull(hospital.headOrBranchCN)+ "</td><td>"+getIsNull(hospital.medicalInsuranceTypeCN)+"</td><td>"+getIsNull(hospital.hospitalLevelString)+ "</td><td>"+getIsNull(hospital.hospitalAttrString)+"</td><td>"+getIsNull(hospital.hospitalEconomicTypeCN)+"</td><td>"+getIsNull(hospital.hosProvinceString)+ "</td><td>"+getIsNull(hospital.hosCityString)+"</td><td>"+getIsNull(hospital.keyDepartment)+"</td><td>"+getIsNull(hospital.isSpecialHospital)+"</td><td>"+getIsNull(hospital.isBlacklist)+"</td></tr>");
				}
				/*for ( var i = 1; i <= data.total; i++) {
					$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'hospital')\" style=\"cursor:pointer;\"><span>"+i+"</span></a>");
				}*/
				
				////
				var j = 1;
				for( var i = data.pageNumber ; i <= data.total; i++){
					if(j <= data.pageNumber+3){
 						$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'hospital')\" style=\"cursor:pointer;\"><span>"+i+"</span></a>");
					}else{
						break;
					}
					j++;
				}
				
				if(data.total > data.pageNumber+3 ){
					$("#hospital-nav").append("<span>...</span>");
				}
				
				if(data.total == data.pageNumber+7 ){
						$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'hospital')\" style=\"cursor:pointer;\"><span>"+data.total+"</span></a>");
				}
				
				if(data.total > data.pageNumber+7 ){
					for ( var k = data.total-2 ; k <= data.total; k++) {
						$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'hospital')\" style=\"cursor:pointer;\"><span>"+k+"</span></a>");
				}
			}
				////
				
				$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"next(this,'hospital')\" style=\"cursor:hospital;\"><span class=\"turn_btn\">&gt;</span></a>");
				
                $("#hospital-nav .turn_cur").removeAttr("class");
            	$("#hospital-nav").find("span").each(function(){
            		if($(this).html()==data.pageNumber){
            			$(this).attr("class","turn_cur");
            		}
            	});
			}else{
				$("#hospital-context").append("<tr><td></td><td></td><td></td><td></td><td></td><td>查询结果为空</td><td></td><td></td><td></td><td></td><td></td></tr>");
			}
			
/*			for ( var i = 0; i < data.length; i++) {
				var hospital = data[i];
				$("#hospital-context").append(
						"<tr style=\"cursor:pointer\"><td><span name='icheckbox' onclick='changeCheckBox(this)' is-update=\""+hospital.isEdit+"\" class=\"check_no\" id=\""+getIsNull(hospital.hospitalCode)+ "\"></span></td><td>"+getIsNull(hospital.hospitalName)+"</td><td>"+getIsNull(hospital.headOrBranchCN)+ "</td><td>"+getIsNull(hospital.medicalInsuranceTypeCN)+"</td><td>"+getIsNull(hospital.hospitalLevelString)+ "</td><td>"+getIsNull(hospital.hospitalAttrString)+"</td><td>"+getIsNull(hospital.hospitalEconomicTypeCN)+"</td><td>"+getIsNull(hospital.hosProvinceString)+ "</td><td>"+getIsNull(hospital.hosCityString)+"</td><td>"+getIsNull(hospital.keyDepartment)+"</td><td>"+getIsNull(hospital.isSpecialHospital)+"</td><td>"+getIsNull(hospital.isBlacklist)+"</td></tr>");
			}*/
			
		}
	});
	
}


//复选按钮实现
function changeCheckBox(obj){
	
	if($(obj).is(".check_no")){
		$("#hsptCode").val(obj.id);
	}else{
		$("#hsptCode").val("");
	}
	//初始化机构复选框
	zTree=$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	zTree.expandAll(true);
//	$("#group").html("");
//	displayGroup();
	
	$("#classcode-context").find("tr").each(function(){
		$(this).find("span").removeClass("check_yes").addClass("check_no");
	});
	

	var isUpdate = $(obj).attr("is-update");
	$("#is-update-org-and-group").val(isUpdate);
//	getInsuranceTypeInfo();
	
	var n = $(obj).attr("id");
	if ($(obj).is(".check_no")){
		var s = $("span[name='icheckbox']");
		var isAllChecked = true;
		for(var i in s){
			if(s[i].className=="check_no"){
				if (s[i].id != n){
					isAllChecked = false;
					break;
				}
			}
		}
		$(obj).removeClass("check_no");
		$(obj).addClass("check_yes"); 
		if (isAllChecked) {
			$("#check-all-hospital").removeClass("check_no").addClass("check_yes");
		}
	} else {
		$(obj).removeClass("check_yes");
		$(obj).addClass("check_no"); 
			var s = $("span[name=icheckbox]");
			var isAllChecked = true;
			for(var i in s){
				if(s[i].className=="check_no"){
					if (s[i].id != n){
						isAllChecked = false;
						break;
					}
				}
			}
			if (isAllChecked) {
				$("#check-all-hospital").removeClass("check_yes").addClass("check_no");
			}
			
     hospitalCode = obj.id;
     
     
// 	if($(obj).is(".check_yes")){
//		$("#group span").removeClass("check_yes").addClass("check_no");
//		$("#insuctx").find("tr").each(function(){
//			if($(this).is("check_yes"))
//			$(this).find("span").removeClass("check_yes").addClass("check_no");
//		});
//	}
	  //获取医院信息
//     if(isUpdate!="N"){
//    	 selectGroup();
//    	 $.ajax({
//   		  type:"POST",
//   		  url:"queryHospitalOrgInfo.do",
//   		  data:{"hospitalCode":hospitalCode},
//   		  error:function(){
//   			  alert("获取医院信息失败");
//   			  id=-1;
//   			  return;
//   		  },
//   		  success:function(data){
//   			  zTree=$.fn.zTree.init($("#treeDemo"), setting, zNodes);
//   				zTree.expandAll(true);
//   			  for(key in data){
//   				  id=data[key].objId;
//   				  checkSe(data[key].branchId,data[key].bmdm);
//   			  }
//   		  }
//   	  });
//     }
}
	startCheck();
}

var zTree;
var zNodes;
//获取机构信息
function getOrgInfo() {
	$.ajax({
		type : "POST",
		url : "queryOrgInfo.do",
		data : "",
		error : function() {
			alert("机构信息获取错误");
			return;
		},
		success : function(data) {
			if (data != '') {
				zNodes = eval("(" + data + ")");
				zTree=$.fn.zTree.init($("#treeDemo"), setting, zNodes);
				zTree.expandAll(true);
			}
		}

	});
}

function checkSe(branchId,bmdm){
	var nodes=zTree.getNodesByParamFuzzy("name","",null);
	for(key in nodes){
		if(bmdm!=null){
			if(bmdm==nodes[key].id){
				zTree.selectNode(nodes[key]);
				zTree.checkNode(nodes[key]);
			}
		}
		if(branchId!=null){
			if(branchId==nodes[key].id){
				zTree.selectNode(nodes[key]);
				zTree.checkNode(nodes[key]);
			}
		}
	}
}

var setting = {
		
		view: {
			showTitle: false,
			showIcon : false
			},
			 check: {  
				enable: true,
				chkStyle:"checkbox",
				chkboxType:{ "Y" : "p", "N" : "" },
			},
			data: {
				key: {title:"t"},
				simpleData: {enable: true}
			},
			callback:{
				beforeClick:zTreeBeforeClick,
				onCheck:zTreeOnChange
			}
};
function zTreeBeforeClick(treeId,trrrNode,clickFlag){
	return false;
}
var a;
function zTreeOnChange(event, treeId,treeNode){
	a=treeNode.getParentNode();
//	alert("父节点ID"+a.id+",名字："+a.name);
}

var hospitalCode;
function submitInfo(){
	var flag=false;
	var hospitals=$("span[name='icheckbox']");
	for(var i=0;i<hospitals.length;i++){
		var className=hospitals[i].className;
		if(className=="check_yes"){
			flag=true;
			break;
		}
	}
	if(!flag){
		alert("请选择医院");
		return;
	}
	
	n=zTree.getCheckedNodes(true);
			var param = "";
			for(i in n){
				if(n[i].orgLevel=="2"){
					if(param[param.length-1]==","){
						param=param.substring(0,param.length-1 );
					}else{
						param+=",";
					}
					param += "|"+n[i].id+":";
				}else if(n[i].orgLevel=="3"){
					param += n[i].id+",";
				}
			}
			if(param[param.length-1]==","){
				param=param.substring(0,param.length-1 );
			}else{
				param+=",";
			}
			param=param.substring(2, param.length);

	
	//保存所有勾选信息
	var noSaveHospital = [];         // 不能保存的医院名称
	var hospitalNameList = "";       // 不能保存的医院名称列表,逗号分隔
	var hospitalCodeArray = [];      // 所有已勾选医院的代码
	$("#hospital-context tr span.check_yes").each(function(){
		var isUpdate = $(this).attr("is-update");
		if(isUpdate=="N"){
			noSaveHospital.push($(this).parent().parent().find("td:eq(1)").html());
		}else{
			hospitalCodeArray.push($(this).attr("id"));
		}
	});
	
	for ( var i = 0; i < noSaveHospital.length; i++) {
		hospitalNameList = hospitalNameList + "," + noSaveHospital[i];
	}
	
	var groupNoArray = [];      // 所有已勾选组别信息的代码
	$("#group tr span.check_yes").each(function(){
		groupNoArray.push($(this).attr("id"));
	});
	
	var classCodeArray = [];      // 所有已勾选险种信息的代码
	$("#classcode-context tr span.check_yes").each(function(){
		classCodeArray.push($(this).attr("id"));
	});
	
	if(hospitalCodeArray.length>0){
		$.ajax({
			type:"POST",
			url:"saveHospitalOrgInfo.do",
			data:{"hospitalCodeArray":hospitalCodeArray,"groupNoArray":groupNoArray,"classCodeArray":classCodeArray,"orgAll":param},
			error:function(){
				alert("失败");
				return;
			},
			success:function(data){
				if(hospitalNameList == "" || hospitalNameList == null){
					alert("成功");
				}else{
					alert("成功，其中  "+hospitalNameList+ " 不能保存机构和组别信息");
				}
				
				
			}
		});
	}else{
		alert(hospitalNameList+ " 不能保存机构和组别信息");
	}
}

//若果返回数据位null，则显示为空
function getIsNull(val){
	 if(val==null||val=="null"){
		 return "";
	 }else{
		 return val; 
	 }
	 
 }
 
//回车查询
function enterQuery(){
	 var event = window.event||arguments.callee.caller.arguments[0];
	 if(event.keyCode==13){
		 queryHospitalInfo();
	 }
}

//高级查询
function advancedQuery(f){
	
	var hsptName = $("#hsptName").val();
	var hsptPro = $("#search-select-province").val();
	var hsptCity = $("#search-select-city").val();
	var hsptLevel = $("#search-select-level").val();
	var hsptCap = $("#search-select-property").val();
	var hsptAttr = $("#search-select-attr").val();
	var hsptImpoDep = $("#hsptImpoDep").val();
	var brchCom = $("#search-select-branch").val();
	var bmdmCom = $("#search-select-bmdm").val();
	var insuType = $("#insuType").val();
	var hospitalEconomicType = $("#search-hospital-economic-type").val();
	var groupName = $("#keyword-group-name").val();
	
	$.ajax({
		type:"POST",
		url:"advancedQuery.do",
		data:{"pageNumber":f,"groupName":groupName,"hsptName":hsptName,"hsptPro":hsptPro,"hsptCity":hsptCity,"hsptLevel":hsptLevel,"hsptCap":hsptCap,"hsptAttr":hsptAttr,"hsptImpoDep":hsptImpoDep,"brchCom":brchCom,"bmdmCom":bmdmCom,"insuType":insuType,"hospitalEconomicType":hospitalEconomicType},
		error:function(){
			alert("查询出错");
			return;
		},
		success:function(data){
			$("#hospital-context").empty();
			var designatedHospitalVoList = data.designatedHospitalVoList;
			if(designatedHospitalVoList != null && designatedHospitalVoList != ""){
				$("#hospital-total").val(data.total);
				$("#hospital-currentPage").val(data.pageNumber);
				
				$("#hospital-nav").empty();
				$("#hospital-context").empty();
				$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"pre(this,'advanced')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&lt;</span></a>");
				
				
				for ( var i = 0; i < designatedHospitalVoList.length; i++) {
					var hospital = designatedHospitalVoList[i];
					$("#hospital-context").append("<tr style=\"cursor:pointer\"><td><span name='icheckbox' onclick='changeCheckBox(this)' is-update=\""+hospital.isEdit+"\" class=\"check_no\" id=\""+getIsNull(hospital.hospitalCode)+ "\"></span></td><td>"+getIsNull(hospital.hospitalName)+"</td><td>"+getIsNull(hospital.headOrBranchCN)+ "</td><td>"+getIsNull(hospital.medicalInsuranceTypeCN)+"</td><td>"+getIsNull(hospital.hospitalLevelString)+ "</td><td>"+getIsNull(hospital.hospitalAttrString)+"</td><td>"+getIsNull(hospital.hospitalEconomicTypeCN)+"</td><td>"+getIsNull(hospital.hosProvinceString)+ "</td><td>"+getIsNull(hospital.hosCityString)+"</td><td>"+getIsNull(hospital.keyDepartment)+"</td><td>"+getIsNull(hospital.isSpecialHospital)+"</td><td>"+getIsNull(hospital.isBlacklist)+"</td></tr>");
				}
				/*for ( var i = 1; i <= data.total; i++) {
					$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'advanced')\" style=\"cursor:pointer;\"><span>"+i+"</span></a>");
				}*/
				var j = 1;
				for( var i = data.pageNumber ; i <= data.total; i++){
					if(j <= data.pageNumber+3){
 						$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'advanced')\" style=\"cursor:pointer;\"><span>"+i+"</span></a>");
					}else{
						break;
					}
					j++;
				}
				
				if(data.total > data.pageNumber+3 ){
					$("#hospital-nav").append("<span>...</span>");
				}
				
				if(data.total == data.pageNumber+7 ){
						$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'advanced')\" style=\"cursor:pointer;\"><span>"+data.total+"</span></a>");
				}
				
				if(data.total > data.pageNumber+7 ){
					for ( var k = data.total-2 ; k <= data.total; k++) {
						$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'advanced')\" style=\"cursor:pointer;\"><span>"+k+"</span></a>");
				}
			}
			
				
				$("#hospital-nav").append("<a href=\"javascript:void(0);\" onclick=\"next(this,'advanced')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&gt;</span></a>");
				
                $("#hospital-nav .turn_cur").removeAttr("class");
            	$("#hospital-nav").find("span").each(function(){
            		if($(this).html()==data.pageNumber){
            			$(this).attr("class","turn_cur");
            		}
            	});
			}else{
				$("#hospital-context").append("<tr><td></td><td></td><td></td><td></td><td></td><td>查询结果为空</td><td></td><td></td><td></td><td></td><td></td></tr>");
			}
			
			
			
			
/*			if(data==""||data==null){
				$("#hospital-context").append("<tr><td>没有相关内容</td></tr>");
			}
			for ( var i = 0; i < data.length; i++) {
				var hospital = data[i];
				$("#hospital-context").append(
						"<tr style=\"cursor:pointer\"><td><span name='icheckbox' onclick='changeCheckBox(this)' is-update=\""+hospital.isEdit+"\" class=\"check_no\" id=\""+getIsNull(hospital.hospitalCode)+ "\"></span></td><td>"+getIsNull(hospital.hospitalName)+"</td><td>"+getIsNull(hospital.headOrBranchCN)+ "</td><td>"+getIsNull(hospital.medicalInsuranceTypeCN)+"</td><td>"+getIsNull(hospital.hospitalLevelString)+ "</td><td>"+getIsNull(hospital.hospitalAttrString)+"</td><td>"+getIsNull(hospital.hospitalEconomicTypeCN)+"</td><td>"+getIsNull(hospital.hosProvinceString)+ "</td><td>"+getIsNull(hospital.hosCityString)+"</td><td>"+getIsNull(hospital.keyDepartment)+"</td><td>"+getIsNull(hospital.isSpecialHospital)+"</td><td>"+getIsNull(hospital.isBlacklist)+"</td></tr>");
			}*/
			
			
/*			$("#hospital-context").empty();
			if(data==""||data==null){
				$("#hospital-context").append("<tr><td>没有相关内容</td></tr>");
			}
			for ( var i = 0; i < data.length; i++) {
				var hospital = data[i];
				$("#hospital-context").append("<tr style=\"cursor:pointer\"><td><span name='icheckbox' onclick='changeCheckBox(this)' class=\"check_no\" id=\""+getIsNull(hospital.hospitalCode)+ "\"></span></td><td>"+getIsNull(hospital.hospitalName)+"</td><td>"+getIsNull(hospital.hosProvinceString)+ "</td><td>"+getIsNull(hospital.hosCityString)+"</td><td>"+getIsNull(hospital.hosAddress)+ "</td><td>"+getIsNull(hospital.hosContactPhone)+"</td><td>"+getIsNull(hospital.hosPostcode)+"</td><td>"+getIsNull(hospital.hospitalLevelString)+ "</td><td>"+getIsNull(hospital.hospitalAttrString)+"</td><td>"+getIsNull(hospital.hospitalCapitalString)+"</td><td>"+getIsNull(hospital.keyDepartment)+"</td></tr>");
			}*/
		}
	});
}

//查询险种信息
function getInsuranceTypeInfo(){
	var hospitalCode = $("#hsptCode").val();
	if(hospitalCode==""||hospitalCode==null) return;
	if(hospitalCode!=null&&hospitalCode!=""&&hospitalCode!=undefined){
	$.ajax({
		type:"POST",
		url:"getInsuranceTypeInfo.do",
		data:{"hospitalCode":hospitalCode},
		error:function(){
			alert("获取险种信息失败");
		},
		success:function(data){
			if(data.length>0){
				for(var i=0;i<data.length;i++){
					$("#"+data[i].classCode).attr("class","check_yes");
				}
			}else{
				$("#classcode-context").append("<td></td><td></td><td></td>");
			}
		}
	});
	}
}

//获取所有险种类型
/*function displayAllInsuranceType(){
	$("#insuctx").html("");
	$.ajax({
		type:"POST",
		url:"getAllInsuranceType.do",
		error:function(){
			alert("获取险种信息失败");
		},
		success:function(data){
			if(data.length>0){
				for(var i=0;i<data.length;i++){
					$("#insuctx").append("<tr><td><span class=\"check_no\" id=\""+data[i].classcode+"\"  onclick=\"checkIsAndNo(this)\"></span></td><td>"+data[i].classcode+"</td><td>"+data[i].classname+"</td></tr>");
				}
			}
		}
	});
}*/

//获取勾选的险种类型
function selectInsuranceType(e){
	$("selected-class-code").val($(e).attr("data-class-code").val());
}


//保存关联险种信息
function saveInsuranceType(){
	var hospitalCode = $("#hsptCode").val();
	var classCodeArray = [];
	$("#insuctx").find("tr").each(function(){
		if($(this).find("span").is(".check_yes")){
			var classCode = $(this).find("span:eq(0)").attr("id");
			classCodeArray.push(classCode);
		}
	});
	
	$.ajax({
		type:"POST",
		url:"saveInsuranceType.do",
		data:{"classCodeArray":classCodeArray,"hospitalCode":hospitalCode},
		error:function(){
			alert("保存险种信息失败");
		},
		success:function(data){
			alert("保存险种信息成功!");
		}
	});
}

//全选医院
function checkAllHospital(n) {
	var s=$("span[name=icheckbox]");
	if ($("#check-all-hospital").is(".check_yes")) {
		for(var i in s){
			if(s[i].className=="check_yes"){
				s[i].className="check_no";
			}
		}
		
		$("#group .check_yes").removeClass("check_yes").addClass("check_no");
		$("#insuctx .check_yes").removeClass("check_yes").addClass("check_no");
	}else{
		for(var i in s){
			if(s[i].className=="check_no"){
				s[i].className="check_yes";
			}
		}
		//勾选组别、险种、机构
		startCheck();
	}
	
}

//勾选组别、险种、机构
function startCheck(){

	var hositalCodeArray = [];      // 所有已勾选医院的代码
	$("#hospital-context tr span.check_yes").each(function(){
		hositalCodeArray.push($(this).attr("id"));
	});
	
	//如果没勾选医院，就不查
	if(hositalCodeArray.length == 0) return;
	
	$.ajax({
		type:"POST",
		url:"queryHospitalCheck.do",
		async:true,
		data:{"hositalCodeList":hositalCodeArray},
		error:function(){
			alert("获取已选择信息失败");
		},
		success:function(data){
			//开始勾选组别、险种、机构
			var insuranceTypeCheckVoList = data.insuranceTypeCheckVoList;
			var groupCheckVoList = data.groupCheckVoList;
			var hospitalOrgRlList = data.hospitalOrgRlList;
			if(insuranceTypeCheckVoList!=null){
				for ( var i = 0; i < insuranceTypeCheckVoList.length; i++) {
					$("#"+insuranceTypeCheckVoList[i].insuranceCode).removeClass("check_no").addClass("check_yes");
				}
			}

			if(groupCheckVoList!=null){
				for ( var i = 0; i < groupCheckVoList.length; i++) {
					$("#"+groupCheckVoList[i].groupId).removeClass("check_no").addClass("check_yes");
				}
			}

			for ( var i = 0; i < hospitalOrgRlList.length; i++) {
				var branchId = hospitalOrgRlList[i].branchId;
				var bmdm = hospitalOrgRlList[i].bmdm;
				checkSe(branchId,bmdm);
			}
			
		}
	});
}

//根据险种名称查询险种信息
function searchByInsuName(f){
	var className = $("#class-name-keyword").val();
	$("#insuctx").html("");
	$.ajax({
		type:"POST",
		url:"searchByInsuranceName.do",
		data:{"className":className,"pageNumber":f},
		error:function(){
			alert("获取险种信息失败");
		},
		success:function(data){

			if(data != null && data != ""){
				$("#classcode-total").val(data.total);
				$("#classcode-currentPage").val(data.pageNumber);
				
				$("#classcode-nav").empty();
				$("#classcode-context").empty();
				$("#classcode-nav").append("<a href=\"javascript:void(0);\" onclick=\"pre(this,'searchByInsuranceName')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&lt;</span></a>");
				
				var classcodeList = data.classcodeList;
				for ( var i = 0; i < classcodeList.length; i++) {
					var classcodeTable = classcodeList[i];
					$("#classcode-context").append("<tr><td><span class=\"check_no\" id=\""+classcodeTable.classcode+"\"  onclick=\"checkIsAndNo(this)\"></span></td><td>"+classcodeTable.classcode+"</td><td>"+classcodeTable.classname+"</td></tr>");
				}
				for ( var i = 1; i <= data.total; i++) {
					$("#classcode-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'searchByInsuranceName')\" style=\"cursor:pointer;\"><span>"+i+"</span></a>");
				}
				$("#classcode-nav").append("<a href=\"javascript:void(0);\" onclick=\"next(this,'searchByInsuranceName')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&gt;</span></a>");
				
                $("#classcode-nav .turn_cur").removeAttr("class");
            	$("#classcode-nav").find("span").each(function(){
            		if($(this).html()==data.pageNumber){
            			$(this).attr("class","turn_cur");
            		}
            	});
			}
			
			
/*			if(data.length>0){
				for(var i=0;i<data.length;i++){
					$("#insuctx").append("<tr><td><span class=\"check_no\" id=\""+data[i].classcode+"\" onclick=\"checkIsAndNo(this)\"></span></td><td>"+data[i].classcode+"</td><td>"+data[i].classname+"</td></tr>");
				}
			}else{
				$("#insuctx").append("<tr><td></td><td></td>查无信息<td></td></tr>");
			}*/

		}
	});
}

function selectGroup(){
	var hospitalCode = $("#hsptCode").val();
	$.ajax({
		type:"POST",
		url:"getSelectedGroup.do",
		data:{"hospitalCode":hospitalCode},
		error:function(){
			alert("获取信息失败");
		},
		success:function(data){
			if(data.length>0){
				for ( var i = 0; i < data.length; i++) {
					$("#"+data[i].groupNo).removeClass("check_no").addClass("check_yes");
				}
			}
		}
	});
}

function saveGroup(){
	var groupNoArray = [];
	$("#group").find("span").each(function(){
		if($(this).is(".check_yes")){
			var groupNo = $(this).attr("id");
			groupNoArray.push(groupNo);
		}
	});
	
	var hospitalCode = $("#hsptCode").val();
	$.ajax({
		type:"POST",
		url:"saveHospitalGroup.do",
		data:{"hospitalCode":hospitalCode,"groupNoArray":groupNoArray},
		error:function(){
			alert("保存组别信息失败");
		},
		success:function(data){
			alert("保存组别信息成功");
		}
	});
}

//
function displayGroup(){
	
	$("#group").empty();
	displayGroupInfo("1");
/*	$.ajax({
		type:"POST",
		url:"getAllGroup.do",
		data:{},
		error:function(){
			alert("获取组别信息失败");
		},
		success:function(data){
			$("#group").empty();
			if(data.length>0){
				for(var i=0;i<data.length;i++){
					$("#group").append("<tr><td><span class=\"check_no\" id=\""+data[i].groupNo+"\" onclick=\"checkIsAndNo(this)\"></span></td><td>"+data[i].groupNo+"</td><td>"+data[i].groupName+"</td><td>&nbsp&nbsp&nbsp</td></tr>");
				}
			}
		}
	});*/
	var hospitalCodeArray = [];
	var hospitalCode = $("#hsptCode").val();
	hospitalCodeArray.push(hospitalCode);
	$.ajax({
		type:"POST",
		url:"getSelectedGroup.do",
		data:{"hospitalCodeArray":hospitalCodeArray},
		error:function(){
			alert("获取已勾选组别信息失败");
		},
		success:function(data){
			if(data.length>0){
				for ( var i = 0; i < data.length; i++) {
					$("#"+data[i].groupNo).removeClass("check_no").addClass("check_yes");
				}
			}
		}
	});
}

function selpro(e){
	$("#search-select-province").val($(e).attr("scode"));
	
	findCity($(e).attr("scode"));
	
	
}

//根据所选省份获取市
function findCity(provinceCode){
	 $("#citySpan").html("请选择市");
	 $("#cityContext").html("");
	 $.ajax({
		 
		 type:"POST",
		 url:"findCity.do",
		 data:{"provinceCode":provinceCode},
		 error:function(){
			 alert("获取市级单位失败");
			 return;
		 },
		 success:function(data){
			 $("#display-city").html("请选择");
			 $("#city-context").empty();
			 for(var i=0;i<data.length;i++){
				 var city = data[i];
				 
				 $("#city-context").append("<li id=\""+city.cityCode+"\" onclick=\"getCityCode(this)\">"+city.cityName+"</li>");
			 }
			 //选中
			 $(".select").find(".sel_list li").hover( function (){
					$(this).addClass("sel_hov");		
				},function (){
					$(this).removeClass("sel_hov");	
				}).on('click',function(){
					var _this = $(this).html();
					
					var _thisText = $(this).closest(".select").find(".sel_text");
					_thisText.html(_this);
					
				});
		 }
		 
	 });
}


function getCityCode(obj){
	 $("#search-select-city").val(obj.id);
}


//显示高级查询省份列表
function displayProvinceList(){
	 $.ajax({
		 type:"POST",
		 url:"getProvince.do",
		 data:"",
		 error:function(){
			 alert("获取省份失败");
			 return;
		 },
		 success:function(data){
			 for(var i=0;i<data.length;i++){
				 var province = data[i];
				 $("#province-list").append("<li id="+province.cityCode+" onclick=\"selpro(this)\">"+province.cityName+"</li>");
			 }
		 }
	 });
}

function displaybmdm(obj){
	var orgcode = $(obj).attr("scode");
	$("#search-select-branch").val($(obj).attr("scode"));//fengongsidaima
	$("#bmdm-list").empty();
	$("#bmdm-list").html("请选择中支公司");
	$.ajax({
		type:"POST",
		url:"queryOrg.do",
		data:{"orgCode":orgcode},
		error:function(){
			alert("获取中支公司失败");		
		},
		success:function(data){
			for(var i=0;i<data.length;i++){
				$("#bmdm-list").append("<li scode=\""+data[i].code+"\" onclick=\"fillInput(this)\">"+data[i].name+"</li>");
			}
			 $(".select").find(".sel_list li").hover( function (){
					$(this).addClass("sel_hov");		
				},function (){
					$(this).removeClass("sel_hov");	
				}).on('click',function(){
					var _this = $(this).html();
					var _thisText = $(this).closest(".select").find(".sel_text");
					_thisText.html(_this);
				});
		}
	});
}

function checkIsAndNo(e){
	if($(e).is(".check_yes")){
		$(e).attr("class","check_no");
	}else {
		$(e).attr("class","check_yes");
	}
}


function fillInput(e){
	$("#search-select-bmdm").val($(e).attr("scode"));
}

function selLevel(e){
	$("#search-select-level").val($(e).attr("scode"));
}

function selProperty(e){
	$("#search-select-property").val($(e).attr("scode"));
}

function selectedHospitalEconomicType(e){
	$("#search-hospital-economic-type").val($(e).attr("scode"));
}

function selAttr(e){
	$("#search-select-attr").val($(e).attr("scode"));
}


//组别信息
function displayGroupInfo(f){
	
	
	$.ajax({
		type:"POST",
		url:"getAllGroup.do",
		data:{"pageNumber":f},
		error:function(){
			alert("获取组别信息失败");		
		},
		success:function(data){

			if(data != null && data != ""){
				$("#group-total").val(data.total);
				$("#group-currentPage").val(data.pageNumber);
				
				$("#group-nav").empty();
				$("#group").empty();
				$("#group-nav").append("<a href=\"javascript:void(0);\" onclick=\"pre(this,'group')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&lt;</span></a>");

				var groupList = data.groupList;
				for ( var i = 0; i < groupList.length; i++) {
					var group = groupList[i];
					$("#group").append("<tr><td><span class=\"check_no\" id=\""+group.groupNo+"\" onclick=\"checkIsAndNo(this)\"></span></td><td>"+group.groupNo+"</td><td>"+group.groupName+"</td><td>"+group.groupMemo+"</td></tr>");
				}
				for ( var i = 1; i <= data.total; i++) {
					$("#group-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'group')\" style=\"cursor:pointer;\"><span>"+i+"</span></a>");
				}
				$("#group-nav").append("<a href=\"javascript:void(0);\" onclick=\"next(this,'group')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&gt;</span></a>");
				
                $("#group-nav .turn_cur").removeAttr("class");
            	$("#group-nav").find("span").each(function(){
            		if($(this).html()==data.pageNumber){
            			$(this).attr("class","turn_cur");
            		}
            	});
			}
			
		}
	});
	
}



//险种信息
function displayClasscodeTable(f){
	$.ajax({
		type:"POST",
		url:"getAllInsuranceType.do",
		data:{"pageNumber":f},
		error:function(){
			alert("获取险种信息失败");		
		},
		success:function(data){

			if(data != null && data != ""){
				$("#classcode-total").val(data.total);
				$("#classcode-currentPage").val(data.pageNumber);
				
				$("#classcode-nav").empty();
				$("#classcode-context").empty();
				$("#classcode-nav").append("<a href=\"javascript:void(0);\" onclick=\"pre(this,'classcode')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&lt;</span></a>");
				
				var classcodeList = data.classcodeList;
				for ( var i = 0; i < classcodeList.length; i++) {
					var classcodeTable = classcodeList[i];
					$("#classcode-context").append("<tr><td><span class=\"check_no\" id=\""+classcodeTable.classcode+"\"  onclick=\"checkIsAndNo(this)\"></span></td><td>"+classcodeTable.classcode+"</td><td>"+classcodeTable.classname+"</td></tr>");
				}
				for ( var i = 1; i <= data.total; i++) {
					$("#classcode-nav").append("<a href=\"javascript:void(0);\" onclick=\"cur_skip(this,'classcode')\" style=\"cursor:pointer;\"><span>"+i+"</span></a>");
				}
				$("#classcode-nav").append("<a href=\"javascript:void(0);\" onclick=\"next(this,'classcode')\" style=\"cursor:pointer;\"><span class=\"turn_btn\">&gt;</span></a>");
				
                $("#classcode-nav .turn_cur").removeAttr("class");
            	$("#classcode-nav").find("span").each(function(){
            		if($(this).html()==data.pageNumber){
            			$(this).attr("class","turn_cur");
            		}
            	});
			}
			
		}
	});
}


//翻页
function cur_skip(e,func){
	var pn = $(e).find("span").html();
	if(func == "group"){
		displayGroupInfo(pn);
		$("#group-nav .turn_cur").removeAttr("class");
	}else if(func == "classcode"){
		displayClasscodeTable(pn);
		$("#classcode-nav .turn_cur").removeAttr("class");
	}else if(func == "hospital"){
		queryHospitalInfo(pn);
		$("#hospital-nav .turn_cur").removeAttr("class");
	}else if(func == "advanced"){
		advancedQuery(pn);
	   $("#hospital-nav .turn_cur").removeAttr("class");
    }else if(func == "searchByInsuranceName"){
		searchByInsuName(pn);
		$("#classcode-nav .turn_cur").removeAttr("class");
	}
	$(e).find("span").attr("class","turn_cur");
	//翻页之后重新勾选
	startCheck();
}

//前一页
function pre(e,func){
	var funcTemp = func;
	if(func == "advanced") func = "hospital";
	var cur = $("#"+func+"-nav").find(".turn_cur").html();
//	var next = Number(cur)+Number(1);
	var maxPage = $("#"+func+"-total").val();
	if(funcTemp == "advanced") func = "advanced";
	if(cur>1){
		var pre = Number(cur)-Number(1);
		if(func == "group"){
			displayGroupInfo(pre);
		}else if(func == "classcode"){
			displayClasscodeTable(pre);
		}else if(func == "hospital"){
			queryHospitalInfo(pre);
		}else if(func == "advanced"){
			advancedQuery(pre);
		}else if(func == "searchByInsuranceName"){
			searchByInsuName(pre);
		}
	}else{
		if(func == "group"){
			displayGroupInfo(maxPage);
		}else if(func == "classcode"){
			displayClasscodeTable(maxPage);
		}else if(func == "hospital"){
			queryHospitalInfo(maxPage);
		}else if(func == "advanced"){
			advancedQuery(maxPage);
		}else if(func == "searchByInsuranceName"){
			searchByInsuName(maxPage);
		}
	}
	
	//翻页之后重新勾选
	startCheck();
}
//下一页
function next(e,func){
	var funcTemp = func;
	if(func == "advanced") func = "hospital";
	var cur = $("#"+func+"-nav").find(".turn_cur").html();
	var next = Number(cur)+Number(1);
	var maxPage = $("#"+func+"-total").val();
	if(funcTemp == "advanced") func = "advanced";
	if(parseInt(cur)<parseInt(maxPage)){
		if(func == "group"){
			displayGroupInfo(next);
		}else if(func == "classcode"){
			displayClasscodeTable(next);
		}else if(func == "hospital"){
			queryHospitalInfo(next);
		}else if(func == "advanced"){
			advancedQuery(next);
		}else if(func == "searchByInsuranceName"){
			searchByInsuName(next);
		}
	}else{
		if(func == "group"){
			displayGroupInfo("1");
		}else if(func == "classcode"){
			displayClasscodeTable("1");
		}else if(func == "hospital"){
			queryHospitalInfo("1");
		}else if(func == "advanced"){
			advancedQuery("1");
		}else if(func == "searchByInsuranceName"){
			searchByInsuName("1");
		}
	}
	
	//翻页之后重新勾选
	startCheck();
}
//跳转至
function custom(func){
	var pn = $.trim($("#turn_inp").val());
	if(pn == "") return;
	if(func == "group"){
		displayGroupInfo(pn);
	}else if(func == "classcode"){
		displayClasscodeTable(pn);
	}
	
	//翻页之后重新勾选
	startCheck();
}

function getAllSelectedGroupCodes(){
	var groupNoArray = [];      // 所有已勾选组别信息的代码
	$("#group tr span.check_yes").each(function(){
		groupNoArray.push($(this).attr("id"));
	});
	return groupNoArray;
}

function findInsuranceTypeList(){
	
	var insuranceType = $("#insuType").val(); // 险种名称
	
	$.ajax({
		type : "POST",
		url : "schinsu.do",
		data : {"className" : insuranceType},
		error :function(){
			alert("获取险种名称列表失败");
		},
		success:function(data){
			var ul=$("<ul id=\"insuType-tip\"></ul>");
			if(data.length>0){
				for ( var i = 0; i < data.length; i++) {
					$(ul).append("<li onmouseover=\"changbackclrFn(this)\" onblur=\"insuTypeDisplayNone()\" onclick=\"selectedInsuTypeFn('"+data[i].classname+"')\" title="+data[i].classname+">"+data[i].classname+"</li>");
				}
				$("#insuType-tip-div").html(ul);
				$("#insuType-tip-div").css("display","");
			}else{
				$("#insuType-tip-div").css("display","none");
			}
		}
	});
}

function insuTypeDisplayNone(){
	$("#insuType-tip").css("display","none");
}

function groupNameDisplayNone(){
	$("#group-name-tip").css("display","none");
}

function changbackclrFn(flag){
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
}


//添加信息到文本框
function selectedInsuTypeFn(flag){
	$("#insuType").val(flag);
	$("#insuType").attr("title",flag);
	$("#insuType-tip-div").css("display","none");
}



//添加信息到文本框
function selectedCompanyInfoFn(flag){
	$("#keyword-group-name").val(flag);
	$("#group-name-tip-div").css("display","none");
}

function findGroupNameList(){
	var groupName = $("#keyword-group-name").val(); // 险种名称
	
	$.ajax({
		type : "POST",
		url : "findGroupInfoList.do",
		data : {"groupName" : groupName},
		error :function(){
			alert("获取组别名称列表失败");
		},
		success:function(data){
			var ul=$("<ul id=\"group-name-tip\"></ul>");
			if(data.length>0){
				for ( var i = 0; i < data.length; i++) {
					$(ul).append("<li onmouseover=\"changbackclrFn(this)\" onblur=\"groupNameDisplayNone()\" onclick=\"selectedGroupInfoFn('"+data[i].groupName+"')\" title="+data[i].groupName+" title=\""+data[i].groupName+"\">"+data[i].groupName+"</li>");
				}
				$("#group-name-tip-div").html(ul);
				$("#group-name-tip-div").css("display","");
			}else{
				$("#group-name-tip-div").css("display","none");
			}
		}
	});
}

function selectedGroupInfoFn(flag){
	$("#keyword-group-name").val(flag);
	$("#group-name-tip-div").css("display","none");
}
