var ids = null;	
var addWinWidth = 0;  //新增窗口宽度
/**
 * 初始化
 */
$(function() {
	
	addWinWidth = (document.body.clientWidth/100)*80  
	initData();
	initCombobox();
});

function initCombobox(){
	$('#roleNameId').combobox({
		url : '/admin/role/getRolesList',
		valueField : 'id',
		textField : 'text',
		panelHeight: "auto",
		multiple:false,   //是否多选
		editable : false, //不允许手动输入
		width : fixShortWidth(),
//		onLoadSuccess : function(){  //数据加载完毕事件
//			var data = $("#roleNameId").combobox('getData');
//			if(data.length>0){
//				$("#roleNameId").combobox('setValue',data[0].text);
//			}
//		}
		onSelect : function(record){
			if(null != ids){
				for(var i=0;i<ids.length;i++){
					var node = $("#tt").tree('find',ids[i]);
					if(null != node){
						$("#tt").tree('uncheck',node.target);
					}
				}
			}
			$.getJSON(url = '/admin/privilegeManage/queryPriurlByRoleId?roleId=' + record.id,function(data){
				ids = data.flag;
				if(null != ids){
					for(var i=0;i<ids.length;i++){
						var node = $("#tt").tree('find',ids[i]);
						if(null != node){
							$("#tt").tree('check',node.target);
						}
					}
				}
			});
		}
	});
	
}

function initData(){
	//初始化菜单树
	$('#tt').tree( {
		url : '/admin/privilegeManage/getLeftTree',
		checkbox : true,
		cascadeCheck : false,
		onLoadSuccess:function(node,data) {
			//加载成功后选中根节点
	 		$('#tt').tree('select',$('#tt').tree('getRoot').target);
		},
		onBeforeExpand : function(node,param){
		},
		onClick : function(node) {
//			url = '/admin/privilegeManage/queryOrgById?orgId=' + node.id;
//			$.post(url, function(bo) {
//				$('#right').form('load', bo);
//			}, 'json');
		}
	});
}
	
	
// 保存
function saveData(){
	var nodes = $('#tt').tree('getChecked');
	var roleId = $("#roleNameId").combobox('getValue');
	var s = '';
	var parentNode = null;
	var node = null;
	for(var i=0; i<nodes.length; i++){
		if (s != '') s += ',';
		if(1 != nodes[i].id){
			node = $("#tt").tree('find',nodes[i].id);
			parentNode = $("#tt").tree('getParent',node.target);
			s += nodes[i].priUrl+","+ parentNode.priUrl;
		}
	}
	if("" != roleId){
		$.getJSON(url = '/admin/role/addRolePri?roleId='+roleId+"&priIds="+s,function(data){
			if(data.success){
				$.messager.alert('提示', '权限分配成功！', 'ok');
			}else {
				$.messager.alert('提示', '权限分配失败', 'error');
			}
		});
	}else {
		$.messager.alert('提示', '请选择要进行授权的角色', 'info');
	}
	/*$("#addRolePriForm").form('submit',{
		url : '/admin/role/addRolePri?roleId='+roleId+"&priIds="+s,
		onsubmit : function(){
			var bool=$(this).form('validate');
			if(bool==false){
				enableButtion("savebutton");
			}
			return bool;
		},
		success : function(result){
			var obj = eval("("+result+")");
			if (obj.flag) {
				showAlert("提示", obj.info, 'info', function() {
//				initData();
				});
			} else {
				showAlert("提示", obj.info, 'error');
			}
		}
	}); */
}
	
	
//修改
function editMenu(){
	var row = $('#tt').tree('getSelected');
	if (row != null){
		$.getJSON(url = './queryMenuById.do?menuId=' + row.id,function(data){
			$("#addMenuForm").form('load',data);
		});
		$("#addMenuId").window({
			width:900,
			height:300 
		});
		$("#addMenuId").window('open');
	}else{ 
		$.messager.alert('提示', '请选中一条记录', 'info');
		//$.调用jquery自己的内置对象
	}
}
	
	
	
var clientWidth = document.documentElement.clientWidth;
var clientHeight = document.documentElement.clientHeight;
$(document).ready(function(){
	//左侧div宽度
 	var div_left_width = $(".panel-header").width()+18;
	my_resize(div_left_width);
	$(window).resize(function(){
		clientHeight = document.documentElement.offsetHeight;
		clientWidth = document.documentElement.offsetWidth;
		div_left_width = $(".panel-header").width()+18;
		my_resize(div_left_width);
	});
	//自动适应页面大小
	function my_resize(panelWidth){
		$("#cc").css("width",clientWidth);
		$("#cc").css("height",clientHeight);
		//$("#cc").layout('resize',{height:clientHeight,width:clientWidth});
		$('#left').panel('resize',{height:clientHeight,width:div_left_width});
		$('#right').panel('resize',{height:clientHeight,width:clientWidth - panelWidth});
		
	}

	$(".layout-button-left").hide();
	$(".layout-button-left").bind("click",function(){
		my_resize(div_left_width-21);
		clientWidth = document.documentElement.clientWidth;
		$(".layout-button-right").bind("click",function(){
			var temp = 0-div_left_width+145;
			my_resize(temp);
			clientWidth = document.documentElement.clientWidth;
		});
	});
});
	
//设置 short 宽度自适应
function fixShortWidth(){   
	return addWinWidth*0.20;           
} 