var index = -1;
$(function(){
	$("#roleSet").focus();
	
	
});



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

}

function addTr(){
	var html=$("#roleList").html();
	var tr = "<tr id=\"addnewtr\" onclick=\"choosEd(this)\">"+
	"<td><a class=\"icon_cancel\"><input type=\"text\" class=\" input i_s_240 input_bot_line\" style=\"text-align: center;\" code=\"\" value=\"\" onblur=\"compareRepeat(this);\" maxlength=\"10\"/></a></td>"+
	"<td><a class=\"icon_cancel\"><input type=\"text\" class=\" input i_s_240 input_bot_line\" style=\"text-align: center;\" code=\"\" value=\"\" onblur=\"compareRepeat(this);\" maxlength=\"10\"/></a></td>"+
	"<td><a class=\"icon_cancel\"><input type=\"text\" class=\" input i_s_240 input_bot_line\" style=\"text-align: center;\" code=\"\" value=\"\" maxlength=\"10\"/></a></td>"+
	"<td><div class=\"select i_s_100 float_l\"><span class=\"sel_text\" change=\"\">否</span><div class=\"sel_list i_ul_125\" style=\"display: none;\"><ul><li>是</li><li>否</li></ul></div></div></td>"+
    "</tr>";
	$("#roleList").html(html+tr);
	selected();
}

function delTr(){
	if(index!=-1){
		var mainRoleName = $("#roleList tr:eq("+index+")").find("input:eq(0)").val().trim();
		var emproleid = $("#roleList tr:eq("+index+")").attr("id").trim();
		$.ajax({
			 type: "POST", 
		     url: "checkMoudleToRole.do",
		     data:"mainRoleName="+mainRoleName+"&emproleid="+emproleid,
		     dataType:"json",
		     async : false,
		     success:function(data){
		    	if(data.returnCode =="noexists"){
		    		if (confirm("是否确认删除?")) {
		    			if(emproleid != "addnewtr"){
		    				$.ajax({
			    				 type: "POST", 
			    			     url: "deleteRoleSet.do",
			    			     data:"id="+emproleid,
			    			     dataType:"json",
			    			     async : false,
			    			     success:function(data){
			    			    	$("#roleList tr:eq("+index+")").remove();
					    			index=-1;
					    			alert("删除成功");
			    			     },
			    			     error:function(data){
			    			    	 alert("删除失败");
			    			     }
			    			});
		    			}else{
		    				$("#roleList tr:eq("+index+")").remove();
			    			index=-1;
			    			alert("删除成功");
		    			}
		    			
		    			
		    		}
		    	}else if(data.returnCode =="exists"){
		    		alert("请先删除模块角色对应关系");
		    		return;
		    	}else if(data.returnCode == "existsEmp"){
		    		alert("该角色已存在分配人员，不能删除");
		    		return;
		    	}
		     },
		     error:function(data){
		     }
		});
	}else if(index == -1){
		alert("请选择需要删除的角色");
	}
	
}

function saveEmpRole(){
	var flag="1";
	var changeStr  ='';
	var jsonStr= "";
	$("#roleList").find("input").each(function(){
		if(!$(this).val()){
			flag="2";
			return false;
		}
	});
	if(flag == "2"){
		alert("所有设置均不能为空，请仔细核对");
		return;
	}
	
	$("#roleList").find("tr").each(function(){
		var change = $(this).find(".sel_text").attr("change");
		var _this= $(this).find(".sel_text").html();
		var changeFlag = "1";
		$(this).find("input").each(function(){
			if($(this).val().trim() != $(this).attr("code")){
				changeFlag ="2";
				return false;
			}else if(change != _this){
				changeFlag ="2";
				return false;				
			}
		});
		if(changeFlag =="2"){
			var id=$(this).attr("id");
			if(id =='addnewtr'){
				id='';
			}
			var roleName=$(this).find("input:eq(0)").val().trim();
			var roleLevel =$(this).find("input:eq(1)").val().trim();
			var roleRelationshipLevel =$(this).find("input:eq(2)").val().trim();
			var roleType =$(this).find("span:eq(0)").html().trim();
			if(roleType =="是"){
				roleType='02';
			}else{
				roleType='01';
			}
			
			changeStr +='{"id":"'+id+'","roleName":"'+roleName+'","roleLevel":"'+roleLevel+'","roleRelationshipLevel":"'+roleRelationshipLevel+'","roleType":"'+roleType+
			'"},';
		}
		
	
	});
	
	if(changeStr ==''){
		alert("没有做修改，无需保存");
		return;
	}

	changeStr = "["+changeStr.substring(0,changeStr.length-1)+"]";
	jsonStr = '{"ids":"","empRoleList":' + changeStr+'}';
	$.ajax({
		 type: "POST", 
		 contentType:"application/json; charset=UTF-8",
	     url: "saveRoleSet.do",
	     data:jsonStr,
	     dataType:"json",
	     success:function(data){
	    	 alert("保存成功");
	    	 SkipByPost("showRoleSet.do","");
	     },
	     error:function(data){
	    	alert(data) ;
	     }
	});
}

function compareRepeat(input){
	if( $(input).attr("code") != $(input).val()){
		var roleName = $("#roleList tr:eq("+index+") input:eq(0)").val();
		var roleLevel = $("#roleList tr:eq("+index+") input:eq(1)").val();
		$("#roleList").find("tr").each(function(i,n){
			if( i != index){
				if(roleName == $(this).find("input:eq(0)").val() && roleLevel == $(this).find("input:eq(1)").val()){
					alert("已存在相同的角色信息,请确认");
					$("#roleList tr:eq("+index+") input:eq(1)").attr("value","");
					$("#roleList tr:eq("+index+") input:eq(1)").focus();
					return false;
				}
			}
		});
	}
	
	
}

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 (){
		$(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);
	});
	
}