var divHeight = "";  //查询面板高度
var dataGridWidth = ""; //查询面板宽度
var dataGridHeight = "";
var h = "";   //内容显示窗口高度
var operation = "";
var inputWidth = ""; //输入框宽度
var bodyWidth = 0;
var bodyHeight = 0;
var addWinWidth = 0;  //新增窗口宽度
var addWinHeight = 0;  //新增窗口高度
$(function(){
	$("#addRoleWinID").window('close');
	divHeight = $("#queryDiv").height();
	dataGridWidth = $("#queryDiv").width();
	dataGridHeight = divHeight + 28;
	bodyWidth = document.body.clientWidth;
	bodyHeight = document.body.clientHeight;
	
	bodyWidth = document.body.clientWidth;
	bodyHeight = document.documentElement.clientHeight;
	addWinWidth = (bodyWidth/5)*3;   
	addWinHeight = (addWinWidth/2)*1;  
	
	
	initData();
	
	$("#roleNameId00").attr("style","width:"+ fixShortWidth()+"px");
	$("#roleNameId").attr("style","width:"+ fixShortWidth()+"px");
	$("#urlPerfixsId").attr("style","width:"+ fixShortWidth()+"px");
	$("#descriptionId").attr("style","width:"+ fixShortWidth()+"px");
	
}); 
 
function initData(){
	h = document.body.clientHeight;
	$("#roleListTableID").datagrid({
		height: h - dataGridHeight, //列表高度 
		title : '角色列表',
		url : '/admin/role/getAllRoles',
		nowrap : true,//设置为true，当数据长度超出列宽时将会自动截取  
		striped : true,//设置为true将交替显示行背景。
		loadMsg : '数据装载中......',  
		fitColumns : true,//允许表格自动缩放，以适应父容器  
		singleSelect : false,//是否单选 
		pagination : true,//分页控件 
		rownumbers:true,//行号 
		pageList : [ 15, 20, 30, 50 ],//可以选择的分页集合
        columns:[[{ 
			field:'ck',
			title:'id',
			width:15, 
			checkbox:true
		},{
			field:'roleName',
			title:'角色名称',
			align:'center',
			formatter : dataStyle,
			width:100,
			sortable:true
		},{ 
			field:'description',
			title:'角色描述',
			width:140,
			align:'center'
		}/*,{
			field:'urlPerfixs',
			title:'权限url', 
			width:200,
			align:'center'
		}*/]], 
		onLoadSuccess:function(data){
	    	$('#roleListTableID').datagrid('clearSelections'); 
	    } 
	})
}

//查询
function queryData() {
	var roleName = $("#roleNameId00").val()
	var queryParams = null;
	$("#roleListTableID").datagrid({
		queryParams : {
			roleName : roleName
		}
	});
}
//清空查询条件
function clearQueryForm(){
	$("#queryFormId").form('clear');
}

function dataStyle(val,row){
	return '<a class="" href="#" onclick="viewRole(\''+row.id+'\')">'+row.roleName+'</a>'; 
}

//打开 新增 |修改 窗口
function openAddWin(winType){
	operation = winType;
	var title = "";
	if('add' == winType){
		title = "新增角色信息";
		$("#addRoleWinID").window({
			title : title,
			width : addWinWidth,
			height : addWinHeight, 
			left : (dataGridWidth - addWinWidth)/2,
			top : (bodyHeight - addWinHeight)/2,
			closable : true, 	 //否显示关闭按钮
			collapsible : false, //是否显示折叠按钮
			minimizable : false, //是否显示最小化按钮
			maximizable : false, //是否显示最大化按钮
			close : true,        //初始是否关闭
			modal : true
		});
		$("#buttonId01").show();
		$("#buttonId02").show();
		$("#addRoleForm").form('clear');
		$("#addRoleWinID").window('open');
	}
	if('edit' == winType){
		$("#addRoleForm").form('clear');
		title = "修改角色信息";
		var rows = $('#roleListTableID').datagrid('getSelections');
		if(rows.length == 1){
			var row = $('#roleListTableID').datagrid('getSelected');
			$.getJSON("/admin/role/getRoleByID?roleId=" + row.id, function(data) {
				$("#addRoleForm").form('load', data);
			});
			$("#addRoleWinID").window({
				title : title,
				width : addWinWidth,
				height : addWinHeight, 
				left : (dataGridWidth - addWinWidth)/2,
				top : (bodyHeight - addWinHeight)/2,
				closable : true, 	 //否显示关闭按钮
				collapsible : false, //是否显示折叠按钮
				minimizable : false, //是否显示最小化按钮
				maximizable : false, //是否显示最大化按钮
				close : true,        //初始是否关闭
				modal : true
			});
			$("#buttonId01").show();
			$("#buttonId02").show();
			$("#addRoleWinID").window('open');
		}else {
			$.messager.alert('提示', '请选中一条记录', 'info');
		}
	}
}
//保存数据
function saveData(){
	var url = "";
	if('add' == operation){
		url = "/admin/role/addRole";
	}
	if('edit' == operation){
		var row = $('#roleListTableID').datagrid('getSelected');
		url = "/admin/role/editRole/"+row.id;
	}
	
	if("" != url){
		$('#addRoleForm').form('submit',{
			url : url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(data){
				var data = eval("("+data+")")
				if (data.flag){ 
					$.messager.alert('提示', data.info, 'ok');
					closeRoleWin();
					initData();
				} else {
					$.messager.alert("提示",data.info,'error');
				}
			}
		});
	}
}

//删除
function delRole(){
	var rows = $('#roleListTableID').datagrid('getSelections');
	if (rows.length !=0 ){
		var jsonIds = arrayTojson(rows);
		$.messager.confirm("删除","确定删除？",function(r){
			if (r){
				var postUrl = '/admin/role/delRoles';
				$.post(postUrl,{
					"ids":jsonIds},function(result){
					if (result.flag){
						$.messager.alert('提示',result.info,'ok');
						initData();
					} else {
						$.messager.alert('提示',result.info,'error');
						initData();
						return;
					}
				},'json');
			}
		});
	}else{ 
		$.messager.alert('提示', '请选中一条记录', 'info');
		//$.调用jquery自己的内置对象
	}
}

//打开 查看 窗口
function viewRole(id){
	
	$.getJSON("/admin/role/getRoleByID?roleId=" + id, function(data) {
		$("#addRoleForm").form('load', data);
	});
	$("#addRoleWinID").window({
		title : '查看角色信息',
		width : addWinWidth,
		height : addWinHeight, 
		left : (dataGridWidth - addWinWidth)/2,
		top : (bodyHeight - addWinHeight)/2,
		closable : true, 	 //否显示关闭按钮
		collapsible : false, //是否显示折叠按钮
		minimizable : false, //是否显示最小化按钮
		maximizable : false, //是否显示最大化按钮
		close : true,        //初始是否关闭
		modal : true
	});
	$("#buttonId01").hide();
	$("#buttonId02").hide();
	$("#addRoleWinID").window('open');
	
}

function closeRoleWin(){
	$("#addRoleWinID").window('close');
}

/**
 * id数组转换为json字符串
 */
function arrayTojson(arr) {
	var jsonIds = "[";
	for ( var i = 0; i < arr.length; i++) {
		if (i == arr.length - 1) {
			jsonIds += arr[i].id;
		} else {
			jsonIds += arr[i].id + ",";
		}
	}
	jsonIds += "]";

	return jsonIds; 
}

//设置 long 宽度自适应   相对窗口
function fixLongWidth(){ 
	return addWinWidth*0.755;        
} 
            
//设置 short 宽度自适应
function fixShortWidth(){   
	return addWinWidth*0.30;           
} 