var companyNoForSave = "";


$(function(){
	
	comSelected();
	$(".property-branch").on("click",function(){
		var branchId = $(this).attr("data-pro-code");
		$("#bmdm").empty();
		$.ajax({
			type:"POST",
			url:"getPropertyBmdm.do",
			data:{"branchId":branchId},
			error:function(){
				alert("获取产险部门组失败");
			},
			success:function(data){
				if(data.length>0){
					for ( var i = 0; i < data.length; i++) {
						var bmdm = data[i];
						$("#bmdm").append("<li id="+bmdm.branchId+" onclick=\"chooseBmdmCompany(this)\" >"+bmdm.branchName+"</li>");
					}
				}
			}
		});
		
		
	});
});
//查询收单单位
function queryUnit(){
	var unitName = $("#unitName").val();
	var settingStart = $("#settingStart").val();
	var settingEnd = $("#settingEnd").val();
	var company = $("#selectCompany").attr("code");
	var branch = $("#selectBranch").attr("code");
	var bmdm = $("#selectBmdm").attr("code");
	
	$("#unitNameExcel").val(unitName);
	$("#settingStartExcel").val(settingStart);
	$("#settingEndExcel").val(settingEnd);
	$("#companyExcel").val(company);
	$("#branchExcel").val(branch);
	$("#bmdmExcel").val(bmdm);
	if(settingStart>settingEnd){
		alert("开始日期不能大于结束日期");
		return;
	}
	if(settingStart==""||settingStart==null||settingStart==undefined){
		if(settingEnd!=null&&settingEnd!=""&&settingEnd!=undefined){
			alert("请输入开始日期");
			return;
		}
	}
	if(settingEnd==""||settingEnd==null||settingEnd==undefined){
		if(settingStart!=null&&settingStart!=""&&settingStart!=undefined){
			alert("请输入结束日期");
			return;
		}
	}
	
	var parameters={"unitName":unitName,"settingStart":settingStart,
			"settingEnd":settingEnd,"company":company,"branch":branch,"bmdm":bmdm};
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryUnit.do",
		data:parameters,
		error:function(){
			alert("查询失败");
		},
		success:function(data){
			if(data.length>0){
				$("#receiveUnitList").html("");
				$("#companyList").html("");
				for(var i=0;i<data.length;i++){
				var bpoCompany = data[i];
				$("#receiveUnitList").append("<tr id=\""+bpoCompany.objId+"\" onclick=\"selectRow(this)\" ><td>"+(Number(i)+1)+"</td>" +
						"<td><input type=\"text\" value=\""+bpoCompany.showSetDate+"\" class=\"input_time i_s_100 setDate\" id=\"\" onclick=\"WdatePicker()\" />" +
								"</td><td><input type=\"text\" value=\""+bpoCompany.companyName+"\" class=\"input i_s_137 companyName\" id=\"\" autocomplete=\"off\" onkeydown=\"uTipSearch(this)\" onkeyup=\"uTipSearch(this)\" id=\"\" onfocus=\"bundleTip(this);\"/>" +
								"</td><td><input type=\"text\" value=\""+bpoCompany.showEffectdate+"\" class=\"input_time i_s_100 effectdate\" id=\"\" onclick=\"WdatePicker()\" />" +
								"</td><td><input type=\"text\" class=\"input_time i_s_100 quitadate\" id=\"\" value=\""+bpoCompany.showQuitadate+"\" onclick=\"WdatePicker()\" /></td>" +
							"<td><div class=\"select i_s_60 mar_cen\"> <span class=\"sel_text cycles\" id=\"\" data-code=\""+bpoCompany.cycles+"\" >"+bpoCompany.showCycles+"</span>" +
							"<div class=\"sel_list i_ul_85\" style=\"display:none;\">" +
							"<ul><li>每一周</li><li>每两周</li><li>每月</li><li>每两月</li>" +
							"<li>每季度</li><li>每半年</li><li>每年</li></ul></div></div></td><td><div class=\"select i_s_60 mar_cen\"><span class=\"sel_text isTpa\" id=\"\" data-code=\""+(bpoCompany.isTpa == null ? "" : bpoCompany.isTpa)+"\" >"+isTpaNotNull(bpoCompany.isTpaCN)+"</span><div class=\"sel_list i_ul_85\" style=\"display:none;\"><ul><li code=\"\" onclick=\"getAndSetIsTpa(this)\">请选择</li><li code=\"Y\" onclick=\"getAndSetIsTpa(this)\">是</li><li code=\"N\" onclick=\"getAndSetIsTpa(this)\">否</li></ul></div></div></td>"+
							"<td><div class=\"select i_s_60 mar_cen\"><span class=\"sel_text businesstype\" id=\"\" data-code=\""+(bpoCompany.businesstype == null ? "" : bpoCompany.businesstype)+"\" >"+isTpaNotNull(bpoCompany.businesstypeCN)+"</span><div class=\"sel_list i_ul_85\" style=\"display:none;\"><ul><li code=\"0\" onclick=\"getAndSetbusinesstype(this)\">半外包</li><li code=\"1\" onclick=\"getAndSetbusinesstype(this)\">全外包</li><li code=\"2\" onclick=\"getAndSetbusinesstype(this)\">小额理赔</li></ul></div></div></td>"+
							"<td id="+bpoCompany.companyNo+" onclick=\"displayCompany(this)\" title=\"点击编码，再录入公司信息\" style=\"cursor:pointer;\" class=\"hover-under-line\" >"+bpoCompany.companyNo+"</td></tr>");
			}
				selected();
			}else{
				$("#receiveUnitList").empty();
				$("#receiveUnitList").append("<tr class=\"chawuxinxi\" ><td>查无信息</td><tr>");
			}
		}
	});
	
}

// 如果为null,页面不显示"null"
function isTpaNotNull(a){
	return ( a == null || a == "" ) ? "请选择" : a ;
}

// 选择是否Tpa
function getAndSetIsTpa(o){
	$(o).parent().parent().parent().children(".isTpa:eq(0)").attr("data-code",$(o).attr("code"));
}
function getAndSetbusinesstype(o){
	$(o).parent().parent().parent().children(".businesstype:eq(0)").attr("data-code",$(o).attr("code"));
}
/*function getAndSetCycles(o){
		$(o).parents(".cycles:eq(0)").attr("data-code",$(o).attr("code"));
}
*/
//展示公司详情
function displayCompany(tdElement){
	$("#companyList").html("");
	var comNo = tdElement.id;
	companyNoForSave = comNo;
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryCompany.do",
		data:{"companyNo":comNo},
		error:function(){
			alert("获取公司信息失败");
		},
		success:function(data){
			if(data.length>0){
				
				for(var i=0;i<data.length;i++){
				var bpoCompanyOrgRl = data[i];
				if(bpoCompanyOrgRl.objId==80000000000000){
					
				}
				$("#companyList").append("<tr onclick=\"selectComRow(this)\" id=\""+bpoCompanyOrgRl.objId+"\" >" +
												"<td>"+(Number(i)+1)+"</td>" +
												"<td>" +
													"<div class=\"select i_s_137 mar_cen\"><span id=\"ssgs_"+bpoCompanyOrgRl.objId+"\" class=\"sel_text\" data-code=\""+bpoCompanyOrgRl.orgId+"\">"+noNull(bpoCompanyOrgRl.orgString)+"</span>" +
													"<div class=\"sel_list i_ul_162\" style=\"display:none;\">" +
															"<ul id=\""+bpoCompanyOrgRl.objId+"\" >" +
																"<li id=\"80000000000000\" onclick=\"chooseCompanyForSerach(this)\">健康险</li>" +
																"<li id=\"00000000000000\" onclick=\"chooseCompanyForSerach(this)\">寿险</li>" +
																"<li id=\"0000000\" onclick=\"chooseCompanyForSerach(this)\">产险</li>" +
															"</ul>" +
													"</div>" +
													"</div>" +
												"</td>" +
												"<td>" +
													"<div class=\"select i_s_240 mar_cen\">" +
													"<span id=\"branchspan_"+bpoCompanyOrgRl.objId+"\" class=\"sel_text\" data-code=\""+bpoCompanyOrgRl.branchId+"\">"+noNull(bpoCompanyOrgRl.branchString)+"</span>" +
															"<div class=\"sel_list i_ul_265\" style=\"display:none; height: 300px; overflow: scroll;\">" +
															"<ul id=\"fgs"+bpoCompanyOrgRl.objId+"\">" +
																	"</ul>" +
																	"</div></div>" +
																	"</td><td>" +
																	"<div class=\"select i_s_240 mar_cen\">" +
																	"<span id=\"bmdmspan_"+bpoCompanyOrgRl.objId+"\" class=\"sel_text\" data-code=\""+bpoCompanyOrgRl.bmdm+"\">"+noNull(bpoCompanyOrgRl.bmdmString)+"</span>" +
														"<div class=\"sel_list i_ul_265\" style=\"display:none; height: 300px; overflow: scroll;\"><ul id=\"zgs"+bpoCompanyOrgRl.objId+"\"></ul></div></div></td></tr>");
				}
				selected();
			}
		}
	});
}

function noNull(a){
	return a == null ? "" : a;
}

//保存单位
function saveUnit(){
	
	var boo = false;
	
	var bpoComVoList = [];
	
	$("#receiveUnitList").find("tr").each(function(){
		var bpoComVo = {};
		bpoComVo.id=$(this).attr("id");
		bpoComVo.setDate = $(this).find(".setDate").val();
		bpoComVo.companyName =  $(this).find(".companyName").val();
		bpoComVo.effectdate =  $(this).find(".effectdate").val();
		bpoComVo.quitadate =  $(this).find(".quitadate").val();
		bpoComVo.cycles =  $(this).find(".cycles").html();
		var isTpa = $(this).find(".isTpa").attr("data-code");
		bpoComVo.isTpa = ( isTpa != null && isTpa != "" ) ? isTpa : "" ;//isTpa
		var businesstype = $(this).find(".businesstype").attr("data-code");
		bpoComVo.businesstype = ( businesstype != null && businesstype != "" ) ? businesstype : "" ;
		bpoComVoList.push(bpoComVo);
		
		if(bpoComVo.effectdate>bpoComVo.quitadate){
			boo = true;
			alert("收单起始日期应该小于收单截止日期");
			return false;
		}
		
		if(bpoComVo.setDate==""||bpoComVo.setDate==null||bpoComVo.setDate==undefined){
			boo = true;
			alert("请输入设置日期");
			return false;
		}
		if(bpoComVo.companyName==""||bpoComVo.companyName==null||bpoComVo.companyName==undefined){
			boo = true;
			alert("请输入单位名称");
			return false;
		}
		if(bpoComVo.effectdate==""||bpoComVo.effectdate==null||bpoComVo.effectdate==undefined){
			boo = true;
			alert("请选择收单起始日期");
			return false;
		}
		if(bpoComVo.quitadate==""||bpoComVo.quitadate==null||bpoComVo.quitadate==undefined){
			boo = true;
			alert("请选择收单频率");
			return false;
		}
	});
	
	if(boo){
		return false;
	}
	$.ajax({
		type:"POST",
		contentType:"application/json; charset=UTF-8",
		dataType:"json",
		url:"receiveUnit/saveBpoCompany.do",
		data:JSON.stringify(bpoComVoList),
		error:function(){
			alert("保存失败");
		},
		success:function(data){
				if(data.message =="success"){
					alert("保存成功");
				}else if(data.existed != null){
					var existedList = "";
					for(var i=0;i<data.existed.length;i++){
						existedList += data.existed[i]+" ";
					}
					alert(existedList+"已存在");
				}
		}
	});
}

//选择总公司
var companyForExcel = "";
function chooseCompany(element){
	$("#selectBranch").attr("code","");
	$("#selectBmdm").attr("code","");
	var companyCode = element.id;
	$("#selectCompany").attr("code",companyCode);
	companyForExcel = companyCode;
	if(companyCode=='80000000000000'){//健康险
		$("#selectBranch").html("请选择分公司");
		$("#selectBmdm").html("请选择支公司");
		$("#branchCompany").html("");
		$("#bmdm").html("");
		getHealthBranchCom(companyCode);
	}else if(companyCode=='0000000'){//产险
		$("#selectBranch").html("请选择分公司");
		$("#selectBmdm").html("请选择支公司");
		$("#branchCompany").html("");
		$("#bmdm").html("");
		getPropertyBranchForSearch(companyCode);
	}else if(companyCode=='00000000000000'){//寿险
		$("#selectBranch").html("请选择分公司");
		$("#selectBmdm").html("请选择支公司");
		$("#bmdm").html("");
		getLifeOrg(companyCode);
	}
}

//公司信息-选择总公司
function chooseCompany2(element){
	
	var companyCode = element.id;
	var index = element.parentElement.id;
	
	if(companyCode=='80000000000000'){//健康险
		var id = "#branchComList2"+index;
		$("#ssgs_"+index).attr("data-code",companyCode);
		$("#ssfgs_"+index).html("请选择分公司");
		$(id).html("");
		getHealthBranchCom2(companyCode,index);
	}else if(companyCode=='0000000'){//产险
		$("#ssgs_"+index).attr("data-code",companyCode);
		$("#ssfgs_"+index).html("请选择分公司");
		var id = "#branchComList2"+index;
		$(id).html("");
		getPropertyBranch(companyCode,index);
		
	}else if(companyCode=='00000000000000'){//寿险
		$("#ssgs_"+index).attr("data-code",companyCode);
		$("#ssfgs_"+index).html("请选择分公司");
		var id = "#branchComList2"+index;
		$(id).html("");
		getLifeOrg2(companyCode,index);
	}
}

function chooseCompanyForSerach(element){
	var companyCode = element.id;//所属公司
	var index = element.parentElement.id;//objId
	
	if(companyCode=='80000000000000'){//健康险
		$("#ssgs_"+index).attr("data-code",companyCode);
		$("#ssgs_"+index).html("请选择分公司");
		var id = "#fgs_"+index;
		$(id).html("");
		getHealthBranchComForSearch(companyCode,index);
	}else if(companyCode=='0000000'){//产险
		$("#ssgs_"+index).attr("data-code",companyCode);
		$("#ssgs_"+index).html("请选择分公司");
		var id = "#fgs_"+index;
		$(id).html("");
		getPropertyBranchByUpdate(companyCode,index);
	}else if(companyCode=='00000000000000'){//寿险
		$("#ssgs_"+index).attr("data-code",companyCode);
		$("#ssgs_"+index).html("请选择分公司");
		var id = "#fgs_"+index;
		$(id).html("");
		getLifeOrgForSearch(companyCode,index);
	}
}

//获取产险分公司
function getPropertyBranch(companyCode,index){
	chooseBranchCompany(companyCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/getPropertyBranch.do",
		data:"",
		error:function(){
			alert("获取产险分公司失败");
		},
		success:function(data){
			if(data.length>0){
				var id = "#branchComList2"+index;
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchId+" name=\""+index+"\" data-code="+data[i].branchId+" class=\"chan-branch\" onclick=\"getChanBmdmForAdd(this)\" >"+data[i].branchName+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").addClass("sel_hov");		
				},function (){
					$(this).removeClass("sel_hov");	
				}).on('click',function(){
					var _this = $(this).html();
					var _code=$(this).attr("data-code");
					var _thisText = $(this).closest(".select").find(".sel_text");
					if(_code != undefined){
						_thisText.attr("data-code",_code);
					}
					_thisText.html(_this);
				});
			}
		}
	});
}

//公司信息-修改获取产险分公司
function getPropertyBranchByUpdate(companyCode,index){
	chooseBranchCompany(companyCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/getPropertyBranch.do",
		data:{"comCode":companyCode},
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			if(data.length>0){
				var id = "#fgs"+index;
				$("#bmdmspan_"+index).html("");
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchId+" name=\""+index+"\" data-code="+data[i].branchId+" class=\"company-info-branch\" onclick=\"getChanBmdm(this)\" >"+data[i].branchName+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").addClass("sel_hov");		
				},function (){
					$(this).removeClass("sel_hov");	
				}).on('click',function(){
					var _this = $(this).html();
					var _code=$(this).attr("data-code");
					var _thisText = $(this).closest(".select").find(".sel_text");
					if(_code != undefined){
						_thisText.attr("data-code",_code);
					}
					_thisText.html(_this);
				});
			}
		}
	});
}


//搜索条件-获取产险分公司
function getPropertyBranchForSearch(companyCode){
	$.ajax({
		type:"POST",
		url:"receiveUnit/getPropertyBranch.do",
		data:"",
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			if(data.length>0){
				$("#branchCompany").html("");
				for(var i=0;i<data.length;i++){
					$("#branchCompany").append("<li id="+data[i].branchId+" name=\""+index+"\" class=\"property-branch\" data-pro-code="+data[i].branchId+" >"+data[i].branchName+"</li>");
				}
				$(".selectChoose").find(".sel_list li").hover( function (){
					$(".selectChoose").addClass("sel_hov");		
				},function (){
					$(this).removeClass("sel_hov");	
				}).on('click',function(){
					var _this = $(this).html();
					var _code=$(this).attr("data-pro-code");
					var _thisText = $(this).closest(".selectChoose").find(".sel_text");
					if(_code != undefined){
						_thisText.attr("code",_code);
					}
					_thisText.html(_this);
			
					//获取产险部门组
					$.ajax({
						type:"POST",
						url:"getPropertyBmdm.do",
						data:{"branchId":_code},
						error:function(){
							alert("获取产险部门组失败");
						},
						success:function(data){
							if(data.length>0){
								$("#bmdm").empty();
								for ( var i = 0; i < data.length; i++) {
									var bmdm = data[i];
									$("#bmdm").append("<li id="+bmdm.branchId+" onclick=\"chooseBmdmCompany(this)\" >"+noNull(bmdm.branchName)+"</li>");
								}
							}
							
							$(".selectChoose").find(".sel_list li").hover( function (){
								$(".selectChoose").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);
						
							});
						}
					});
					
					
				});
			}
		}
	});
}


////获取产险支公司
//$(functoin(){
//	
//})

//选择分公司
var companyBranchCode = "";
var companyBmdmCode = "";

function chooseBranchCompany(code){
	companyBranchCode = code;
}
//选择支公司
function chooseBmdmCompany(element){
	companyBmdmCode = element.id;
	$("#selectBmdm").attr("code",element.id);
}

//公司信息-选择支公司
function chooseBmdmCompany2(element){

	var index = $(element).attr("name");
	$("#sszz_"+index).attr("data-code",element.id);
}

function chooseBmdmCompanyForSearch(element){

	var index = $(element).attr("name");
	$("#bmdmspan_"+index).attr("data-code",element.id);
}

//获取健康险支公司列表
function getBranchCompany(element){
	$("#selectBmdm").html("请选择支公司");
	var branchCode = element.id;
	$("#selectBranch").attr("code",branchCode);
//	chooseBranchCompany(branchCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryBranchCompany.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				$("#bmdm").html("");
				for(var i=0;i<data.length;i++){
					$("#bmdm").append("<li id="+data[i].code+" onclick=\"chooseBmdmCompany(this)\" >"+data[i].name+"</li>");
				}
				$(".selectChoose").find(".sel_list li").hover( function (){
					$(".selectChoose").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);
			
				});
			}
		}
	});
}

//公司信息-获取健康险支公司列表
function getBranchCompany2(element){
	var branchCode = element.id;
	var index = $(element).attr("name");
	$("#sszz_"+index).html("请选择中支");
	$("#ssfgs_"+index).attr("data-code",branchCode);
//	chooseBranchCompany(branchCode);
	$.ajax({
		
		type:"POST",
		url:"receiveUnit/queryBranchCompany.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				var id = "#bmdmList2"+index;
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].code+" name=\""+index+"\" onclick=\"chooseBmdmCompany2(this)\" >"+data[i].name+"</li>");
					$(".select").find(".sel_list li").hover( function (){
						$(".select").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);
				
					});
				}
			}
		}
	});
}

//公司信息-获取健康险支公司列表forsearch
function getBranchCompanyForSearch(element){
	var branchCode = element.id;
	var index = $(element).attr("name");
	$("#bmdmspan_"+index).html("请选择中支");
	$("#branchspan_"+index).attr("data-code",branchCode);
	chooseBranchCompany(branchCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryBranchCompany.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			if(data.length>0){
				var id = "#zgs"+index;
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].code+" name=\""+index+"\" onclick=\"chooseBmdmCompanyForSearch(this)\" >"+data[i].name+"</li>");
					$(".select").find(".sel_list li").hover( function (){
						$(".select").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 getLifeBmdmCompany(element){
	$("#selectBmdm").html("请选择支公司");
	var branchCode = element.id;
	$("#selectBranch").attr("code",branchCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/getLifeBmdmCompany.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取寿险支公司失败");
		},
		success:function(data){
			if(data.length>0){
				$("#bmdm").html("");
				for(var i=0;i<data.length;i++){
					$("#bmdm").append("<li id="+data[i].branchid+" onclick=\"chooseBmdmCompany(this)\" >"+data[i].name+"</li>");
				}
				$(".selectChoose").find(".sel_list li").hover( function (){
					$(".selectChoose").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);
			
				});
			}
		}
	});
}

//公司信息-获取寿险支公司列表
function getLifeBmdmCompany2(element){
	var branchCode = element.id;
	var index = element.getAttribute("name");
	$("#ssfgs_"+index).attr("data-code",branchCode);
	$("#sszz_"+index).html("请选择支公司");
	chooseBranchCompany(branchCode);
	$.ajax({
		
		type:"POST",
		url:"receiveUnit/getLifeBmdmCompany.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取寿险支公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				var id = "#bmdmList2"+index;
				$(id).html("");
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchid+" name=\""+index+"\" onclick=\"chooseBmdmCompany2(this)\" >"+data[i].name+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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 getLifeBmdmCompanyForSearch(element){
	var branchCode = element.id;
	var index = element.getAttribute("name");
	$("#bmdmspan_"+index).html("请选择中支");
	$("#branchspan_"+index).attr("data-code",branchCode);
	chooseBranchCompany(branchCode);
	$.ajax({
		
		type:"POST",
		url:"receiveUnit/getLifeBmdmCompany.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取寿险支公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				var id = "#zgs"+index;
				$(id).html("");
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchid+" name=\""+index+"\" onclick=\"chooseBmdmCompanyForSearch(this)\" >"+data[i].name+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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 getChanBmdm(element){
	var branchCode = element.id;
	var index = element.getAttribute("name");
	$("#bmdmspan_"+index).html("请选择中支");
	$("#branchspan_"+index).attr("data-code",branchCode);
	chooseBranchCompany(branchCode);
	$.ajax({
		
		type:"POST",
		url:"getPropertyBmdm.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取寿险支公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				var id = "#zgs"+index;
				$(id).html("");
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchId+" name=\""+index+"\" onclick=\"chooseBmdmCompanyForSearch(this)\" >"+noNull(data[i].branchName)+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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 getChanBmdmForAdd(element){
	var branchCode = element.id;
	var index = element.getAttribute("name");
	$("#ssfgs_"+index).attr("data-code",branchCode);
	$("#sszz_"+index).html("请选择支公司");
	chooseBranchCompany(branchCode);
	$.ajax({
		
		type:"POST",
		url:"getPropertyBmdm.do",
		data:{"branchId":branchCode},
		error:function(){
			alert("获取寿险支公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				var id = "#bmdmList2"+index;
				$(id).html("");
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchId+" name=\""+index+"\" onclick=\"chooseBmdmCompany2(this)\" >"+data[i].branchName+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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 getLifeOrg(comCode){
	chooseBranchCompany(comCode);
	$.ajax({
		
		type:"POST",
		url:"receiveUnit/queryLifeBranchCompany.do",
		data:{"comCode":comCode},
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				$("#branchCompany").html("");
				$("#bmdm").html("");
				for(var i=0;i<data.length;i++){
					$("#branchCompany").append("<li id="+data[i].branchid+" onclick=\"getLifeBmdmCompany(this)\" >"+data[i].name+"</li>");
					
				}
				$(".selectChoose").find(".sel_list li").hover( function (){
					$(".selectChoose").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);
			
				});
			}
		}
	});
}

//健康险
function getHealthBranchCom(comCode){
	
	chooseBranchCompany(comCode);
	$.ajax({
		
		type:"POST",
		url:"receiveUnit/queryHealthBranchCom.do",
		data:"",
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			
			if(data.length>0){
				$("#branchCompany").html("");
				for(var i=0;i<data.length;i++){
					$("#branchCompany").append("<li id="+data[i].code+" onclick=\"getBranchCompany(this)\" >"+data[i].name+"</li>");
				}
				$(".selectChoose").find(".sel_list li").hover( function (){
					$(".selectChoose").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);
			
				});
			}
		}
	});
}

//公司信息-寿险公司
function getLifeOrg2(comCode,index){
	chooseBranchCompany(comCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryLifeBranchCompany.do",
		data:{"comCode":comCode},
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			if(data.length>0){
				var id = "#branchComList2"+index;
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchid+" name=\""+index+"\" onclick=\"getLifeBmdmCompany2(this)\" >"+data[i].name+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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);
				});
			}
		}
	});
}
//for search
function getLifeOrgForSearch(comCode,index){
	chooseBranchCompany(comCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryLifeBranchCompany.do",
		data:{"comCode":comCode},
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			if(data.length>0){
				var id = "#fgs"+index;
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].branchid+" name=\""+index+"\" onclick=\"getLifeBmdmCompanyForSearch(this)\" >"+data[i].name+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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 getHealthBranchCom2(comCode,index){
	chooseBranchCompany(comCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryHealthBranchCom.do",
		data:"",
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			if(data.length>0){
				var id = "#branchComList2"+index;
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].code+" name=\""+index+"\" onclick=\"getBranchCompany2(this)\" >"+data[i].name+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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);
				});
			}
		}
	});
}

//公司信息-健康险forSearch
function getHealthBranchComForSearch(comCode,index){
	chooseBranchCompany(comCode);
	$.ajax({
		type:"POST",
		url:"receiveUnit/queryHealthBranchCom.do",
		data:"",
		error:function(){
			alert("获取分公司失败");
		},
		success:function(data){
			if(data.length>0){
				var id = "#fgs"+index;
				$(id).html("");
				for(var i=0;i<data.length;i++){
					$(id).append("<li id="+data[i].code+" name=\""+index+"\" onclick=\"getBranchCompanyForSearch(this)\" >"+data[i].name+"</li>");
				}
				$(".select").find(".sel_list li").hover( function (){
					$(".select").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 addRow(){
	if($(".chawuxinxi").length>0){
		$("#receiveUnitList").empty();
	}
		
	var i = $("#receiveUnitList").find("tr").length+Number(1);
	var html = $("#receiveUnitList").html();
	var now = new Date();
//	var day = now.getDate();
//	var year = now.getFullYear();
//	var month = now.getMonth();
	var nowDate = now.getFullYear()+"-"+(Number(now.getMonth())+1)+"-"+now.getDate();
	
	var tr = "<tr class=\"new\" onclick=\"selectRow(this)\"><td>"+i+"</td><td><input type=\"text\" class=\"input_time i_s_100 setDate\" id=\"03\" value=\""+nowDate+"\" onclick=\"WdatePicker()\" /></td><td><input type=\"text\" class=\"input i_s_137 companyName\" autocomplete=\"off\" onkeydown=\"uTipSearch(this)\" onkeyup=\"uTipSearch(this)\" id=\"\" onfocus=\"bundleTip(this);\" /></td><td><input type=\"text\" class=\"input_time i_s_100 effectdate\" id=\"\" onclick=\"WdatePicker()\" /></td><td><input type=\"text\" class=\"input_time i_s_100 quitadate\" id=\"\" onclick=\"WdatePicker()\" /></td><td><div class=\"select i_s_60 mar_cen\"><span class=\"sel_text cycles\" id=\"\" data-code=\"03\" >每月</span><div class=\"sel_list i_ul_85\" style=\"display:none;\"><ul><li>每一周</li><li>每两周</li><li>每月</li><li>每两月</li><li>每季度</li><li>每半年</li><li>每年</li></ul></div></div></td><td><div class=\"select i_s_60 mar_cen\"><span class=\"sel_text isTpa\" id=\"\" data-code=\"\" >请选择</span><div class=\"sel_list i_ul_85\" style=\"display:none;\"><ul><li code=\"\" onclick=\"getAndSetIsTpa(this)\" >请选择</li><li code=\"Y\" onclick=\"getAndSetIsTpa(this)\">是</li><li code=\"N\" onclick=\"getAndSetIsTpa(this)\">否</li></ul></div></div></td>"+
	"<td><div class=\"select i_s_60 mar_cen\"><span class=\"sel_text businesstype\" id=\"\" data-code=\"\" >请选择</span><div class=\"sel_list i_ul_85\" style=\"display:none;\"><ul><li code=\"0\" onclick=\"getAndSetbusinesstype(this)\" >半外包</li><li code=\"1\" onclick=\"getAndSetbusinesstype(this)\">全外包</li><li code=\"2\" onclick=\"getAndSetbusinesstype(this)\">小额理赔</li></ul></div></div></td>"+
	"<td>&nbsp;</td></tr>";
	$("#receiveUnitList").html(html+tr);
	selected();
}


var index = "";
var unitObjId = "";
//选中行
function selectRow(flag){
	$("#selected-class-name").val($(flag).attr("class"));
	$("#select-row-id").val(flag.id);
	$("#select-row-name").val($(flag).children("td:eq(2)").children("input").val());
	unitObjId = flag.id;
	$(flag).css({"background-color": "#e5e5e5","cursor":"pointer"}).siblings().css("background-color", "#FFFFFF");
	index = $(flag).index();
}

//选中公司信息行
var comindex = "";
var comObjId = "";
function selectComRow(flag){
	comObjId = flag.id;
	$(flag).css({"background-color": "#e5e5e5","cursor":"pointer"}).siblings().css("background-color", "#FFFFFF");
	comindex = $(flag).index();
}

var comRowIndex = 1 ;
//增加公司信息空白行
function addCompanyRow(){
	var html = $("#companyList").html();
	 var path=location.href.substring(0,location.href.lastIndexOf('/'));
		path=path.substring(0,path.lastIndexOf('/'));
	var tr = "<tr onclick=\"selectComRow(this)\" id=\"\"><td>"+comRowIndex+"</td><td><div class=\"select i_s_137 mar_cen\"> <span id=\"ssgs_"+comRowIndex+"\" class=\"sel_text comddd\">健康险</span><div class=\"sel_list i_ul_162\" style=\"display:none;\"><ul id=\""+comRowIndex+"\"><li name=\""+comRowIndex+"\" id=\"80000000000000\" onclick=\"chooseCompany2(this)\">健康险</li><li id=\"00000000000000\" onclick=\"chooseCompany2(this)\">寿险</li><li id=\"0000000\" onclick=\"chooseCompany2(this)\">产险</li></ul></div></div></td><td><div class=\"select i_s_240 mar_cen\"> <span id=\"ssfgs_"+comRowIndex+"\" class=\"sel_text\">请选择分公司</span><div class=\"sel_list i_ul_265\"  style=\"display:none; height: 300px; overflow: scroll;\"><ul id=\"branchComList2"+comRowIndex+"\"></ul></div></div></td><td><div class=\"select i_s_240 mar_cen\"> <span class=\"sel_text\" id=\"sszz_"+comRowIndex+"\">请选择中支</span><div class=\"sel_list i_ul_265\" style=\"display:none; height: 300px; overflow: scroll;\"><ul id=\"bmdmList2"+comRowIndex+"\"></ul></div></div></td></tr>";

	$("#companyList").html(html+tr);
	comRowIndex++;
	selected();

}


//删除公司行

function deleteCompanyOrgAjax(comObjId){
	$.ajax({
		type:"POST",
		url:"receiveUnit/deleteBpoComOrgRl.do",
		data:{"objId":comObjId},
		error:function(){
			alert("删除失败");
		},
		success:function(data){
			$("#companyList tr").eq(comindex).remove();
			alert("删除成功");
		}
		
	});
}

function deleteCompanyRow(){
	
	if(comObjId==""){
		alert("请选择要删除的行");
		return;
	}else if(comObjId=="new Row"){
		$("#companyList tr").eq(comindex).remove();
	}else{
		if(confirm("确认删除？")==true){
			deleteCompanyOrgAjax(comObjId);
	}
	}
}

//保存公司信息
function saveCompany(){
	var parameters = "";
	
	$("#companyList").find("tr").each(function (){
		var id = "";
		var objId = $(this).attr("id");
		if(objId!=undefined){
			id = objId;
		}
		var c = $(this).find(".sel_text:eq(0)").attr("data-code");
		var b = $(this).find(".sel_text:eq(1)").attr("data-code");
		var d = $(this).find(".sel_text:eq(2)").attr("data-code");
		parameters +='{"objId":"'+id+'","orgId":"'+c+'","branchId":"'+b+'","bmdm":"'+d+'"},';
	});
	if(parameters!=''){
		parameters ="["+parameters.substring(0,parameters.length-1)+"]";
	}
	
$.ajax({
		
		type:"POST",
		url:"receiveUnit/saveComInfo.do",
		data:{"parameters":parameters,"companyNo":companyNoForSave},
		error:function(){
			alert("保存失败");
		},
		success:function(data){
				if(data.message="success"){
					alert("保存成功");
				}
		}
	});

}

//导出Excel
function exportExcel(){

	var unitName = $("#unitNameExcel").val();
	var settingStart = $("#settingStartExcel").val();
	var settingEnd = $("#settingEndExcel").val();
	var company = $("#companyExcel").val();
//	var booa = false;
//	var boob = false;
//	var booc = false;
	
	(unitName==null||unitName==undefined||unitName=="")?booa = true:booa = false;
	if((settingStart==null||settingStart==undefined||settingStart=="")&&(settingEnd==null||settingEnd==undefined||settingEnd=="")){
		boob = true;
	}
	(company==null||company==undefined||company=="")?booc = true:booc = false;
	if($("#receiveUnitList tr").length == 0){
		alert("没有数据！");
		return;
	}
//	if(booa&&boob&&booc){
//		alert("没有数据！");
//		return;
//	}
	
	
	
	
	
	$("form").attr('action','receiveUnit/exportExcel.do');
	$("form").submit();
	
}

//删除收单单位行
function delUnit(flag){
	if($("#selected-class-name").val() == "new"){
		$("#receiveUnitList tr").eq(index).remove();
		$("#selected-class-name").val("");
		return;
	}
	var currentId = $("#select-row-id").val();
	
	if(currentId==""){
		alert("请选择要删除的行");
		return;
	}
	var selectedName = $("#select-row-name").val();
	if(confirm("是否删除\""+selectedName+"\"？")==true){

		$.ajax({
			type:"POST",
			url:"receiveUnit/deleteBpoCompany.do",
			data:{"objId":currentId},
			error:function(){
				alert("删除失败");
			},
			success:function(data){
				$("#receiveUnitList tr").eq(index).remove();
				//清空当前选择
				$("#select-row-id").val("");
				$("#select-row-name").val("");
				alert("已删除");

			}
			
		});
		
	}
}

function selected(){
	$(".select").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');
			}),
			$(".select").find(".sel_list li").hover( function (){
				$(".select").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 comSelected(){
	$(".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 (){
		$(".selectChoose").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);
	});
}

function getSelectContent(){
	$(".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 tipSearch(){
	
	var name = $("#unitName").val();
	$.ajax({
		type:"POST",
		url:"receiveUnit/tipSearch.do",
		data:{"name":name},
		error:function(){
		},
		success:function(data){
			var ul=$("<ul id=\"tip\"></ul>");
			if(data.length>0){
				for ( var i = 0; i < data.length; i++) {
					$(ul).append("<li style=\"background-color:#FFFFFF\" onmouseover=\"changbackclrFn(this)\" onblur=\"displayNone()\" onclick=\"selectedCompanyInfoFn('"+data[i].companyName+"')\">"+data[i].companyName+"</li>");
				}
				$("#tip-div").html(ul);
				$("#tip-div").css("display","");
			}else{
				$("#tip-div").css("display","none");
			}
		}
	});
}
function changbackclrFn(flag){
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
}
//chc_添加收单单位信息到文本框
function selectedCompanyInfoFn(flag){
	$("#unitName").val(flag);
	$("#tip-div").css("display","none");
}

function displayNone(){
	$("#tip").css("display","none");
}

function uTipSearch(obj){
	var name = $(obj).val();
	$.ajax({
		type:"POST",
		url:"receiveUnit/tipSearch.do",
		data:{"name":name},
		error:function(){
		},
		success:function(data){
			$("#u-tip").html("");
			if(data.length>0){
				for ( var i = 0; i < data.length; i++) {
					$("#u-tip").append("<li onclick=\"fillInVal(this)\"  onmouseover=\"changbackclrFn(this)\">"+data[i].companyName+"</li>");
				}
			}
		}
	});
}

function fillInVal(obj){
	
	$("#current-tip").val($(obj).html());
	$("#u-tip-div").remove();
}

function bundleTip(obj){
	$("#u-tip-div").remove();
	var ct = document.getElementById("current-tip");
//	ct.removeAttribute(id);
	$(ct).removeAttr("id");
	
	$(obj).attr("id","current-tip");
	var html = "<div class=\"sel_list\" id=\"u-tip-div\" style=\"display:block; margin:0 0 0 53px; width:164px; padding:0;\"><ul id=\"u-tip\" ></ul></div>";
	$(obj).parent("td:eq(0)").append(html);
	
}

function dropTip(obj){
	$(obj).attr("id","");
	$("#u-tip-div").remove();
}