/*2014-05-30 add by yb
 * describe:左侧菜单查询
 * 
 */
var menutree = null;
//菜单树setting配置
var setting = {
	view : {
		dblClickExpand : false,
		showLine : false
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback : {
		beforeExpand: beforeExpand,
		onExpand: onExpand,
		onClick: onClick
	}
};

var curExpandNode = null;
function beforeExpand(treeId, treeNode) {
	var pNode = curExpandNode ? curExpandNode.getParentNode():null;
	var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
	var zTree = $.fn.zTree.getZTreeObj("menus");
	for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
		if (treeNode !== treeNodeP.children[i]) {
			zTree.expandNode(treeNodeP.children[i], false);
		}
	}
	while (pNode) {
		if (pNode === treeNode) {
			break;
		}
		pNode = pNode.getParentNode();
	}
	if (!pNode) {
		singlePath(treeNode);
	}

}
function singlePath(newNode) {
	if (newNode === curExpandNode) return;
	if (curExpandNode && curExpandNode.open==true) {
		var zTree = $.fn.zTree.getZTreeObj("menus");
		if (newNode.parentTId === curExpandNode.parentTId) {
			zTree.expandNode(curExpandNode, false);
		} else {
			var newParents = [];
			while (newNode) {
				newNode = newNode.getParentNode();
				if (newNode === curExpandNode) {
					newParents = null;
					break;
				} else if (newNode) {
					newParents.push(newNode);
				}
			}
			if (newParents!=null) {
				var oldNode = curExpandNode;
				var oldParents = [];
				while (oldNode) {
					oldNode = oldNode.getParentNode();
					if (oldNode) {
						oldParents.push(oldNode);
					}
				}
				if (newParents.length>0) {
					zTree.expandNode(oldParents[Math.abs(oldParents.length-newParents.length)-1], false);
				} else {
					zTree.expandNode(oldParents[oldParents.length-1], false);
				}
			}
		}
	}
	curExpandNode = newNode;
}

function onExpand(event, treeId, treeNode) {
	curExpandNode = treeNode;
}

function onClick(e,treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("menus");
	zTree.expandNode(treeNode, null, null, null, true);
}

/*
function zTreeOnClick(event, treeId, treeNode) {
	menutree.expandNode(treeNode, null, true, false);
};
*/

function modifyPwd(){
	var contextPath=$("#contextPath").val();
	var url=contextPath+"/admin/users/modifyPwd";
	var iTop = (window.screen.height-30-550)/2; //获得窗口的垂直位置;   
	var iLeft = (window.screen.width-10-600)/2; //获得窗口的水平位置;   
	window.open(url,"修改密码",'height=550,innerHeight=550,width=600,innerWidth=600,top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
	
}

//TODO 加载菜单树
function initMenuTree(){
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/admin/ajax/loadMenuTree",
		type : "POST",
		dataType:"json",
		success : function(data) {
			if(data==null||data.length<=0){
				$("#menus").html("你目前没权限菜单");
			}
			else{
				$.fn.zTree.init($("#menus"), setting, data);
				menutree=$.fn.zTree.getZTreeObj("menus");
				//menutree.expandAll(true);
				//记载完列表信息后手动给图标
				$.each($('#menus>li'),function(index, el) {
					var last_dom = $(el).children().eq(1)
					var last_title = $(el).children().eq(1).attr('title')
					var icon = icons(last_title)
					last_dom.css({
						'background-image': 'url('+contextPath+'/images/admin/icons/'+icon+'.png)',
						'background-size': '18px 18px',
						'background-repeat': 'no-repeat',
						'background-position': '31px 16px'
					});
				});
			}
		},
		error : function(data) {
			// alert("系统繁忙,菜单加载失败..");
		}
	});
}
function icons(title){
	var icon = ''
	switch (title) {
	    case '系统管理':
	        icon = "xtgl";
	        break;
	    case '组织架构':
	        icon = "zzjg";
	        break;
	    case '设置中心':
	        icon = "szzx";
	        break;
	    case '基础设置':
	        icon = "jcsz";
	        break;
	    case '消费账单':
	        icon = "xfzd";
	        break;
	    case '公告资讯':
	        icon = "ggzx";
	        break;
	    case '工程维修':
	        icon = "gcwx";
	        break;
	    case '企业入驻':
	        icon = "qyrz";
	        break;
	    case '企业迁出':
	        icon = "qyqc";
	        break;
	    case '电子放行条':
	        icon = "dzfxt";
	        break;
	    case '会议室预约':
	        icon = "hysyy";
	        break;
	    case '小邮局':
	        icon = "xyj";
	        break;
	    case '洗车管理':
	        icon = "xcgl";
	        break;
	    case '宿舍管理':
	        icon = "ssgl";
	        break;
	    case '订餐管理':
	        icon = "dcgl";
	        break;
	    case '空调加时管理':
	        icon = "ktjsgl";
	        break;
	    case '通行管理':
	        icon = "txgl";
	        break;
	    case '统计数据':
	        icon = "sjtj";
	        break;
	    case '问卷调查':
	        icon = "wjdc";
	        break;
	    case '投诉表扬':
	        icon = "tsby";
	        break;
	    case '企业管理':
	        icon = "qygl";
          break;
      case '装修管理':
	        icon = "zxsq";
          break;
      case '报修管理':
	        icon = "bxfw";
	        break;          
	    default: 
	    icon = "xtgl";
	}
	return icon
}
function getOfficeUrl(callback){
	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/admin/other/getPostOfficeUrl",
		type : "POST",
		success : function(data) {
			if(data.code=="200"){
				localStorage.setItem("meetingUrl",data.data.meetingUrl);
				localStorage.setItem("postUrl",data.data.postUrl);
				
				localStorage.setItem("wjdcHeadUrl",data.data.wjdcHeadUrl);
				localStorage.setItem("tsbyHeadUrl",data.data.tsbyHeadUrl);
				
				callback();
			}
			else{
				callback();
				alert(data.msg)
			}
		},
		error : function(data) {
			// alert("系统繁忙");
		}
	});
}
$(function() {
	//初始化菜单树
	
	getOfficeUrl(function(){
		initMenuTree();
	})

	var contextPath=$("#contextPath").val();
	$.ajax({
		url :contextPath+"/admin/ajax/loadRoleUser",
		type : "POST",
		dataType:"json",
		success : function(data) {
			sessionStorage.setItem("passType",data.passType)
			if(data.userType== '-1'){
				$("#userType").show();
			}
		}
	});
});

