var moduleLi = '';
var empRoleLi = '';
var index = -1;
$(function(){
	showMoudleToRole();
});

function showMoudleToRole(){
	getModuleAndRoleList();
	$.ajax({
		 type: "POST",  
	     url: "getModuleToRole.do",
	     data:"",
	     success:function(data){
	    	for(var i = 0 ;i< data.length;i++){
	    		var info= data[i];
	    		var tr = "<tr id=\""+info.id+"\" onclick=\"choosEd(this)\">"+
	    		"<input type=\"hidden\" code=\""+info.pubRoleId+"\" value=\""+info.pubRoleId+"\" readonly=\"readonly\"/>"+
	    		"<td><div class=\"select i_s_240 mar_cen\"> <span class=\"sel_text\" code=\""+info.pubRoleName+"\">"+info.pubRoleName+"</span><div class=\"sel_list i_ul_265\" style=\"display:none;\"><ul>" +
	    		moduleLi +
	    		"</ul></div></div></td>"+
	    		"<td><div class=\"select i_s_240 mar_cen\"> <span class=\"sel_text\" code=\""+info.mainRoleName+"\">"+info.mainRoleName+"</span><div class=\"sel_list i_ul_265\" style=\"display:none;\">" +
	    				" <ul>"+empRoleLi+"</ul> </div></div></td>" +
	    	    "</tr>";
	    		
	    		$("#modelToRoleList").append(tr);
	    		
	    	}
	    	selected();	
	     },
	     error:function(data){
	    	 
	     }
	});
}
function choosEd(flag){
	 
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
	index=$(flag).index();

}

function getModuleAndRoleList(){
	$.ajax({
		 type: "POST",  
	     url: "getModuleAndEmpRoleList.do",
	     data:"",
	     async : false,
	     success:function(data){
	    	var empRoleList = data.empRoleList;
	    	var moduleList = data.moudleList;
	    	for(var i = 0; i < moduleList.length; i++){
	    		var module =  moduleList[i];
	    		moduleLi += "<li id=\""+module.id+"\">"+module.name+"</li>";
	    	}
	    	for(var j = 0; j < empRoleList.length; j++){
	    		var empRole =  empRoleList[j];
	    		empRoleLi +="<li>"+empRole.roleName+"</li>";
	    	}
	    	
	     },
	     error:function(data){
	    	 
	     }
	});
}

function addTr(){
	var tr = "<tr id=\"addnewtr\" onclick=\"choosEd(this)\">"+
	"<input type=\"hidden\" value=\"\" code=\"\" readonly=\"readonly\"/>"+
	"<td><div class=\"select i_s_240 mar_cen\"> <span class=\"sel_text\" code=\"\">请选择</span><div class=\"sel_list i_ul_265\" style=\"display:none;\"><ul>" +
	moduleLi +
	"</ul></div></div></td>"+
	"<td><div class=\"select i_s_240 mar_cen\"> <span class=\"sel_text\" code=\"\">请选择</span><div class=\"sel_list i_ul_265\" style=\"display:none;\">" +
			" <ul>"+empRoleLi+" </ul> </div></div></td>" +
    "</tr>";
	var html = $("#modelToRoleList").html();
	$("#modelToRoleList").html(html+tr);
	selected();	
}

function delTr(){
	
	if(index!=-1){
		if (confirm("是否确认删除?")) {
			var id = $("#modelToRoleList tr:eq("+index+")").attr("id").trim();
			if(id != "addnewtr"){
				$.ajax({
					 type: "POST",  
				     url: "deleteMoudleToRole.do",
				     data:"id="+id,
				     async : false,
				     success:function(data){
				    	 $("#modelToRoleList tr:eq("+index+")").remove();
				    	 index=-1;
				    	 alert("删除成功");
				     },
				     error:function(data){
				    	 alert("删除失败");
				     }
				});
			}else{
				 $("#modelToRoleList tr:eq("+index+")").remove();
		    	 index=-1;
		    	 alert("删除成功");
			}
		}
		
	}else if(index == -1){
		alert("请选择需要删除的模块对应角色");
	}

}

function saveModuleToRole(){
	var flag="1";
	var changeStr  ='';
	var jsonStr= "";
	$("#modelToRoleList").find("span").each(function(){
		if($(this).html()=="请选择"){
			flag="2";
			return false;
		}
	});
	if(flag == "2"){
		alert("所有设置均不能为空，请仔细核对");
		return;
	}
	
	$("#modelToRoleList").find("tr").each(function(){
		var changeFlag = "1";
		$(this).find("span").each(function(){
			if($(this).html()!= $(this).attr("code")){
				changeFlag ="2";
				return false;
			}
		});
		if(changeFlag =="2"){
			var id=$(this).attr("id");
			if(id =='addnewtr'){
				id='';
			}
			var mainRoleName =$(this).find("span:eq(1)").html().trim();
			var pubRoleId =$(this).find("input:eq(0)").val().trim();
			
			changeStr +='{"id":"'+id+'","mainRoleName":"'+mainRoleName+'","pubRoleId":"'+pubRoleId+
			'"},';
		}
		
	
	});
	if(changeStr ==''){
		alert("没有做修改，无需保存");
		return;
	}
	changeStr = "["+changeStr.substring(0,changeStr.length-1)+"]";
	jsonStr = '{"ids":"","empMainRoleList":' + changeStr+'}';
	
	$.ajax({
		 type: "POST", 
		 contentType:"application/json; charset=UTF-8",
	     url: "saveMoudleToRole.do",
	     data:jsonStr,
	     dataType:"json",
	     success:function(data){
	    	 alert("保存成功");
	    	 SkipByPost("showModuleToRole.do","");
	     },
	     error:function(data){
	    	alert(data) ;
	     }
	});
}


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 (){
		$(".select").addClass("sel_hov");		
	},function (){
		$(this).removeClass("sel_hov");	
	}).on('click',function(){
		var _this = $(this).html();
		var _id= $(this).attr("id");
		var flag="1";
		//判断是否为模块
		if(_id != undefined){
			var roleName =  $("#modelToRoleList tr:eq("+index+")").find("span:eq(1)").html();
			$("#modelToRoleList").find("tr").each(function(i,n){
				if( i != index){
					if(_this == $(this).find("span:eq(0)").html() && roleName == $(this).find("span:eq(1)").html()){
						flag="2";
						return false;
					}
				}
			});
			
			if(flag =="2"){
				alert("已存在相同的模块角色对应关系,请确认");
				return;
			}else{
				var _thisText = $(this).closest(".select").find(".sel_text");
				_thisText.html(_this);
				var _thisRoleId = $("#modelToRoleList tr:eq("+index+")").find("input:eq(0)");
				_thisRoleId.val(_id);
			}
		}else{
			var moduleName =  $("#modelToRoleList tr:eq("+index+")").find("span:eq(0)").html();
			$("#modelToRoleList").find("tr").each(function(i,n){
				if( i != index){
					if(moduleName == $(this).find("span:eq(0)").html() && _this == $(this).find("span:eq(1)").html()){
						flag="2";
						return false;
					}
				}
			});
			if(flag =="2"){
				alert("已存在相同的模块角色对应关系,请确认");
				return;
			}else{
				var _thisText = $(this).closest(".select").find(".sel_text");
				_thisText.html(_this);
			}
		}
	});
}
