/*2014-05-30 add by yb
 * describe:菜单管理
 * 
 */
var depttree = null;
var treecodeStart=0;
var newCount = 1;
var className = "dark";
//菜单树setting配置
var setting1 = {
	view: {
		addHoverDom: addHoverDom,
		removeHoverDom: removeHoverDom,
		selectedMulti: false
	},
	edit: {
		enable: true,
		editNameSelectAll: true,
		showRemoveBtn: showRemoveBtn,
		showRenameBtn: showRenameBtn
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		beforeDrag: beforeDrag,
		beforeEditName: beforeEditName,
		beforeRemove: beforeRemove,
		beforeRename: beforeRename,
		onRemove: onRemove,
		onRename: onRename,
		onClick: onClick_Info
	}
};

function beforeDrag(treeId, treeNodes) {
	return false;
}

function showRenameBtn(treeId, treeNode){
	if(treeNode.id=="0"){
		return false;
	}
	else{
		return true;
	}
}

//新增节点
function addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
	var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
		+ "' title='新增部门' onfocus='this.blur();'></span>";
	sObj.after(addStr);
	var btn = $("#addBtn_"+treeNode.tId);
	if (btn) btn.bind("click", function(){
		var zTree = $.fn.zTree.getZTreeObj("depttree");
		var treeid=treecodeStart + newCount;
		var name="部门 " + (newCount++);
		zTree.addNodes(treeNode, {id:treeid, pId:treeNode.id, name:name});
		
		var newNode={id:treeid, pId:treeNode.id, name:name};
		saveOrUpdate(newNode,"add");
		return false;
	});
};

function removeHoverDom(treeId, treeNode) {
	$("#addBtn_"+treeNode.tId).unbind().remove();
};

function showRemoveBtn(treeId, treeNode) {
	if(treeNode.isParent||treeNode.id=="0"){
		return false;
	}
	else{
		return true;
	}
	//return !treeNode.isFirstNode;
}

//编辑按钮的 click 事件
function beforeEditName(treeId, treeNode) {
	className = (className === "dark" ? "":"dark");
	var zTree = $.fn.zTree.getZTreeObj("depttree");
	zTree.selectNode(treeNode);
	return confirm("进入节点 -- " + treeNode.name + " 的编辑状态吗？");
}

//捕获节点被删除之前的事件
function beforeRemove(treeId, treeNode) {
	className = (className === "dark" ? "":"dark");
	var zTree = $.fn.zTree.getZTreeObj("depttree");
	zTree.selectNode(treeNode);
	return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
}

//捕获删除节点之后的事件回调函数
function onRemove(e, treeId, treeNode) {
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/admin/organization/ajaxDeleteDept",
		type : "POST",
		data : {treeCode:treeNode.id, parentCode:treeNode.pId},
		dataType:"json",
		success : function(data) {
			if(data!=null&&data.errcode=="200"){
				alert("菜单 -"+treeNode.name+" 删除成功");
			}
			else{
				alert("删除失败."+data.errcode +" 请刷新");
			}
		},
		error : function(data) {
			alert("删除失败."+data.errcode +" 请刷新");
		}
	});
}

//捕获节点编辑名称结束（Input 失去焦点 或 按下 Enter 键）
function beforeRename(treeId, treeNode, newName, isCancel) {
	className = (className === "dark" ? "":"dark");
	if (newName.length == 0) {
		alert("节点名称不能为空.");
		var zTree = $.fn.zTree.getZTreeObj("depttree");
		setTimeout(function(){zTree.editName(treeNode)}, 10);
		return false;
	}
	return true;
}

//用于捕获节点编辑名称结束之后的事件回调函数
function onRename(e, treeId, treeNode, isCancel) {
	if(!isCancel){
		saveOrUpdate(treeNode,"update");
	}
}

//查询节点基本信息
function onClick_Info(e, treeId, treeNode, isCancel){
	$("#menuName").val(treeNode.name);
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/admin/organization/ajaxGetDeptInfo",
		type : "POST",
		data : {treeCode:treeNode.id, parentCode:treeNode.pId},
		dataType:"json",
		success : function(data) {
			if(data!=null&&data!=""){
				$("#deptname").val(data.deptName);
				$("#description").val(data.description);
				$("#remark").val(data.remark);
			}
			else{
				$("#deptname").val("");
				$("#description").val("");
				$("#remark").val("");
			}
		},
		error : function(data) {
		}
	});
	
}

//TODO 加载部门树
function initdepttree(){
	var contextPath=$("#contextPath").val();
	var compId=$("#compId").val();
	var compName=$("#compName").val();
	$.ajax({
		url :contextPath+"/admin/organization/depttree",
		type : "POST",
		data :{compId:compId,compName:compName},
		dataType:"json",
		success : function(data) {
			$.fn.zTree.init($("#depttree"), setting1, data);
			depttree=$.fn.zTree.getZTreeObj("depttree");
			depttree.expandAll(true);
		},
		error : function(data) {
			// alert("系统繁忙,加载失败..");
		}
	});
}

function saveOrUpdate(treeNode,actType,description,remark,compId){
	compId=$("#compId").val();
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/admin/organization/addOrUpdateDept",
		type : "POST",
		data :{treeCode:treeNode.id,parentCode:treeNode.pId,name:treeNode.name,
			actType:actType,description:description,remark:remark,compId:compId},
		dataType:"json",
		success : function(data) {
			if(data.errcode=="200"){
				alert("保存成功.");
			}
			else{
				alert("保存失败."+data.errcode);
			}
		},
		error : function(data) {
			// alert("系统繁忙,保存失败");
		}
	});
}

$(function() {
	//初始化菜单树
	initdepttree();
	
	var depttreeCount=$("#totalcount").val();
	if(depttreeCount!=null&&depttreeCount!=""){
		treecodeStart= parseInt(depttreeCount);
	}
	
	//编辑提交
	$("#btn-save").bind("click", function() {
		var selectnode=depttree.getSelectedNodes();            //获取左侧菜单树选中node
		var description=$("#description").val();
		var remark=$("#remark").val();
		
		if(selectnode.length==0||selectnode[0].id=="0"){
			alert("请选择部门");
			return;
		}
		saveOrUpdate(selectnode[0],"update",description,remark,"");
	});
	
});

