$(function(){ 	
	if($("#pageFrom").val()=='03'||$("#pageFrom").val()=='02'){
		$("#_isReport").hide();
		$(".bot_btn_03").removeAttr("onclick");
		$(".bot_btn_03").hide();
	}
	
	//2-通知书页面
	if ($("#saveCompleteFlag").val() == 'Y' && $("#skipPageFlag").val() == '2') {
		var params = new Array();
		params["claimNo"] = $("#claimNo").val();
		params["noticeType"] = "04";
		params["allrefused"]=$("#allrefused").val();
		SkipByPost("queryClaimNotice.do", params);
	}
	
	/*selectBox*/
	$(".select1").hover( function () {
		$(this).addClass("sel_over");
	},function () {
		$(this).removeClass("sel_over");
	}).on('click',function(){
		var _this = $(this),
			_bthis = $(this).find(".sel_list1");
		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_list1"),
			_this = $(this);
		_thisa.hide();
		_this.removeAttr('isopen');
	}),$(this).find(".sel_list1 li").hover( function (){
		$(this).addClass("sel_hov");		
	},function (){
		$(this).removeClass("sel_hov");	
	}).on('click',function(){
		var _this = $(this).html();
		var _code=$(this).attr("code");
		var _thisText = $(this).closest(".select1").find(".sel_text");
		if(_code != undefined){
			_thisText.attr("code",_code);
		}
		var _index = $(this).attr("spIndex");
		var _flag = $(this).attr("spFlag");
		
		/**
		 * REQHOP1-161028-1564 心安质重续保需求  hl
		 * 下拉框选值优先级：否('N')>不确定('F')>是('Y')
		 * 注: 只能优先级低选高，不可优先级高选低
		 * */
//		var index_classcode = $("#" + "insuranceProcessList_" + _index + "_classcode" ).val();
//		//如果是心安质重、心安怡、花样年华 ，只能优先级低选高，不可优先级高选低
//		if(index_classcode == '27770100' || index_classcode == 'EH7701' 
//				|| index_classcode == '12120100' || index_classcode == 'AH1201'
//				|| index_classcode == '87010100' || index_classcode == 'HT0101'){
//			var curIsRenewal = $("#" + "insuranceProcessList_" + _index + "_isRenewal").val();
//			if(curIsRenewal == 'N'){
//				alert('优先级不允许从高改为低');
//			}else if(curIsRenewal == 'F'){
//				if(_flag == 'Y'){
//					alert('优先级不允许从高改为低');
//				}else{
//					if(confirm("是否确定需要修改自动续保标识")){
//						$("#" + "insuranceProcessList_" + _index + "_isRenewal").val(_flag);
//						_thisText.html(_this);
//					}
//				}
//			}else if(curIsRenewal == 'Y'){
//				if(confirm("是否确定需要修改自动续保标识")){
//					$("#" + "insuranceProcessList_" + _index + "_isRenewal").val(_flag);
//					_thisText.html(_this);
//				}
//			}
//		}else{
//			if(_flag != $("#" + "insuranceProcessList_" + _index + "_isRenewal" ).val()){
//				if(confirm("是否确定需要修改自动续保标识")){
//					$("#" + "insuranceProcessList_" + _index + "_isRenewal").val(_flag);
//					_thisText.html(_this);
//				}
//			}else{
//				_thisText.html(_this);
//			}
//		}
		
		if(_flag != $("#" + "insuranceProcessList_" + _index + "_isRenewal" ).val()){
			if(confirm("是否确定需要修改自动续保标识")){
				$("#" + "insuranceProcessList_" + _index + "_isRenewal").val(_flag);
				_thisText.html(_this);
			}
		}else{
			_thisText.html(_this);
		}
		
		
		var vid =$(this).closest(".select1").attr("id")+"_code";
		var vidinput =$(this).closest(".select1").attr("id")+"input";
		if($("#"+vid).is("input")){
			$("#"+vid).val($(this).attr("code"));
		}else{
			$("body").append("<input type='hidden' id='"+vid+"'>");
			$("#"+vid).val($(this).attr("code"));
		}
		if($("#"+vidinput).is("input")){
			$("#"+vidinput).val(_this);
		}
	});
	
});

function skip2OtherPageByClaimNo(url, claimNo) {
	var params = new Array();
	params["claimNo"] = claimNo;
	SkipByPost(url, params);
}
function skip2OtherPageByClaimNos(url, claimNo) {
//	if(!flag){
//		if(confirm("是否需要对您的操作进行保存？")){
//			if(saveAuditClaimResult()==false){
//				return false;
//			}
//		}
//	}
	var params = new Array();
	params["claimNo"] = claimNo;
	SkipByPost(url, params);
}
function skip2OtherPage(url,claimNo,pid,pageFrom){
	var params = new Array();
	params["claimNo"] = claimNo;
	params["pid"] = pid;
	params["pageFrom"] = pageFrom;
	SkipByPost(url, params);
}
function selectReasonResult(idx, val){
	$("#refuseReasonId_"+ idx ).val(val);
	$("#revokeReasonId_"+ idx ).val(val);
	if(val=="其他"){
		$("#refuseReasonId_"+ idx +"_div").hide();
		$("#revokeReasonId_"+ idx +"_div").hide();
		$("#refuseReasonId_"+ idx ).val("");
		$("#revokeReasonId_"+ idx ).val("");
		$("#insuranceLiabilityList_" + idx + "_claimResReason").val(val);
		$("#insuranceLiabilityList_" + idx+ "_claimResReason").show();
	}else{
		$("#insuranceLiabilityList_" + idx + "_claimResReason").val(val);
	}
}
//选择理赔结论时触发
function selectClaimResult(idx, val) {
	$("#isXay_"+idx+"_ClaimResultNotNull").css("display","none");
	$("#insuranceLiabilityList_" + idx + "_claimResCode").val(val);
	$("#insuranceLiabilityList_" + idx + "_claimResReason").val("");
	if ("01" == val) {
		$("#insuranceLiabilityList_" + idx + "_claimResReason").val("符合条款约定赔付");
	} else {
		if ($("#insuranceLiabilityList_" + idx + "_claimResReason").val() == "符合条款约定赔付") {
			$("#insuranceLiabilityList_" + idx + "_claimResReason").val("");
		}
	}
	if("03"==val){
		$("#refuseReasonId_"+ idx ).val("");
		$("#revokeReasonId_"+ idx ).val("");
		$("#insuranceLiabilityList_" + idx + "_claimResReason").val("");
		$("#refuseReasonId_"+ idx ).html("");
		$("#refuseReasonId_"+ idx +"_div").show();
		$("#revokeReasonId_"+ idx +"_div" ).hide();
		$("#insuranceLiabilityList_" + idx+ "_claimResReason").hide();
	}else if("04"==val){
		$("#refuseReasonId_"+ idx ).val("");
		$("#revokeReasonId_"+ idx ).val("");
		$("#refuseReasonId_"+ idx +"_div" ).hide();
		$("#insuranceLiabilityList_" + idx + "_claimResReason").val("");
		$("#revokeReasonId_"+ idx ).html("");
		$("#revokeReasonId_"+ idx +"_div").show();
		$("#insuranceLiabilityList_" + idx+ "_claimResReason").hide();
	}else{
		$("#refuseReasonId_"+ idx +"_div").hide();
		$("#revokeReasonId_"+ idx +"_div").hide();
		$("#insuranceLiabilityList_" + idx+ "_claimResReason").show();
	}
	
	if("02"==val || "05"==val){
		//心安怡融通、和解必填
		if($("#insuranceLiabilityList_" + idx + "_classcode").val()=="87010100"||$("#insuranceLiabilityList_" + idx + "_classcode").val()=="HT0101"){
			$("#isXay_"+idx+"_ClaimResultNotNull").css("display","");
		}
	}
	//01赔付
	//02通融赔付
	//03拒赔
	//04不预授理
	//05和解
	//责任处理
	if ("01" == val || "02" == val || "05" == val) {
		var claimAccStr = $("#claimAccStr").val();
		//01身故died
		//02伤残deformity
		//03重大疾病seriousIllness
		//04门急诊医疗outpatient
		//05住院医疗inHospitalMedical
		//06住院补贴inHospitalSubsidy
		//07女性生育
		//08牙科
		//09眼科
		//审核/审核结论/险种状态后续处理，重疾选择赔付，自动终止保单（排除花样年华12120100除了90天内责任其余的责任不终止保单）
		var classcode = $("#insuranceLiabilityList_" + idx + "_classcode").val();
		var delCode = $("#insuranceLiabilityList_" + idx + "_delCode").val();
		var typeNo = $("#insuranceLiabilityList_" + idx + "_typeNo").val();
		if (null != claimAccStr && "" != claimAccStr && claimAccStr.indexOf("03") > -1 && !(classcode=='12120100' && delCode+typeNo!='12300')) {		
			var policyNo = $("#insuranceLiabilityList_" + idx + "_policyNo").val();

			//险种状态后续处理
			$("#insuranceProcessTable input[type=hidden][id^=insuranceProcessList_][id$=_policyNo]").each(function(){
				var policyNoId = $(this).attr("id");
				var idx = policyNoId.substring(21,22);
				var tempPolicyNo = $(this).val();
				var tempClasscode = $("#insuranceProcessList_" + idx + "_classcode").val();
				if (policyNo == tempPolicyNo && classcode == tempClasscode) {
					$("#insuranceProcessList_" + idx + "_isTermination").val("Y");
					$("#insuranceProcessList_" + idx + "_isTermination_span").html("是");
				}
			});
		}
	} else {
		var claimAccStr = $("#claimAccStr").val();
		//01身故died
		//02伤残deformity
		//03重大疾病seriousIllness
		//04门急诊医疗outpatient
		//05住院医疗inHospitalMedical
		//06住院补贴inHospitalSubsidy
		//07女性生育
		//08牙科
		//09眼科
		//审核/审核结论/险种状态后续处理，重疾选择赔付，自动终止保单（排除花样年华12120100除了90天内责任其余的责任不终止保单）
		var classcode = $("#insuranceLiabilityList_" + idx + "_classcode").val();
		var delCode = $("#insuranceLiabilityList_" + idx + "_delCode").val();
		var typeNo = $("#insuranceLiabilityList_" + idx + "_typeNo").val();
		if (null != claimAccStr && "" != claimAccStr && claimAccStr.indexOf("03") > -1 && !(classcode=='12120100' && delCode+typeNo!='12300')) {		
			var policyNo = $("#insuranceLiabilityList_" + idx + "_policyNo").val();
			
			//险种状态后续处理
			$("#insuranceProcessTable input[type=hidden][id^=insuranceProcessList_][id$=_policyNo]").each(function(){
				var policyNoId = $(this).attr("id");
				var idx = policyNoId.substring(21,22);
				var tempPolicyNo = $(this).val();
				var tempClasscode = $("#insuranceProcessList_" + idx + "_classcode").val();
				if (policyNo == tempPolicyNo && classcode == tempClasscode && $("#insuranceProcessList_" + idx + "_isTermination").val() == "Y") {
					$("#insuranceProcessList_" + idx + "_isTermination").val("N");
					$("#insuranceProcessList_" + idx + "_isTermination_span").html("否 ");
				}
			});
		}
	}
}
var flag=false;
//选择险种后续处理时触发
function selectPolicyProcess(idx, name, val) {
	$("#" + "insuranceProcessList_" + idx + "_" + name).val(val);
	//chc 20160819要上线的坑死人的心安怡优化需求
	var classcode = $("#insuranceProcessList_" + idx + "_classcode").val();
	if(name=="isClause"){
		 var tempA=$("#" + "insuranceProcessList_" + idx + "_isClause").val();
		 /**
		 * REQHOP1-161028-1564 心安质重续保需求 hl
		 * */
		 if(classcode=='87010100' || classcode=='HT0101' //|| classcode == '27770100' || classcode == 'EH7701'
			 ){
			 if(val==tempA){
			   $("#isClauseReasonSpanA_"+idx).css("display","");
			   $("#isClauseReasonSpanB_"+idx).css("display","");
		   }else{
			   $("#isClauseReasonSpanA_"+idx).css("display","none");
			   $("#isClauseReasonSpanB_"+idx).css("display","none");
		   }
		 }
	}
}
//理赔结论保存
function saveAuditClaimResult() {
	
	/*$("#saveAuditClaimResult").attr("onclick","");*/
	//险种状态后续处理
	var insuranceProcessListCount = $("#insuranceProcessList_count").val();
	var insuranceLiabilityListCount = $("#insuranceLiabilityList_count").val();
	//审核意见
	var auditMesgInfo = $("#auditMesgInfo").val;
	if(auditMesgInfo.length>500){
		alert("审核意见必须小于500字");
		return false;
	}
	
	for (var i = 0; i < insuranceProcessListCount; i++) {
		var policyNo = $("#insuranceProcessList_" + i + "_policyNo").val();
		var classcode = $("#insuranceProcessList_" + i + "_classcode").val();
		var isTermination = $("#insuranceProcessList_" + i + "_isTermination").val();//是否终止：Y是、N否
		var isClause = $("#insuranceProcessList_" + i + "_isClause").val();//是否解约：Y是、N否
		var isRefund = $("#insuranceProcessList_" + i + "_isRefund").val();//是否退费：1	退还全额保费、2	退还现金价值、3	退还未满期保费、4否(不退费)
		var isRenewal = $("#insuranceProcessList_" + i + "_isRenewal").val();//是否续保：Y是、N否、F 不确定
		// chc 20160819要上线的坑死人的心安怡优化需求
		var otherReason=$("#otherReason_"+i).val();
		$("#insuranceProcessList_"+i+"_isClauseReason").val(otherReason);
		
		//只有选择解约时才能对退费进行选择  05和解
		if (("4" != isRefund && "" != isRefund) && "Y" != isClause) {
			alert("只有选择解约时才能对退费进行选择！");
			return false;
		}
		// chc 20160819要上线的坑死人的心安怡优化需求
		if("Y" == isClause && $("#isClauseReasonId_"+i).attr("code")=="" && (classcode=='87010100'|| classcode=='HT0101')){
			alert("请选择解约原因！");
			return false;
		}
		if("Y" == isClause && ($("#otherReason_"+i).val()=="" && $("#isClauseReasonId_"+i).attr("code")=="05") && (classcode=='87010100'|| classcode=='HT0101')){
			alert("请填写解约原因！");
			return false;
		}
		
		
		
		
		//只有选择过理赔结论的保单才可操作险种状态后续处理，否则提示“未做出理赔结论的保单不可操作险种状态后续处理 ”
		if ("Y" == isTermination || "Y" == isClause || ("4" != isRefund && "" != isRefund) || "N" == isRenewal) {
			var hasLiabClaimResult = false;
			for (var j = 0; j < insuranceLiabilityListCount; j++) {
				var policyNoInLiab = $("#insuranceLiabilityList_" + j + "_policyNo").val();
				var classcodeInLiab = $("#insuranceLiabilityList_" + j + "_classcode").val();
				var claimResCodeInLiab = $("#insuranceLiabilityList_" + j + "_claimResCode").val();
				if (policyNo == policyNoInLiab && classcode == classcodeInLiab && (null != claimResCodeInLiab && "" != claimResCodeInLiab)) {
					hasLiabClaimResult = true;
				}
			}
			if (hasLiabClaimResult == false) {
				alert("未做出理赔结论的保单不可操作险种状态后续处理！");
				return false;
			}
		}
	}

	//01赔付
	//02通融赔付
	//03拒赔
	//04不预授理
	//05和解
	//责任处理
	var hasClaimResult = false; //是否有理赔结论
	var hasClaimPay = false;    //是否有赔付的理赔结论
	var hasClaimNoAccept = false;//是否有不予受理的理赔结论
	var syncrescode=0;//猝死保险金和疾病身故保险金不能同时赔付
	//检查理赔结论
	for (var i = 0; i < insuranceLiabilityListCount; i++) {
		var claimResCode = $("#insuranceLiabilityList_" + i + "_claimResCode").val();
		//是否有理赔结论
		if (null != claimResCode && "" != claimResCode && hasClaimResult == false) {
			hasClaimResult = true;
		}
		//是否有赔付的理赔结论
		if (hasClaimPay == false && "01" == claimResCode) {
			hasClaimPay = true;
		}
		//是否有不予受理的理赔结论
		if (hasClaimNoAccept == false && "04" == claimResCode) {
			hasClaimNoAccept = true;
		}
		var classcodetemp=$("#insuranceLiabilityList_" + i + "_classcode").val();
		if(classcodetemp=="82030100"){
			if("01" == claimResCode||"02" == claimResCode||"05" == claimResCode){
				syncrescode=syncrescode+1;
			}
			
		}
	}
	//82030100下 不能同时出现理赔结论是赔付、通融赔付、和解
	if(syncrescode>1){
		alert("猝死保险金和疾病身故保险金不能同时赔付！");
		return false;
	}
	//判断是否有理赔结论
	if (hasClaimResult == false) {
		alert("请给出理赔结论！");
		return false;
	}
	for (var i = 0; i < insuranceLiabilityListCount; i++) {
		
		var a = $("#insuranceLiabilityList_" + i + "_claimResReasonType").html().substring(0,2);
		var b = $("#insuranceLiabilityList_" + i + "_claimResReason").val().trim();
		
		var classcodetemp=$("#insuranceLiabilityList_" + i + "_classcode").val();
		if(classcodetemp=="87010100"||classcodetemp=="HT0101"){
			var reasonType=$("#insuranceLiabilityList_" + i + "_claimResReasonType").html().trim();
			if(reasonType=="通融赔付"||reasonType=="和解"){
				if(b==null||b==""){
					alert("请输入结论原因");
					return false;
				}
			}
		}
		
		if(a=="拒赔" || a=="撤案"){
			if(b==null||b==""){
				alert("请输入结论原因");
				return false;
			}
		}
	}
	var caseSource = $("#caseSource").val();
	var isBpo = $("#isBpo").val();
	var isTpa = $("#isTpa").val();
	
	//当结论为“不预授理”时
	if (hasClaimNoAccept == true) {
		if (confirm("确定对此赔案进行撤案操作吗，先前对其他责任所作的结论将不被保存？") == false) {
			if (caseSource != '3') {
				$("#saveFlag").val("1");//保存标识：1-暂存、2保存
			} else {
				return false;
			}
		}
	} else {
		if (confirm("是否已完成赔案结论操作？") == false) {
			if (caseSource != '3') {
				$("#saveFlag").val("1");//保存标识：1-暂存、2保存
			} else {
				return false;
			}
		}
	}
//	if (caseSource == '3' || ((caseSource == '1' || caseSource == '2') && isBpo == 'N') || (caseSource == '2' && isTpa == '1')) {
	if(caseSource=='3'||caseSource=='1' ||(caseSource=='2' && isTpa=='1') || (caseSource=='2' && isBpo == 'N')){//20160729上线需求
		isPrint();
	}
	
	//保存是否直付案件
	var isDpa = $("#dpa-value").attr("is-dpa");
	$("#is-dpa").val(isDpa);
	$("#saveAuditClaimResult").attr("onclick","");
	$("#humanAuditClaimForm").submit();
	flag=true;
}

//选择是否打印通知书
function isPrint(){	
	if($("#_checkboxs").attr("class")=='check_yes'){		
		$("#isPrint").val("Y");
	}else{		
		$("#isPrint").val("N");
	}
}

function selectPayType(flag){
	$(flag).attr("is-dpa")=='1'?$(flag).attr("is-dpa","0"):$(flag).attr("is-dpa","1");
}

//chc 20160819要上线的坑死人的心安怡优化需求
function selectReasonFn(flag,index){
	if($(flag).attr("code")=="05"){
		$("#otherReason_"+index).removeAttr("readonly");
	}else{
		$("#otherReason_"+index).val("");
		$("#otherReason_"+index).attr("readonly","readonly");
	}
	$("#insuranceProcessList_"+index+"_isClauseReasonCode").val($(flag).attr("code"));
	
}



