/*2014-05-30 add by yb
 * describe:菜单管理
 * 
 */
var depttree = null;
var treecodeStart=0;
var newCount = 1;
var className = "dark";
//菜单树setting配置
var setting = {
	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 addHoverDom(treeId, treeNode) {
	var sObj = $("#" + treeNode.tId + "_span");
	if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0 || treeNode.level==2) 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("tree");
		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 beforeDrag(treeId, treeNodes) {
	return false;
}

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

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("tree");
	zTree.selectNode(treeNode);
	return confirm("进入--" + treeNode.name + " 的编辑状态吗？");
}

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

//捕获删除节点之后的事件回调函数
function onRemove(e, treeId, treeNode) {
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/busi/property/deleVirtualArch",
		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("tree");
		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){
	$("#vname").val(treeNode.name);
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/busi/property/getvirtualArchInfo",
		type : "POST",
		data : {treeCode:treeNode.id, parentCode:treeNode.pId},
		dataType:"json",
		success : function(data) {
			if(data!=null&&data!=""){
				$("#vname").val(data.name);
				$("#type").val(data.type);
				$("#remark").val(data.remark);
				$("#codeNumber").val(data.codeNumber);
				if(data.parentCode=='0'){
					$("#customerDiv").show();
					$("#phoneDiv").show();
					$("#addressDiv").show();
					$("#customerNumber").val(data.customerNumber);
					$("#phone").val(data.phone);
					if(data.address=="" || data.address==null){
						$("#address").val($("#organizationAddress").val());
					}else{
						$("#address").val(data.address);
					}
				}else{
					$("#customerDiv").hide();
					$("#phoneDiv").hide();
					$("#addressDiv").hide();
				}
			}
			else{
				$("#vname").val("");
				$("#type").val("");
				$("#remark").val("");
			}
		},
		error : function(data) {
		}
	});
}

//TODO 加载树
function initTree(){
	var contextPath=$("#contextPath").val();
	var organizationId=$("#organizationId").val();
	var organizationName=$("#organizationName").val();
	$.ajax({
		url :contextPath+"/busi/property/virtualArchTree",
		type : "POST",
		data :{organizationId:organizationId,organizationName:organizationName},
		dataType:"json",
		success : function(data) {
			$.fn.zTree.init($("#tree"), setting, data);
			ztree=$.fn.zTree.getZTreeObj("tree");
			ztree.expandAll(true);
			
			var nodes = ztree.getNodes();
			ztree.selectNode(nodes[0]);
		},
		error : function(data) {
			// alert("系统繁忙,加载失败..");
		}
	});
}

function saveOrUpdate(treeNode,actType,type,remark,organizationId,codeNumber,customerNumber,phone,address){
	organizationId=$("#organizationId").val();
	if(type==""||type==undefined){
		var type=treeNode.level;
	}
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/busi/property/addOrUpdateVirtualArch",
		type : "POST",
		data :{treeCode:treeNode.id,parentCode:treeNode.pId,name:treeNode.name,
			actType:actType,type:type,remark:remark,organizationId:organizationId,codeNumber:codeNumber,
			customerNumber:customerNumber,phone:phone,address:address},
		dataType:"json",
		success : function(data) {
			if(data.errcode=="200"){
				alert("保存成功.");
			}
			else{
				alert("保存失败."+data.errcode);
			}
		},
		error : function(data) {
			// alert("系统繁忙,保存失败");
		}
	});
}

$(function() {
	//初始化菜单树
	initTree();
	var treeCount=$("#totalcount").val();
	if(treeCount!=null&&treeCount!=""){
		treecodeStart= parseInt(treeCount);
	}
	//编辑提交
	$("#btn-save").bind("click", function() {
		var selectnode=ztree.getSelectedNodes();            //获取左侧菜单树选中node
		var type=$("#type").val();
		var remark=$("#remark").val();
		var codeNumber=$("#codeNumber").val();
		var customerNumber=$("#customerNumber").val();
		var phone=$("#phone").val();
		var address=$("#address").val();
		var reg = /^\s*$/g;
		if(selectnode.length==0||selectnode[0].id=="0"){
			alert("请在左侧选择组织");
			return;
		}
		if(reg.test(type)){
			alert("请选择类型");
			return;
		}
		if(selectnode[0].pId==0){
			if(reg.test(customerNumber)){
				alert("请输入唯一编号");
				return;
			}
			if(reg.test(phone)){
				alert("请输入企业电话");
				return;
			}
			var regPhone=/^(^(\d{2,4}[-_－—]?)?\d{3,8}([-_－—]?\d{3,8})?([-_－—]?\d{1,7})?$)|(^0?1[35]\d{9}$)$/;
			 if(!regPhone.test(phone)){
				alert("电话号码有误 ，请重填");
				return;
			 }
			if(reg.test(address)){
				alert("请填写地址");
				return;
			}
		}
		saveOrUpdate(selectnode[0],"update",type,remark,"",codeNumber,customerNumber,phone,address);
	});
});

