var accCodestr="";
$(document).ready(function(){
accCodestr=$("#accCode").val();//出险类型
});
//验证限额
function checkNorm(obj,len){
	var reg=/^\d{1,3}$/;
	var $norm=$(obj);
	if(len=="4"){
		reg=/^\d{1,4}$/;
	}
	if(!reg.test($norm.val())){
		$norm.val('');
	}
	if(len=="3"){
		var rate = $norm.val();
		if(rate>100){
			$norm.val('');
		}
	}
}
//出险信息
function getAccidentjson(){
	var accCodes="";
	var acctypes=$("#accTypes").val();//索赔事故性质
	var accArray=acctypes.trim().split("_");
	var accidentjson="";
	if(accCodestr.length>0){
		accCodes=accCodestr.substring(0, accCodestr.length-1).split(",");//出险类型
		if(accCodestr!=""&&acctypes==""){
			if(!confirm("选择了出险类型，未选择索赔事故性质时；将删除当前选择出险类型对应的索赔事故性质！")){
				return "error";
			}
//			alert("请选择对应的索赔事故性质！");
//			return "del";
		}
	}else{
		if(accCodestr==""&&acctypes!=""){
			alert("请选择出险类型！");
			return "error";
		}
	}
	for(var j=0;j<accCodes.length;j++){
		for(var i=0;i<accArray.length;i++){
			accidentjson+='{"accidentCode":"'+accCodes[j]+'","claimAccType":"'+accArray[i]+'","itemType":"1"},';
		}
	}
	if(accidentjson.length>1){
		return accidentjson.substring(0, accidentjson.length-1);
	}else{
		return "";
	}
	
	
}
//保存小额理赔配置信息
function saveSmallClaim(){
	var normtrs=$("#smallclaimTbl tr");
	var normjson="";
	var accidentCode="";//出险类型
	var claimAccType="";//索赔事故性质
	var norm="";//限额
	var jsonstr="";
	var applyTotalSum=$("#applyTotalSum").val();//案件申请总金额
	var qualityPassRate=$("#qualityPassRate").val();//小额自动质检通过率
	var labourCheckRate=$("#labourCheckRate").val();//自动审核人工复核率
	for (var i=1;i<normtrs.length-3;i++) {
		var tr=$(normtrs[i]);
		accidentCode=tr.find("INPUT[name='accidentCode']").val();
		claimAccType=tr.find("INPUT[name='claimAccType']").val();
		norm=tr.find("INPUT[name='norm']");
		if(norm.val().trim()!=""){
			normjson+='{"accidentCode":"'+accidentCode+'","claimAccType":"'+claimAccType+'","norm":"'+norm.val()+
			'","itemType":"2","applyTotalSum":"'+applyTotalSum+'","qualityPassRate":"'+qualityPassRate+'","labourCheckRate":"'+labourCheckRate+'"},';
		}else{
			alert("限额为空！");
			return;
		}
	}
	if(applyTotalSum==""){
		alert("案件申请总金额为空！");
		return;
	}
	if(qualityPassRate==""){
		alert("小额质检抽检率为空！");
		return;
	}
	if(labourCheckRate==""){
		alert("小额复核抽检率为空！");
		return;
	}
	var accidentJson=getAccidentjson();//出险类型，索赔事故性质json
	if(accidentJson!=""){
		if(accidentJson=="error"){
			return;
		}else{
			jsonstr='{"clmSmallClaimlist":['+normjson.substring(0, normjson.length-1)+","+accidentJson+']}';
		}
		
	}else{
		jsonstr='{"clmSmallClaimlist":['+normjson.substring(0, normjson.length-1)+']}';
		
	}
	$.ajax({
		type:"POST",
		contentType:"application/json; charset=UTF-8",
		url:"saveClmSmallClaimDeploy.do",
		data:jsonstr,
		error:function(){
			alert("操作失败");
		},success:function(data){
			alert("操作成功");
			initTrlist(data);
//			location.reload(false); 
		}
	});
	
}
//初始化行列表
function initTrlist(data){
	$("#smallclaimTbl tr:gt(0)").remove();
	var tr="";
	var claimdeploy;//含有总金额、通过率的对象
	for(var i=0;i<data.length;i++){
		var smallClaimDeploy=data[i];
		var itemType=smallClaimDeploy.itemType;
		if(itemType=="2"){
			claimdeploy=data[i];
			var accident=smallClaimDeploy.accidentCode=="2"?"疾病":"意外";
			var accType="";
			if(smallClaimDeploy.claimAccType=="04"){
				accType="门急诊医疗";
			}else if(smallClaimDeploy.claimAccType=="05"){
				accType="住院医疗";
			}else if(smallClaimDeploy.claimAccType=="06"){
				accType="住院补贴";
			}
			tr+='<tr><td><input type="hidden" name="accidentCode" value="'+smallClaimDeploy.accidentCode+'"></input>'+accident+'</td>'+
					'<td><input type="hidden" name="claimAccType" value="'+smallClaimDeploy.claimAccType+'"></input>'+accType+'</td>'+
					'<td><input type="text" name="norm" value="'+smallClaimDeploy.norm+'"  onkeyup="checkNorm(this,4)" /></td></tr>';
		}else{
			if(accCodestr.indexOf(smallClaimDeploy.accidentCode)==-1){
				accCodestr+=smallClaimDeploy.accidentCode+",";
			}
		}
	}
	tr+='<tr><td colspan="2">案件申请总金额：</td><td><input id="applyTotalSum" type="text" value="'+claimdeploy.applyTotalSum+'" onkeyup="checkNorm(this,4)"  /></td></tr>';
	tr+='<tr><td colspan="2">小额质检抽检率：</td><td><input id="qualityPassRate" type="text" value="'+claimdeploy.qualityPassRate+'" onkeyup="checkNorm(this,3)"  />%</td></tr>';
	tr+='<tr><td colspan="2">小额复核抽检率：</td><td><input id="labourCheckRate" type="text" value="'+claimdeploy.labourCheckRate+'" onkeyup="checkNorm(this,3)"  />%</td></tr>';
	$("#smallclaimTbl").append(tr);
}
//身故
function shenggu(a){
	if($('#shenggu').attr('class')=="re_icon1"){
		$('#shenggu').attr('class','re_icon1 re_icon1_cur');
		saveClaimAcc('add','01');
	}else{
		$('#shenggu').attr('class','re_icon1');
		saveClaimAcc('del','01');
	}
}
//伤残
function shangchang(a){
	if($('#shangchang').attr('class')=="re_icon2"){
		$('#shangchang').attr('class','re_icon2 re_icon2_cur');
		saveClaimAcc('add','02');
	}else{
		$('#shangchang').attr('class','re_icon2');
		saveClaimAcc('del','02');
	}
}
//重大疾病
function zhongda(a){
	if($('#zhongda').attr('class')=="re_icon3"){
		$('#zhongda').attr('class','re_icon3 re_icon3_cur');
		saveClaimAcc('add','03');
	}else{
		saveClaimAcc('del','03');
		$('#zhongda').attr('class','re_icon3');
	}
}
//门争论医疗
function menzhen(a){
	if($('#menzhen').attr('class')=="re_icon4"){
		$('#menzhen').attr('class','re_icon4 re_icon4_cur');
		saveClaimAcc('add','04');
	}else{
		$('#menzhen').attr('class','re_icon4');
		saveClaimAcc('del','04');
	}
	
}
//住院医疗
function zhuyuanyiliao(a){
	if($('#zhuyuanyiliao').attr('class')=="re_icon5"){
		$('#zhuyuanyiliao').attr('class','re_icon5 re_icon5_cur');
		saveClaimAcc('add','05');
	}else{
		$('#zhuyuanyiliao').attr('class','re_icon5');
		saveClaimAcc('del','05');
	}
}
//住院补贴
function zhuyuanbutie(a){
	if($('#zhuyuanbutie').attr('class')=="re_icon6"){
		$('#zhuyuanbutie').attr('class','re_icon6 re_icon6_cur');
		saveClaimAcc('add','06');
	}else{
		$('#zhuyuanbutie').attr('class','re_icon6');
		saveClaimAcc('del','06');
	}
}
//女性生育
function nvxingshengyu(a){
	if($('#nvxingshengyu').attr('class')=="re_icon7"){
		$('#nvxingshengyu').attr('class','re_icon7 re_icon7_cur');
		saveClaimAcc('add','07');
	}else{
		$('#nvxingshengyu').attr('class','re_icon7');
		saveClaimAcc('del','07');
	}
}
//牙科
function dentisty(a){
	if($('#dentisty').attr('class')=="re_icon8"){
		$('#dentisty').attr('class','re_icon8 re_icon8_cur');
		saveClaimAcc('add','08');
	}else{
		$('#dentisty').attr('class','re_icon8');
		saveClaimAcc('del','08');
	}
}
//眼科
function onculist(a){
	if($('#onculist').attr('class')=="re_icon9"){
		$('#onculist').attr('class','re_icon9 re_icon9_cur');
		saveClaimAcc('add','09');
	}else{
		$('#onculist').attr('class','re_icon9');
		saveClaimAcc('del','09');
	}
}
//大病门诊
function dabingmenzhen(a){
	if($('#dabingmenzhen').attr('class')=="re_icon11"){
		$('#dabingmenzhen').attr('class','re_icon11 re_icon11_cur');
		saveClaimAcc('add','10');
	}else{
		$('#dabingmenzhen').attr('class','re_icon11');
		saveClaimAcc('del','10');
	}
}
//紧急牙科
function jinjiyake(a){
	if($('#jinjiyake').attr('class')=="re_icon10"){
		$('#jinjiyake').attr('class','re_icon10 re_icon10_cur');
		saveClaimAcc('add','11');
	}else{
		$('#jinjiyake').attr('class','re_icon10');
		saveClaimAcc('del','11');
	}
}
//药房购药
function yaofanggouyao(a){
	if($('#yaofanggouyao').attr('class')=="re_icon12"){
		$('#yaofanggouyao').attr('class','re_icon12 re_icon12_cur');
		saveClaimAcc('add','12');
	}else{
		$('#yaofanggouyao').attr('class','re_icon12');
		saveClaimAcc('del','12');
	}
}
//体检
function tijian(a){
	if($('#tijian').attr('class')=="re_icon13"){
		$('#tijian').attr('class','re_icon13 re_icon13_cur');
		saveClaimAcc('add','13');
	}else{
		$('#tijian').attr('class','re_icon13');
		saveClaimAcc('del','13');
	}
}
//女性疾病
function girlDisease(a){
	if($('#girlDisease').attr('class')=="re_icon14"){
		$('#girlDisease').attr('class','re_icon14 re_icon14_cur');
		saveClaimAcc('add','14');
	}else{
		$('#girlDisease').attr('class','re_icon14');
		saveClaimAcc('del','14');
	}
}
//艾滋病
function aizibing(a){
	if($('#aizibing').attr('class')=="re_icon15"){
		$('#aizibing').attr('class','re_icon15 re_icon15_cur');
		saveClaimAcc('add','15');
	}else{
		$('#aizibing').attr('class','re_icon15');
		saveClaimAcc('del','15');
	}
}
//紧急医疗
function jinjiyiliao(a){
	if($('#jinjiyiliao').attr('class')=="re_icon16"){
		$('#jinjiyiliao').attr('class','re_icon16 re_icon16_cur');
		saveClaimAcc('add','16');
	}else{
		$('#jinjiyiliao').attr('class','re_icon16');
		saveClaimAcc('del','16');
	}
}
//体检育苗
function tijianyumiao(a){
	if($('#tijianyumiao').attr('class')=="re_icon17"){
		$('#tijianyumiao').attr('class','re_icon17 re_icon17_cur');
		saveClaimAcc('add','17');
	}else{
		$('#tijianyumiao').attr('class','re_icon17');
		saveClaimAcc('del','17');
	}
}
//先天性疾病
function xiantianxingjibing(a){
	if($('#xiantianxingjibing').attr('class')=="re_icon18"){
		$('#xiantianxingjibing').attr('class','re_icon18 re_icon18_cur');
		saveClaimAcc('add','18');
	}else{
		$('#xiantianxingjibing').attr('class','re_icon18');
		saveClaimAcc('del','18');
	}
}

//心血管
function xinxueguan(a){
	if($('#xinxueguan').attr('class')=="re_icon19"){
		$('#xinxueguan').attr('class','re_icon19 re_icon19_cur');
		saveClaimAcc('add','19');
	}else{
		$('#xinxueguan').attr('class','re_icon19');
		saveClaimAcc('del','19');
	}
}
//骨折
function guzhe(a){
     if($( '#guzhe').attr('class' )=="re_icon20" ){
          $( '#guzhe').attr('class' ,'re_icon20 re_icon20_cur');
          saveClaimAcc( 'add', '20');
     }else{
          $( '#guzhe').attr('class' ,'re_icon20' );
          saveClaimAcc( 'del', '20');
     }
}



/**出险类型选择 1、意外 2、疾病 3、其他*/	
function cxlxCheckBox(dataId){
	
	switch (dataId) {
	case 'cxlxYw':
		if(accCodestr.indexOf("1")==-1){
			accCodestr+="1,";
		}
		break;
	case 'cxlxJB':
		if(accCodestr.indexOf("2")==-1){
			accCodestr+="2,";
		}
		break;
	case 'cxlxQT':
		if(accCodestr.indexOf("3")==-1){
			accCodestr+="3,";
		}
		break;
	default:
		break;
	}
	if($("#"+dataId).is(".check_yes")){
		if(dataId=="cxlxYw"){
			accCodestr=accCodestr.replace("1,", "");
		}else if(dataId=="cxlxJB"){
			accCodestr=accCodestr.replace("2,", "");
		}else if(dataId=="cxlxQT"){
			accCodestr=accCodestr.replace("3,", "");
		}
	}
	
}
//初始化索赔事故 性质选项
function initclaimacc(accTypesStr){
	clearClaimAcc();
	var accTypes = accTypesStr.split(",");
	var i = 0 ;
	for(; accTypes != '' && i < accTypes.length ; i++ ){
		if(accTypes[i]=="01"){
			$('#shenggu').attr('class','re_icon1 re_icon1_cur');
		}
		if(accTypes[i]=="02"){
			$('#shangchang').attr('class','re_icon2 re_icon2_cur'); 
		}
		if(accTypes[i]=="03"){
			$('#zhongda').attr('class','re_icon3 re_icon3_cur');
		}
		if(accTypes[i]=="04"){
			$('#menzhen').attr('class','re_icon4 re_icon4_cur');
		}
		if(accTypes[i]=="05"){
			$('#zhuyuanyiliao').attr('class','re_icon5 re_icon5_cur');
		}
		if(accTypes[i]=="06"){
			$('#zhuyuanbutie').attr('class','re_icon6 re_icon6_cur');
		}
		if(accTypes[i]=="07"){
			$('#nvxingshengyu').attr('class','re_icon7 re_icon7_cur');
		}
		if(accTypes[i]=="08"){
			$('#dentisty').attr('class','re_icon8 re_icon8_cur');
		}
		if(accTypes[i]=="09"){
			$('#onculist').attr('class','re_icon9 re_icon9_cur');
		}
		if(accTypes[i]=="11"){
			$('#jinjiyake').attr('class','re_icon10 re_icon10_cur');
		}
		if(accTypes[i]=="10"){
			$('#dabingmenzhen').attr('class','re_icon11 re_icon11_cur');
		}
		if(accTypes[i]=="12"){
			$('#yaofanggouyao').attr('class','re_icon12 re_icon12_cur');
		}
		if(accTypes[i]=="13"){
			$('#tijian').attr('class','re_icon13 re_icon13_cur');
		}
		if(accTypes[i]=="14"){
			$('#girlDisease').attr('class','re_icon14 re_icon14_cur');
		}
		if(accTypes[i]=="15"){
			$('#aizibing').attr('class','re_icon15 re_icon15_cur');
		}
		if(accTypes[i]=="16"){
			$('#jinjiyiliao').attr('class','re_icon16 re_icon16_cur');
		}
		if(accTypes[i]=="17"){
			$('#tijianyumiao').attr('class','re_icon17 re_icon17_cur');
		}
		if(accTypes[i]=="18"){
			$('#xiantianxingjibing').attr('class','re_icon18 re_icon18_cur');
		}
		if(accTypes[i]=="19"){
			$('#xinxueguan').attr('class','re_icon19 re_icon19_cur');
		}
		if(accTypes[i]=="20"){
			$('#guzhe').attr('class','re_icon20 re_icon20_cur');
		}
		saveClaimAcc('add',accTypes[i]);
	}
	
}
//清除所有索赔事故选项
function clearClaimAcc(){
	var claimaccs=$("#spsgxz_td a");
	for(var i=0;i<claimaccs.length;i++){
		$(claimaccs[i]).attr('class','re_icon'+(i+1));
	}
	$("#accTypes").val("");
}
//保存索赔事故性质值
function saveClaimAcc(method,value){
	if(method=='add'){
		var accTypesStr = $("#accTypes").val();
		var accTypes = accTypesStr.split("_");
		var i = 0 ;
		//判断是否已经保存了这个 索赔事故性质
		var flag = 0;
		for(; accTypes != '' && i < accTypes.length ; i++ ){
			if(accTypes[i]==value){
				flag = 1;
				break;
			}
		}
		if(flag==0){
			if($("#accTypes").val()==''){
				$("#accTypes").val(value);
			}else{
				$("#accTypes").val($("#accTypes").val()+"_"+value);
			}
		}
	}
	
	if(method=='del'){
		var accTypesStr = $("#accTypes").val();
		var accTypes = accTypesStr.split("_");
		var i = 0 ;
		//判断是否已经保存了这个 索赔事故性质
		var flag = 0;
		for(; accTypes != '' && i < accTypes.length ; i++ ){
			if(accTypes[i]==value){
				accTypes[i] = '00';
				flag = 1;
				break;
			}
		}
		if(flag==1){
			var accTypesNew = '';
			i = 0;
			var position = 0;
			for(; accTypes != '' && i < accTypes.length ; i++ ){
				if(accTypes[i]!='00'){
					if(position==0){
						accTypesNew = accTypes[i];
					}else{
						accTypesNew = accTypesNew + '_' + accTypes[i];
					}
					position++; 
				}
			}
			
			$("#accTypes").val(accTypesNew);
		}
	}
	
}