//流水号
var sSn = null;

function queryBlacklist(){
	if($("#certificates").val()==null || $("#certificates").val()=="" || $("#certificates").val()==undefined){
		alert("请选择证件类型!");
		return;
	}
	
	//去掉placeholder提示文字值
	if($("#keyword").val()==$("#keyword").attr("placeholder")){
		$("#keyword").val()=="";
	}
	
	//获取搜索关键词
	var keyword = $("#keyword").val();
	
	//身份证输入号验证
	  if($("#certificates").val()=="a" && !IDCardCheck(keyword)){
		return;
	}
	

//	var black = "";

	//AJAX提交关键词
	$.ajax({
		
		type:"POST",
		url:"queryBlacklist.do",
		data:{"stype":$("#certificates").val(),"sId":keyword},
		error:function(){
			alert("查询失败！");
			return;
		},
		success:function(data){
			
			//清空旧的列表
			$("#blacklistContext").html("");
			//清空编辑框
			add();
			
			if(data==""||data==null){
				$("#blacklistContext").append("<tr><td>没有相关内容，请检查输入是否正确</td></tr>");
				return;
			}
			for(var i=0;i<data.length;i++){
			
				var blacklist= data[i];
				
				//是否有效中文字符
				var sFlagString = null;
				if(blacklist.sFlag=="Y"){
					sFlagString = "是";
				}else if(blacklist.sFlag=="N"){
					sFlagString = "否";
				}else{
					sFlagString = "";
				}
				$("#blacklistContext").append("<tr style=\"cursor:pointer\" name=\""+blacklist.ssn+"\" onclick=\"getSId(this)\" id=\""+keyword+"\"><td>"+blacklist.sFName+"</td><td>"+keyword+
				"</td><td>"+sFlagString+"</td><td>"+blacklist.sBegdate+"</td><td>"
				+blacklist.sFEnddate+"</td><td>"+blacklist.sClaimno+"</td><td>"+blacklist.sPolicyno+"</td></tr>");
			
			/**
				var blacklist = data[i];
				
				//是否有效中文字符
				var sFlagString = null;
				if(blacklist.sFlag=="Y"){
					sFlagString = "是";
				}else if(blacklist.sFlag=="N"){
					sFlagString = "否";
				}else{
					sFlagString = "";
				}
			
				sSn = blacklist.ssn;
				if(blacklist.sFName==null){
					$("#blacklistContext").html("");
					$("#blacklistContext").append("<tr><td>没有相关内容，请检查输入是否正确</td></tr>");
					return;
				}

				
				
				//清空旧的列表
				$("#blacklistContext").html("");
				//清空编辑框
				add();
				 black +="<tr style=\"cursor:pointer\" onclick=\"getSId(this)\" id=\""+keyword+"\"><td>"+blacklist.sFName+"</td><td>"+keyword+
				"</td><td>"+sFlagString+"</td><td>"+blacklist.sBegdate+"</td><td>"
				+blacklist.sFEnddate+"</td><td>"+blacklist.sClaimno+"</td><td>"+blacklist.sPolicyno+"</td></tr>";
				$("#blacklistContext").html(black);
				*/
		}
		}
	});
}

//将点击的列信息录入下面表单
var sId="";
function getSId(obj){
	var idType = $("#certificates").val();
	var index = $(obj).index();
	sSn = $(obj).attr("name");
	$(obj).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	
	var tds=obj.childNodes;

	//获取点击列的证件号
     sId = tds[1].innerHTML;
     
     //根据证件号重新查询黑名单信息并填入编辑表单
	$.ajax({
		
		type:"POST",
		url:"getIndexBlacklist.do",
		data:{"sId":sId,"index":index,"stype":idType},
		error:function(){
			alert("获取黑名单失败");
			return;
		},
		success:function(data){

			$("#ulController").attr("style","display:none;");
			
			blacklist = data;
			getReasonString(blacklist.sAddreason);
			
			if(idType == "a"){
				$("#idtype").html("身份证");
			}else if(idType == "c"){
				$("#idtype").html("护照");
			}else if(idType == "d"){
				$("#idtype").html("军官证");
			}else if(idType == "j"){
				$("#idtype").html("港澳台护照");
			}else if(idType == "l"){
				$("#idtype").html("台湾居民通行证");
			}else if(idType == "z"){
				$("#idtype").html("其它");
			}
			
			 $("#certificates1").val(blacklist.sValtype);//证件类型
			 $("#sFId").val(sId);//被保人身份证号
			 $("#sfname").val(blacklist.sFName);//被保人姓名
			 $("#sclaimno").val(blacklist.sClaimno);//赔案号
			 $("#uw_risk_desc").val(blacklist.uw_risk_desc);//风险信息
			 $("#saddresonValue").html(blacklist.reason);//录入原因
			 $("#manual_add_code").val(blacklist.manual_add_code);//录入原因码
			 $("#saddabs").val(blacklist.sAddabs);//具体描述
			 $("#smoveno").val(blacklist.smoveno+blacklist.smovenoName);//撤销员
			 $("#smoveabstr").val(blacklist.sMovereason);//撤销原因
			 $("#saddresonCode").html(blacklist.sAddreason);
			 $("#reasonString").attr("style","display:none;");
			 
			 //健康告知
			 var healthInform = data.healthInformList;
				 if(healthInform>0){
					for(var i=0;i<healthInform.length;i++){
						var h=healthInform[i];
						alert(h.tel_code1);
						alert(h.sValtype);
						$("#healthInform").append("<tr><td>"+i+"</td><td>"+h.flag+"</td><td>"+h.tel_code1+"</td><td>"+h.tel_code2+"</td><td>"+h.tel_type+"</td><td>"+h.tel_value+"</td><td>"+h.dscrptn+"</td></tr><input id=\"certificates1\" type=\"hidden\" name=\"certificates1\" value="+h.sValtype+"/>");
					}
				}
			 
			 
			 
			 $("#sFId").attr("readonly","readonly");
			 $("#sfname").attr("readonly","readonly");
			 $("#sclaimno").attr("readonly","readonly");
			 $("#uw_risk_desc").attr("readonly","readonly");
			 $("#manual_add_code").attr("readonly","readonly");
			 $("#saddabs").attr("readonly","readonly");//具体描述
			 
			 if(blacklist.sMovereason==""||blacklist.sMovereason==null){
				 $("#submitButton").attr("onclick","cancelBlacklist()");
				 $("#smoveabstr").attr("readonly",false);
				
			 }else{
				 $("#smoveabstr").attr("readonly","readonly");
				 $("#submitButton").attr("onclick","");
			 }
			 
		}
		
	});
	
};


//保存编辑

function saveBlacklist(){
	
	var certificates = $("#certificates1").val();//证件类型
	var sFId = $("#sFId").val();//被保人身份证号
	var sfname = $("#sfname").val();//被保人姓名
	var sclaimno = $("#sclaimno").val();//赔案号
	var uw_risk_desc = $("#uw_risk_desc").val();//风险信息
//	var saddreson = $("#saddresonValue").html();//进入黑名单原因
	var manual_add_code =$("#manual_add_code").val();//录入原因
	var saddabs = $("#saddabs").val();//具体描述
	var saddreson = reasonCode;
	
	//录入字段校验
	if(strlength(sfname)>12){
		alert("姓名应小于6个汉字");
		return;
	}
	if(strlength(sclaimno)>15){
		alert("赔案号不能大于15位");
		return;
	}
	if(strlength(uw_risk_desc)>2500){
		alert("赔案号不能大于1250个汉字");
		return;
	}
	if(strlength(saddabs)>100){
		alert("赔案号不能大于50个汉字");
		return;
	}
	
	//身份证输入验证
	if(certificates=="a" && !IDCardCheck(sFId)){
		return;
	}
	
	$.ajax({
		
		type:"POST",
		url:"updateBlacklist.do",
		data:{"stype":certificates,'sFId':sFId,'sfname':sfname,'sclaimno':sclaimno,'uw_risk_desc':uw_risk_desc,'saddreson':saddreson,'manual_add_code':manual_add_code,'saddabs':saddabs,'sSn':sSn},
		error:function(){
			alert("保存失败");
			return;
		},
		success:function(data){
			if(data.message=="success"){
				alert("保存成功");
			}else if(data.message=="error"){
				alert("保存失败");
			}
		}
	});
}



//添加按钮

function add(){
	
	$("#ulController").attr("style","");
	
	$("#idtype").html("请选择证件类型");
	$("#certificate1").val("");//证件类型
    $("#sFId").val("");//被保人身份证号
    $("#sfname").val("");//被保人姓名
    $("#sclaimno").val("");//赔案号
    $("#uw_risk_desc").val("");//风险信息
    $("#saddresonValue").html("请选择类型");//录入原因
    $("#manual_add_code").val("");//录入原因码
    $("#saddabs").val("");//具体描述
    $("#reasonString").attr("style","");
    $("#saddresonCode").html("");
    $("#smoveno").val("");
    $("#smoveabstr").val("");
    
    $("#sFId").attr("readonly",false);
	 $("#sfname").attr("readonly",false);
	 $("#sclaimno").attr("readonly",false);
	 $("#uw_risk_desc").attr("readonly",false);
	 $("#manual_add_code").attr("readonly",false);
	 $("#saddabs").attr("readonly",false);
	 
	 $("#submitButton").attr("onclick","saveBlacklist()");
}





//获取li的值

var ChoosEdvalue="";
function getLiValue(element){
	
    ChoosEdvalue = element.innerHTML;
    
	
}


//填入信息至编辑表单

function edit(){
	
	
	$.ajax({
		
		type:"POST",
		url:"queryBlacklist.do",
		data:sId,
		error:function(){
			alert("获取黑名单失败");
			return;
		},
		success:function(data){
			
			blacklist = data[0];
			
		
			
			 $("#sFId").val(blacklist.sId);//被保人身份证号
			 $("#sfname").val(blacklist.sFName);//被保人姓名
			 $("#sclaimno").val(blacklist.sClaimno);//赔案号
			 $("#uw_risk_desc").val(blacklist.uw_risk_desc);//风险信息
			 $("#manual_add_code").val(blacklist.saddreson);//录入原因
			 $("#saddreson").val(blacklist.manual_add_code);//录入原因码
			 $("#saddabs").val(blacklist.sAddabs);//具体描述
			 
			 
			 
		}
	});
}



    //黑名单撤销信息

function cancelBlacklist(){
	var smoveabstr = $("#smoveabstr").val();
	if(sSn==null){
		alert("请选中要撤销的黑名单");
		return;
	}
	if(smoveabstr==""){
		alert("请输入撤销原因");	
		return;
	}
	if(strlength(smoveabstr)>40){
		alert("撤销原因不能超过20个汉字！");
		return;
	}
	
	$.ajax({
		type:"POST",
		url:"cancelBlacklist.do",
		data:{"sSn":sSn,"smoveabstr":smoveabstr},
		error:function(){
			alert("撤销黑名单失败");
			return;
		},
		success:function(data){
			if(data.message=="success"){
				alert("撤销成功");
			}else if(data.message=="error"){
				alert("撤销失败");
			}
		}
	});
}


//锁定撤销栏
function lockCancelBlack(){
	$("#smoveno").attr("readonly","readonly");
	$("#smoveabstr").attr("readonly","readonly");
	$("#submitButton").attr("onclick","saveBlacklist()");
}

//锁定黑名单录入信息栏
function lockBlacklistEdit(){
	$("#sFId").attr("readonly","readonly");
	$("#sfname").attr("readonly","readonly");
	$("#sclaimno").attr("readonly","readonly");
	$("#uw_risk_desc").attr("readonly","readonly");
	$("#saddresonValue").html("请选择类型");
	$("#manual_add_code").attr("readonly","readonly");
	$("#submitButton").attr("onclick","cancelBlacklist()");
}


//身份证校验
function IDCardCheck(num) {
    num = num.toUpperCase();
    //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。   
    if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
        alert('输入的身份证号长度不对，或者号码不符合规定！\n15位号码应全为数字，18位号码末位可以为数字或X。');
        return false;
    }
    //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
    //下面分别分析出生日期和校验位 
    var len, re;
    len = num.length;
    if (len == 15) {
        re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
        var arrSplit = num.match(re);

        //检查生日日期是否正确 
        var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
        var bGoodDay;
        bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
        if (!bGoodDay) {
            alert('输入的身份证号里出生日期不对！');
            return false;
        }
        else {
            //将15位身份证转成18位 
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            var nTemp = 0, i;
            num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);
            for (i = 0; i < 17; i++) {
                nTemp += num.substr(i, 1) * arrInt[i];
            }
            num += arrCh[nTemp % 11];
            return true;
        }
    }
    if (len == 18) {
        re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
        var arrSplit = num.match(re);

        //检查生日日期是否正确 
        var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
        var bGoodDay;
        bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
        if (!bGoodDay) {
            alert(dtmBirth.getYear());
            alert(arrSplit[2]);
            alert('输入的身份证号里出生日期不对！');
            return false;
        }
        else {
            //检验18位身份证的校验码是否正确。 
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
            var valnum;
            var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            var nTemp = 0, i;
            for (i = 0; i < 17; i++) {
                nTemp += num.substr(i, 1) * arrInt[i];
            }
            valnum = arrCh[nTemp % 11];
            if (valnum != num.substr(17, 1)) {
                alert('18位身份证的校验码不正确！'); //应该为： + valnum
                return false;
            }
            return true;
        }
    }
    return false;
}

//录入原因
function getReasonString(sAddreason){
	
	$.ajax({
	
		type:"POST",
		url:"getReasonString.do",
		data:{"entryCode":sAddreason,"categoryCode":"40"},
		error:function(){
			alert("获取录入原因失败！");
			return;
		},
		success:function(data){
			$("#saddresonValue").html(data);
		}
		
		
		
		
	});
	
}

//记住点击的录入原因
var reasonCode = null;
var reasonString = null;
function rememberReason(obj){
	reasonCode = obj.id;
	reasonString = $("#"+reasonCode).html();
	$("#saddresonCode").html(reasonCode);
	
}


//回车查询
function enterQuery(){
	 var event = window.event||arguments.callee.caller.arguments[0];
	 if(event.keyCode==13){
		 queryBlacklist();
	 }
}

//当编辑录入原因的时候显示录入员
function displayStaffNumber(){
	$("#smoveno").val($("#cancelBlacklistEmpNo").val()+$("#cancelBlacklistEmpName").val());
}


function certificate(val){
	$("#keyword").val("");
	$("#certificates").val(val);
}

function certificate1(val){
	$("#sFId").val("");
	$("#certificates1").val(val);
}