var classCodeIndex = -1;
var index = -1;
$(function(){
	
	$.ajax({
		type:"POST",
		url:"getAutoRules.do",
		data:"",
		error :function(){
		},
		success :function(data){
			var accTypes = data.accType;
			var acc = [];
			if(accTypes.indexOf(",") == -1){
				acc[0] = accTypes;
			}else{
				acc = accTypes.split(",");
			}
			$.each(acc, function(i, n){
				var m=n*1;
				$('#acc'+m).attr('class','re_icon'+m+' re_icon'+m+'_cur');
			});
		}
	});
	
	
	
});


function chooseAcc(id){
	var code=id.substr(3);
	if($('#'+id).attr('class')=="re_icon"+code){
		$('#'+id).attr('class','re_icon'+code +' re_icon'+code+'_cur');
	}else{
		$('#'+id).attr('class','re_icon'+code);
	}
}

function classCodeChoose(){
	$("#classCodeTbl").html("");
	classCodeIndex = -1;
	var classcodeName =$.trim($("#classcodeName").val());
	if(classcodeName==""){
		alert("请填写险种名称");
		return;
	}
    $.ajax({  
        type: "POST",  
        url: "classCodeSearch.do",  
        data:"classcodeName="+classcodeName,
        success: function(data){
        	var items = data.classcodeList; 
        	if(items.length > 0){
        		$.each(items, function(i, n){
           		 $("#classCodeTbl").append("<tr id="+items[i].classcode+" onclick=\"choosclasscodeEd(this)\">" +
           				"<td width=\"50%\" id=\"classname\">"+items[i].classname+"</td>"+
           				"<td width=\"50%\" id=\"classcode\">"+items[i].classcode+"</td>"+
                  		
                  			"</tr>");
           	 });
        	}else{
        		$("#classCodeTbl").append("<tr><td colspan=\"2\">查询无数据</td></tr>");
        	}
        	 
        }  
    }); 
}


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

function choosclasscodeEd(flag){
	 
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	classCodeIndex=$(flag).index();
}

function addTr(){
	var tr = "<tr id=\"addnewtr\" onclick=\"choosEd(this)\">"+
	"<td><input readonly=\"readonly\" id=\"classname\" name=\"classname\" type=\"text\" class=\"input i_s_240 input_bot_line\" style=\"text-align: center;\" onclick=\"showDiv('open2')\"/></td>"+
	"<td><input readonly=\"readonly\" id=\"classcode\" name=\"classcode\" type=\"text\" class=\"input i_s_100 input_bot_line\" style=\"text-align: center;\" onclick=\"showDiv('open2')\"/></td>"+
    "</tr>";
	
	$("#classCodeList").append(tr);
}

function delTr(){
	
	if(index!=-1){
		if (confirm("是否确认删除?")) {
			$("#classCodeList tr:eq("+index+")").remove();
			index=-1;
		}
	}else if(index == -1){
		alert("请选择需要删除的险种");
		return;
	}
	
}

function saveClasscodeDiv(divId){
	if(classCodeIndex < 0 ){
		alert("请选择一条险种");
		return;
	}
	//获取险种选择列
	var tr = $("#classCodeTbl tr:eq("+classCodeIndex+")"); 
	var classname = tr.find("td[id='classname']").html();
	var classcode = tr.find("td[id='classcode']").html();
	//获取
	var classcodestr = $("#classCodeList tr:eq("+index+")");
	classcodestr.find("INPUT[id='classcode']").val(classcode);
	classcodestr.find("INPUT[id='classname']").val(classname);
	$.closePopupLayer(divId);
	
}
function saveAutoInfo(){
	
	var param = {};
	var objId = $("#objId").val();
	//员工工号字符串
	var empNostr='';
	//险种代码字符串
	var classcodestr='';
	var minEstamt = $("#minEstamt").val();
	var maxEstamt = $("#maxEstamt").val();
	var isAllowDeceit = $("#divisAllowDeceit_code").val();
	var accTypestr='';
	$("#staffList").find("span").each(function(i,n){
		if($(this).attr("class") == "check_yes"){
			empNostr += $(this).attr("code")+',';
		}
		
	});
	if(empNostr !=''){
		empNostr = empNostr.substring(0, empNostr.length-1);
	}else{
		alert("请选择至少一位核赔员参与自动分配");
		return;
	}
	
	$("#classCodeList").find("tr").each(function(){
		classcodestr += $(this).find("INPUT[id='classcode']").val() +',';
	});
	if(classcodestr !=''){
		classcodestr = classcodestr.substring(0, classcodestr.length-1);
	}
	
	if(minEstamt ==''){
		alert("最小限额不能为空");
		return;
	}
	if(maxEstamt ==''){
		alert("最大限额不能为空");
		return;
	}
	
	if(minEstamt > maxEstamt){
		alert("最小限额不能大于最大限额");
		return;
	}
	$("#accTypeList").find("a").each(function(){
		if($(this).attr("class").indexOf("_cur") > 0 ){
			accTypestr += $(this).attr("code") +',';
		}
	});
	if(accTypestr !=''){
		accTypestr = accTypestr.substring(0, accTypestr.length-1);
	}
	param['objId'] = objId;
	param['empNostr'] = empNostr;
	param['classcodestr'] = classcodestr;
	param['minEstamt'] = minEstamt;
	param['maxEstamt'] = maxEstamt;
	param['isAllowDeceit'] = isAllowDeceit;
	param['accTypestr'] = accTypestr;
	if(!confirm("请确认是否保存自动分配规则?")){
		return;
	}
	
	 $.ajax({  
	        type: "POST",  
	        url: "saveAutoRole.do",  
	        data:param,
	        success: function(data){
	        	alert("自动分配规则保存成功");
	        	SkipByPost("autoTaskRole.do","");
	        },
	        error:function(data){
	        	alert("系统错误，请稍后再试");
	        }
	  }); 
}


function checkMoney(moneyInput){
	if($(moneyInput).val()!=""){
		var mny = /(^\d+$)|(^\d+.\d+$)/g;
		if(!mny.test($(moneyInput).val())){
			$(moneyInput).val("");
		}
	}
}