var width = -10;
var height = -10;
var setting = {
	view:{
			fontCss : {"font-family": "微软雅黑","color":"blue"}
		},
	data:{
		key:{
			name:"menuName"
		},
		simpleData:{
		enable:true,
		idKey:"menuId",
		pIdKey:"parentId",
	}
	},
	callback: {
		onRightClick: OnRightClick,
		onClick: zTreeOnClick
	}
};
//tree右键点击事件
function OnRightClick(event, treeId, treeNode) {
	if(treeNode != null){
		if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
			zTree.cancelSelectedNode();
			showRMenu("root", event.clientX-width, event.clientY);
		} else if (treeNode && !treeNode.noR) {
			//是否是功能方法
			var isMethod = 0;
			var method = treeNode.method;
			if(method != '' && method != null){
				isMethod = 1;
			}
			//是否有子节点
			var isHaveChildren = 0;
			var childNodes = treeNode.children;
			if(childNodes != undefined){
				isHaveChildren = 1;
			}
			zTree.selectNode(treeNode);
			showRMenu("node", event.clientX-height, event.clientY,isMethod,isHaveChildren,treeNode.menuId);
		}
	}
}

//显示右键菜单
function showRMenu(type, x, y,isMethod,isHaveChildren,menuId) {
	$("#rMenu ul").show();
	if (type=="root") {
		$("#m_del").hide();
		$("#m_add").hide();
		$("#m_reset").hide();
	} else {
		if(isHaveChildren == 0){
			$("#m_del").show();
		}else{
			$("#m_del").hide();
		}
		if(isMethod == 0){
			$("#m_add").show();
		}else{
			$("#m_add").hide();
		}
		if(menuId != 0){
			$("#m_reset").show();
		}else{
			$("#m_reset").hide();
		}
	}
	y += document.body.scrollTop;
	x += document.body.scrollLeft;
	rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});
	$("body").bind("mousedown", onBodyMouseDown);
}
//隐藏右键菜单
function hideRMenu() {
	if (rMenu) rMenu.css({"visibility": "hidden"});
	$("body").unbind("mousedown", onBodyMouseDown);
}

function onBodyMouseDown(event){
	if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
		rMenu.css({"visibility" : "hidden"});
	}
}

function loadSys(){
	$.ajax({
		type:'get',
		url:path+"/menu/loadSys",
		dataType:'json',
		async:false,
		success:function(data){
			$("#sysFlag").empty();
			var html = "";
			if(data != null){
				for (var i = 0; i < data.length; i++) {
					html += '<option value="'+data[i].sysFlag+'">'+data[i].sysName+'</option>';
				}
				$("#sysFlag").append(html);
			}
		}
	})
}
//添加节点
function addTreeNode() {
	$("#menuId").val("");
	$("#menuId1").val("");
	document.forms[0].reset();
	document.forms[1].reset();
	hideRMenu();
	var parentId = zTree.getSelectedNodes()[0].menuId;
	var parentName = zTree.getSelectedNodes()[0].newName;
	var sysFlag = zTree.getSelectedNodes()[0].sysFlag;
	var sysId = zTree.getSelectedNodes()[0].sysId;
	var sysFlag = zTree.getSelectedNodes()[0].sysFlag;
	$("#currentTreeId").val(parentId);
	if(parentId == 0){
		loadSys();
		$("#parentName").val("菜单列表");
	}else{
		loadSys();
		$("#sysFlag").val(zTree.getSelectedNodes()[0].sysFlag);
		$("#parentName").val(parentName);
	}
	$("#parentId").val(parentId);
	$("#parentId1").val(parentId);
	$("#parentName1").val(parentName);
	$("#sysId1").val(sysId);
	$("#sysFlag1").val(sysFlag);
	var method = zTree.getSelectedNodes()[0].method;
	if(method != null && method != ''){
		$("#menuDiv").hide();
		$("#methodDiv").hide();
	}else{
		var children = zTree.getSelectedNodes()[0].children;
		var childrenNames = '';
		var childrenNodeMethods = '';
		if(null != children && children.length > 0){
			for(var i = 0; i < children.length; i++) {
				var childrenNodeName = children[i].newName;
				childrenNames += childrenNodeName + ",";
				var childrenNodeMethod = children[i].method;
				childrenNodeMethods += childrenNodeMethod + ",";
			}
		}
		$("#childrenNodeNames").val(childrenNames);
		$("#childrenNodeMethods").val(childrenNodeMethods);
		var menuUrl = zTree.getSelectedNodes()[0].menuUrl;
		if(menuUrl == '' || menuUrl == null || menuUrl == '#'){
			$("#menuDiv").show();
			$("#methodDiv").hide();
		}else{
			$("#menuDiv").hide();
			$("#methodDiv").show();
		}
	}
}
//删除节点
function removeTreeNode() {
	hideRMenu();
	var nodes = zTree.getSelectedNodes();
	if (nodes && nodes.length>0) {
		if (nodes[0].children && nodes[0].children.length > 0) {
			var msg = "要删除的节点是父节点，先从子节点删除";
			confirm(msg);
		}else {
			Common.confirm({
				title: "提示",
				message: "确定是否删除此条记录?",
				operate: function (reselt) {
					if (reselt) {
						var menuId = nodes[0].menuId;
						$.ajax({
							type: "post",
							url: path + "/menu/delete/" + menuId,
							dataType: 'json',
							success: function (data) {
								if (data.msg == 'success') {
									toastr.success("删除成功！");
								} else {
									toastr.error("删除失败！");
								}
								setTimeout(function(){
									window.location.reload();
								},500)
							}
						})
					}
				}
			})
		}
	}
}
function checkTreeNode(checked) {
	var nodes = zTree.getSelectedNodes();
	if (nodes && nodes.length>0) {
		zTree.checkNode(nodes[0], checked, true);
	}
	hideRMenu();
}
//更新节点
function updateTree() {
	$("#currentTreeId").val(zTree.getSelectedNodes()[0].menuId);
	hideRMenu();
	var nearNode;
	//获取相邻节点的所有
	if(zTree.getSelectedNodes()[0].getParentNode() != null){
		nearNode = zTree.getSelectedNodes()[0].getParentNode().children;
	}
	var method = zTree.getSelectedNodes()[0].method;
	var nearNodeNames = '';
	var nearNodeMethods = '';
	if(null != nearNode && nearNode.length > 0){
		for(var i = 0; i < nearNode.length; i++) {
			var childrenNodeName = nearNode[i].menuName;
			if(childrenNodeName != zTree.getSelectedNodes()[0].menuName){
				nearNodeNames += childrenNodeName + ",";
			}
			var nearNodeMethod = nearNode[i].method;
			if(nearNodeMethod != method){
				nearNodeMethods += nearNodeMethod + ",";
			}
		}
	}
	$("#nearNodeNames").val(nearNodeNames);
	$("#nearNodeMethods").val(nearNodeMethods);
	if(method != null && method != ''){
		//修改方法，回显方法的所有数据
		$("#menuId1").val(zTree.getSelectedNodes()[0].menuId);
		$("#sysId1").val(zTree.getSelectedNodes()[0].sysId);
		$("#sysFlag1").val(zTree.getSelectedNodes()[0].sysFlag);
		$("#menuName1").val(zTree.getSelectedNodes()[0].newName);
		$("#method").val(method);
		$("#functionType").val(zTree.getSelectedNodes()[0].functionType);
		var methodType = zTree.getSelectedNodes()[0].methodType;
		$("#methodType").val(methodType);
		$("#parentId1").val(zTree.getSelectedNodes()[0].parentId);
		var parentNode = zTree.getSelectedNodes()[0].getParentNode();
		if(parentNode != undefined){
			$("#parentName1").val(parentNode.newName);
		}
		$("#sort1").val(zTree.getSelectedNodes()[0].sort);
		$("#effective1").val(zTree.getSelectedNodes()[0].effective);
		$("#remark1").val(zTree.getSelectedNodes()[0].remark);
		document.forms[0].reset();
		$("#methodDiv").show();
		$("#menuDiv").hide();
	}else{
		//修改菜单，回显菜单的所有数据
		$("#menuId").val(zTree.getSelectedNodes()[0].menuId);
		$("#menuName").val(zTree.getSelectedNodes()[0].newName);
		var menuIcon = zTree.getSelectedNodes()[0].menuIcon;
		$("#menuIcon").val(menuIcon);
		$("#iconSpan").removeClass();
		$("#iconSpan").addClass(menuIcon + " iconSpan");
		$("#menuUrl").val(zTree.getSelectedNodes()[0].menuUrl);
		loadSys();
		$("#sysFlag").val(zTree.getSelectedNodes()[0].sysFlag);
		var parentId = zTree.getSelectedNodes()[0].parentId;
		if(parentId == null){
			parentId = 0;
		}
		$("#parentId").val(parentId);
		var parentNode = zTree.getSelectedNodes()[0].getParentNode();
		if(parentNode.menuId != 0){
			$("#parentName").val(parentNode.newName);
		}else{
			$("#parentName").val('菜单列表');
		}
		$("#sort").val(zTree.getSelectedNodes()[0].sort);
		$("#effective").val(zTree.getSelectedNodes()[0].effective);
		$("#remark").val(zTree.getSelectedNodes()[0].remark);
		document.forms[1].reset();
		$("#menuDiv").show();
		$("#methodDiv").hide();
	}
}
//左键点击事件
function zTreeOnClick(event, treeId, treeNode) {
	var menuId = zTree.getSelectedNodes()[0].menuId;
	if(menuId == 0){
		toastr.warning("请选择菜单!");
		$("#menuDiv").hide();
		$("#methodDiv").hide();
	}else{
		$("#currentTreeId").val(menuId);
		var menuDesc = treeNode.menuDesc;
		if(menuDesc != null && menuDesc != '' && menuDesc == '功能'){
			//修改方法，回显方法的所有数据
			$("#sysId1").val(zTree.getSelectedNodes()[0].sysId);
			$("#sysFlag1").val(zTree.getSelectedNodes()[0].sysFlag);
			$("#menuId1").val(zTree.getSelectedNodes()[0].menuId);
			$("#menuName1").val(zTree.getSelectedNodes()[0].newName);
			$("#method").val(zTree.getSelectedNodes()[0].method);
			$("#functionType").val(zTree.getSelectedNodes()[0].functionType);
			var methodType = zTree.getSelectedNodes()[0].methodType;
			$("#methodType").val(methodType);
			$("#parentId1").val(zTree.getSelectedNodes()[0].parentId);
			var parentNode = zTree.getSelectedNodes()[0].getParentNode();
			if(parentNode != undefined){
				$("#parentName1").val(parentNode.newName);
			}
			$("#sort1").val(zTree.getSelectedNodes()[0].sort);
			$("#effective1").val(zTree.getSelectedNodes()[0].effective);
			$("#remark1").val(zTree.getSelectedNodes()[0].remark);
			document.forms[0].reset();
			$("#methodDiv").show();
			$("#menuDiv").hide();
		}else{
			//修改菜单，回显菜单的所有数据
			$("#menuId").val(zTree.getSelectedNodes()[0].menuId);
			$("#menuName").val(zTree.getSelectedNodes()[0].newName);
			var menuIcon = zTree.getSelectedNodes()[0].menuIcon;
			$("#menuIcon").val(menuIcon);
			$("#iconSpan").removeClass();
			$("#iconSpan").addClass(menuIcon + " iconSpan");
			$("#menuUrl").val(zTree.getSelectedNodes()[0].menuUrl);
			loadSys();
			$("#sysFlag").val(zTree.getSelectedNodes()[0].sysFlag);
			var parentId = zTree.getSelectedNodes()[0].parentId;
			if(parentId == null){
				parentId = 0;
			}
			$("#parentId").val(parentId);
			var parentNode = zTree.getSelectedNodes()[0].getParentNode();
			if(parentNode.menuId != 0){
				$("#parentName").val(parentNode.newName);
			}else{
				$("#parentName").val('菜单列表');
			}
			$("#sort").val(zTree.getSelectedNodes()[0].sort);
			$("#effective").val(zTree.getSelectedNodes()[0].effective);
			$("#remark").val(zTree.getSelectedNodes()[0].remark);
			document.forms[1].reset();
			$("#menuDiv").show();
			$("#methodDiv").hide();
		}
	}
}
//异步加载树
var zTree, rMenu,zNodes;
function loadTree(){
	$.ajax({
		type: "post",
		url: path+"/menu/getMenuList",
		dataType:'json',
		success:function(data){
			if(data != ''){
				$.fn.zTree.init ($("#treeDemo"), setting, data);
				zTree = $.fn.zTree.getZTreeObj("treeDemo");
				rMenu = $("#rMenu");
				var nodes = zTree.getNodes();
				//for (var i = 0; i < nodes.length; i++) { //设置节点展开
					zTree.expandNode(nodes[0], true, false, true);
				//}
				zNodes = data;
				$('#loadingModal').modal('hide');
			}else{
				$("#parentId").val(0);
			}
		}
	})
}
$(document).ready(function(){
	loadTree();
	fuzzySearch('treeDemo','#key',null,true,setting,zNodes);
});

//验证同一系统菜单名不能重复
$("#menuName").blur(function () {
	var menuName = $("#menuName").val();
	var menuId = $("#menuId").val();
	var sysFlag = $("#sysFlag").val();
	if(menuId == '' && menuName != ''){
		$.ajax({
			type:'get',
			url:path+'/menu/checkMenuName',
			data:{menuName:menuName,sysFlag:sysFlag},
			dataType:'json',
			success:function (data) {
				if(data.code == 200){
					toastr.warning("菜单名不能重复!");
					$("#menuName").val("");
					$("#menuName").focus();
				}
			}
		})
	}
})
//验证同一菜单功能名不能重复
$("#menuName1").blur(function () {
	var menuName = $("#menuName1").val();
	var menuId = $("#menuId1").val();
	var parentId = $("#parentId1").val();
	if(menuId == '' && menuName != ''){
		$.ajax({
			type:'get',
			url:path+'/menu/checkMethodName',
			data:{methodName:menuName,parentId:parentId},
			dataType:'json',
			success:function (data) {
				if(data.code == 200){
					toastr.warning("功能名不能重复!");
					$("#menuName1").val("");
					$("#menuName1").focus();
				}
			}
		})
	}
})
//验证同一菜单方法名称不能重复
$("#method").blur(function () {
	var method = $("#method").val();
	var menuId = $("#menuId1").val();
	var parentId = $("#parentId1").val();
	if(menuId == '' && method != ''){
		$.ajax({
			type:'get',
			url:path+'/menu/checkMethodName',
			data:{method:method,parentId:parentId},
			dataType:'json',
			success:function (data) {
				if(data.code == 200){
					toastr.warning("功能方法已存在!");
					$("#method").val("");
					$("#method").focus();
				}
			}
		})
	}
})
//保存菜单信息
function addMenu() {
	var parentId = $("#parentId").val();
	var sysFlag = $("#sysFlag").val();
	var menuName = $("#menuName").val();
	var menuUrl = $("#menuUrl").val();
	$("#sysName").val($("#sysFlag option:selected").text());
	if(parentId != ''){
		//判断父节点不能为空
		if(sysFlag != ''){
			//判断所属系统不能为空
			if(menuName != ''){
				if(menuUrl != '' || menuUrl == '#'){
					//判断菜单名不能为空
					var menuId = $("#menuId").val();
					//判断添加时是否子节点已存在相同名称
					var isExsit = 0;
					if(menuId == ''){
						var children = $("#childrenNodeNames").val();
						if(null != children && children.length > 0){
							var childrens = children.split(",");
							for(var i = 0; i < childrens.length; i++) {
								if(childrens[i] == menuName){
									isExsit = 1;
								}
							}
						}
					}else{
						//判断修改时是否相邻节点是否超过两个相同名称
						var nearNodeNames = $("#nearNodeNames").val();
						if(null != nearNodeNames && nearNodeNames.length > 0){
							var nearNodeName = nearNodeNames.split(",");
							for(var i = 0; i < nearNodeName.length; i++) {
								if(nearNodeName[i] == menuName){
									isExsit = 1;
								}
							}
						}
					}
					if(isExsit == 0){
						$.ajax({
							type:'post',
							url:path+"/menu/update",
							data:$("#form1").serialize(),
							dataType:"json",
							success:function(data){
								if(data.code == 100){
									toastr.success("保存成功!");
									loadTree();
									setTimeout(function(){
										var currentTreeId = $("#currentTreeId").val();
										var node = zTree.getNodeByParam("menuId",currentTreeId);
										zTree.selectNode(node);
										zTree.expandNode(node, true, false, true);
										if($("#menuId").val() == ''){
											$("#form1")[0].reset();
											$("#parentName").val(node.newName);
										}
									},500)
								}else{
									toastr.warning(data.msg);
								}
							},
							error:function (e) {
							}
						})
					}else{
						toastr.warning("该菜单名称已存在，不能重复添加!");
					}
				}else{
					toastr.warning("菜单url不能为空!");
				}
			}else{
				toastr.warning("菜单名不能为空!");
			}
		}else{
			toastr.warning("所属系统不能为空!")
		}
	}else{
		toastr.warning("先选中父节点!")
	}
}

//保存方法信息
function addMethod() {
	var parentName = $("#parentName1").val();
	var method = $("#method").val();
	var menuName = $("#menuName1").val();
	if(parentName != ''){
		//判断父节点不能为空
		if(method != ''){
			//判断功能方法不能空
			if(menuName != ''){
				//判断功能名不能为空
				var menuId = $("#menuId1").val();
				//判断添加时是否子节点已存在相同名称
				var isExsit = 0;
				if(menuId == ''){
					//判断功能名是否重复
					var children = $("#childrenNodeNames").val();
					if(null != children && children.length > 0){
						var childrens = children.split(",");
						for(var i = 0; i < childrens.length; i++) {
							if(childrens[i] == menuName){
								isExsit = 1;
							}
						}
					}
					//判断功能方法是否重复
					var childrenMethod = $("#childrenNodeMethods").val();
					if(null != childrenMethod && childrenMethod.length > 0){
						var childrenMethods = childrenMethod.split(",");
						for(var i = 0; i < childrenMethods.length; i++) {
							if(childrenMethods[i] == method){
								isExsit = 1;
							}
						}
					}
				}else{
					//判断修改时功能名称是否相邻节点是否存在
					var nearNodeNames = $("#nearNodeNames").val();
					if(null != nearNodeNames && nearNodeNames.length > 0){
						var nearNodeName = nearNodeNames.split(",");
						for(var i = 0; i < nearNodeName.length; i++) {
							if(nearNodeName[i] == menuName){
								isExsit = 1;
							}
						}
					}
					//判断修改时功能方法是否相邻节点是否存在
					var nearNodeMethods = $("#nearNodeMethods").val();
					if(null != nearNodeMethods && nearNodeMethods.length > 0){
						var nearNodeMethod = nearNodeMethods.split(",");
						for(var i = 0; i < nearNodeMethod.length; i++) {
							if(nearNodeMethod[i] == method){
								isExsit = 1;
							}
						}
					}
				}
				if(isExsit == 0){
					$.ajax({
						type:'post',
						url:path+"/menu/update",
						data:$("#form2").serialize(),
						dataType:"json",
						success:function(data){
							if(data.code == 100){
								toastr.success("保存成功!");
								loadTree();
								setTimeout(function(){
									var currentTreeId = $("#currentTreeId").val();
									var node = zTree.getNodeByParam("menuId",currentTreeId);
									zTree.selectNode(node);
									zTree.expandNode(node, true, false, true);
									if($("#menuId1").val() == ''){
										$("#form2")[0].reset();
										$("#parentName1").val(node.newName);
									}
								},500)
							}else{
								toastr.warning(data.msg);
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {
							alert(XMLHttpRequest.status);
							alert(XMLHttpRequest.readyState);
							alert(textStatus);
						},
					})
				}else{
					toastr.warning("该功能名或功能方法已存在，不能重复添加！");
				}
			}else{
				toastr.warning("功能名称不能为空！")
			}
		}else{
			toastr.warning("功能方法不能为空！")
		}
	}else{
		toastr.warning("先选中父节点！")
	}
}

//选择图标
function tijiao() {
	var value = $("input[name='icon']:checked").val();
	$("#menuIcon").val(value);
	$("#iconSpan").removeClass();
	$("#iconSpan").addClass(value + " iconSpan");
	$('#myModal').modal('hide');
}
//左侧树全部展开
function expandAll(){
	var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
	treeObj.expandAll(true);
}
//左侧树全部关闭
function collapseAll(){
	var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
	treeObj.expandAll(false);
}