//角色树操作
var zNodes;
var menu = {
setting: {
		view:{
			fontCss : {"font-family": "微软雅黑","color":"blue"}
		},
		data:{
				simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",

				},
		},
		callback: { //回调函数
			onClick: zTreeOnClick }
		},
		loadMenuTree:function(){
				var key = $("#key").val();
				$.ajax({
						type: "post",
						url:path+"/menuPower/getRolePowerTree",
						data:{key:key},
						dataType:"json",
						success:function(data){
							$.fn.zTree.init($("#ztree"), menu.setting, data);
							zTree = $.fn.zTree.getZTreeObj("ztree");
							if(key == ''){
								var nodes = zTree.getNodes();
								zTree.expandNode(nodes[0], true);
							}else{
								zTree.expandAll(true);
							}
							/*
							for (var i = 0; i < nodes.length; i++) { //设置节点展开
								zTree.expandNode(nodes[i], true, false, true);
							}*/
							zNodes = data;
							$('#loadingModal').modal('hide');
						}
				})
		}
};
//加载角色树
$().ready(function(data){
	menu.loadMenuTree();
	//fuzzySearch('ztree','#key',null,true,menu.setting,zNodes); //初始化模糊搜索方法
});
function reloadTree() {
	$('#loadingModal').modal('show');
	menu.loadMenuTree();
}
//角色树点击事件
var sysZnodes;
function zTreeOnClick(event, treeId, treeNode) {
	$("#block").empty();
	$("#sysFlag").val("");
	//系统id
	var sysFlag = treeNode.sysFlag;
	//角色id
	var roleId = treeNode.selfId;
	if(roleId != null){
		$.ajax({
			type: "post",
			url:path+"/menuPower/getMenuBySysIdAndRoleIdPowerTree",
			data:{sysFlag:sysFlag,roleId:roleId},
			dataType:"json",
			success:function(data){
				var menuData = [];
				var methodData = [];
				if(data != null && data != ''){
					for(var i = 0; i < data.length; i++) {
						if(data[i].method == '' || data[i].method == null){
							//菜单
							menuData.push(data[i]);
						}else{
							//方法
							methodData.push(data[i]);
						}
					}
					if(null != menuData){
						sysZnodes = menuData;
						//初始化菜单树
						initSysMenuTree();
						//选中节点的menuId赋值到隐藏域
						var menus = '';
						for(var i = 0; i < menuData.length; i++) {
							if(menuData[i].checked == 'true'){
								menus += menuData[i].menuId + ",";
							}
						}
						$("#menus").val(menus);
					}
					$("#roleId").val(roleId);
					$("#sysFlag").val(sysFlag);
					//赋值方法区隐藏复选框
					if(null != methodData && methodData.length > 0){
						var html = '';
						var parentIds = [];
						for(var i = 0; i < methodData.length; i++) {
							parentIds.push(methodData[i].parentId);
						}
						//父id去重
						parentIds = array_remove_repeat(parentIds);
						for(var i = 0; i < parentIds.length; i++) {
							var methods = '';
							var method = [];
							for(var j = 0; j < methodData.length; j++) {
								if(methodData[j].parentId == parentIds[i]){
									method.push(methodData[j]);
								}
								//选中节点的方法demenuId赋值到隐藏域
								if(methodData[j].checked == 'true'){
									methods += methodData[j].menuId + ',';
								}
							}
							if(method != null){
								html += appendHtmlTotal(method,treeNode.menuId);
							}
							$("#methods").val(methods);
						}
						$("#block").append(html);
					}
				}else{
					$("#ztree1").empty();
					$("#roleId").val('');
				}
			}
		})
	}else{
		toastr.warning("请选中角色！");
	}
};

//菜单树操作
var setting = {
	check: {
		enable: true
		},
	view:{
			fontCss : {"font-family": "微软雅黑","color":"blue"}
		},
	data:{
		simpleData: {
		enable: true,
		idKey: "menuId",
		pIdKey: "parentId",
	},
	key:{
		name:"menuName"
	}
	},
	callback: { //回调函数
		onClick: menuZTreeOnClick,
		onCheck:onCheck}
};
//菜单点击事件
function menuZTreeOnClick(event, treeId, treeNode) {
	var menuId = treeNode.menuId;
	//原来的divIndex隐藏
	var divIndex = $("#divIndex").val();
	$("#methodDiv"+divIndex).hide();
	//改节点为父节点的方法显示
	$("#methodDiv"+menuId).show();
	$("#divIndex").val(menuId);
}
//菜单树复选框选中事件
function onCheck(e,treeId,treeNode){
	var treeObj=$.fn.zTree.getZTreeObj("ztree1");
	truenodes=treeObj.getCheckedNodes(true);
	falsenodes=treeObj.getCheckedNodes(false);
	var divIndex = $("#divIndex").val();
	var isExist;
	var menus = '';
	if(null != truenodes){
		for(var i=0;i<truenodes.length;i++){
			//获取树选中节点的menu集合
			var menu = truenodes[i].menuId;
			menus += menu + ",";
		}
	}
	childNodeTrue(treeNode.menuId);
	//菜单id集合赋值到隐藏域
	$("#menus").val(menus);
	if(falsenodes != null){
		for(var i = 0; i < falsenodes.length; i++) {
			//方法区全不选
			$("#methodDiv"+falsenodes[i].menuId+" input[type='checkbox']").each(function(){
				$(this).prop("checked",false);
			});
		}
	}
}
//保存角色菜单权限
function addMenu(){
	var methods = '';
	 $("input[name='methodId']:checked").each(function(){
		methods += $(this).val() + ",";
	})
	var menus = ztree.getCheckedNodes(true);
	var menu = '';
	for(var i = 0; i < menus.length; i++) {
		menu += menus[i].menuId + ",";
	}
	$("#menus").val(menu+methods);
	var roleId = $("#roleId").val();
	if(roleId != null && roleId != ''){
		$.ajax({
			type:'post',
			url:path+"/menuPower/addRoleMenu",
			data:{roleId:$("#roleId").val(),sysFlag:$("#sysFlag").val(),menus:$("#menus").val()},
			dataType:"json",
			success:function(data){
				if(data.code == 100){
					toastr.success("保存成功!");
				}else{
					toastr.error(data.msg);
				}
			}
		})
	}else{
		toastr.warning("请先选中角色！")
	}
}