var currentstateid = $.getUrlParam("currentstateid");

$(function(){
//	console.log("历史记录：",currentstateid);
	initDefault();	
});
//默认
function initDefault(){
	// 设置默认时间
	$("#startTime").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#endTime").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.currentstateid = currentstateid;
	
	sParam.startTime = $('#startTime').datetimebox("getValue");	
	sParam.endTime = $('#endTime').datetimebox("getValue");
	
	//sParam.order = "order by currentstatetime desc"
//	console.log("查询参数：",sParam);
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [/*{
    	text    :	'刷新',
        iconCls : 	'icon-refresh',
        handler : 	function(){location.reload(true);}
    },*/{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywmachinehistorystateAction!listYwmachinehistorystateByPage.do',
		columns : [[
			{field:'machinename',title:'机器名称',hidden:false,width:120},
			{field:'machineip',title:'机器ip',hidden:false,width:120},
			{field:'currentstatetime',title:'检测时间',hidden:false,width:125},
			{field:'currentstate',title:'当前状态',hidden:true,width:60/*,
				formatter:function showClick(value,record,num){ 
 					if(value==1){
						return "<div style='height:13px;width:13px;background-color:green;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";				
					}else if(value==0){
						return "<div style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";						
					}else{
						return value;
					}
				}	*/
			},			
			{field:'repairstate',title:'维修状态',hidden:true,width:60},			
			{field:'machinestate',title:'主机状态',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					// console.log("主机：",value,record);
 					if(value==1){
						return "<div style='height:13px;width:13px;background-color:green;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";				
					}else if(value==0){
						return "<div style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";						
					}else{
						return value;
					}
				}
			},
			{field:'cpustatus',title:'CPU状态',hidden:false,width:110,
				formatter:function(value,record,num){ 
					var cpu = record.cpuusage;
					if(cpu != null){
						cpu = cpu.indexOf('%')>0 ? cpu.replace("%",""):cpu;
						if(value == 0){
							return '<div class="progress"><div class="progress-value"  style="background-color:red;width: ' + (cpu || 0) + 'px;"><div class="progress-text">' + (cpu+"%" || "") + "</div></div></div>"
						}else {
							return $.gridProgressBar(cpu+"%",100*cpu/100.00);
						}						
					}else{
						return "未知";
					}
					
				}
			},
			{field:'cpuusage',title:'CPU使用率',hidden:true,width:120},
			{field:'memorystate',title:'内存状态',hidden:false,width:220,
				formatter:function(value,record,num){ 
					var nc = record.memoryusage;
					var ncrl = record.memorytotal.toUpperCase();
					var dw = "";
					if(ncrl.indexOf('G') > 0){
						ncrl = ncrl.replace("G","");
						dw = "G";
					}else if(ncrl.indexOf('M') > 0){
						ncrl = ncrl.replace("M","");
						dw = "M";
					}else if(ncrl.indexOf('KB') > 0){
						ncrl = ncrl.replace("KB","");
						dw = "KB";
					}
					var sync = "";
					if(nc != null){
						nc = nc.indexOf('%')>0 ? nc.replace("%",""):nc;
						sync = (ncrl * nc)/100;
						if(value == 0){
							return '<div class="progress">'+
										'<div class="progress-value"  style="background-color:red;width: ' + (nc || 0) + 'px;">'+
											'<div class="progress-text">' + (nc+"%" || "") + "</div>" +
										"</div>" + 
										"<div style='position: relative;left: 105px; top: -18px;'>"+parseFloat(sync.toFixed(2))+dw+" / "+parseFloat(parseFloat(ncrl).toFixed(2))+dw+"(总)</div>"+
									"</div>";
						}else {
							return '<div class="progress">'+
										'<div class="progress-value"  style="width: ' + (nc || 0) + 'px;">'+
											'<div class="progress-text">' + (nc+"%" || "") + "</div>" +
										"</div>" +
										"<div style='position: relative;left: 105px; top: -18px;'>"+parseFloat(sync.toFixed(2))+dw+" / "+parseFloat(parseFloat(ncrl).toFixed(2))+dw+"(总)</div>"+
									"</div>";
							
						}
					//	return $.gridProgressBar(nc+"%",100*nc/100.00);
					}else{
						return "未知";
					}				 
				}	
			},
			{field:'memorytotal',title:'内存总容量(G)',hidden:true,width:120},
			{field:'memoryusage',title:'内存使用率',hidden:true,width:120},
			{field:'harddiskstate',title:'硬盘状态',hidden:false,width:220,
				formatter:function(value,record,num){ 
					var yp = record.harddiskusage;
					var yprl = record.harddisktotal.toUpperCase();
					var dw = "";
					if(yprl.indexOf('G') > 0){
						yprl = yprl.replace("G","");
						dw = "G";
					}else if(yprl.indexOf('M') > 0){
						yprl = yprl.replace("M","");
						dw = "M";
					}else if(yprl.indexOf('KB') > 0){
						yprl = yprl.replace("KB","");
						dw = "KB";
					}else{
						dw = 'G'
					}					
					var ypsy="";					
					if(yp != null){
						yp = yp.indexOf('%')>0 ? yp.replace("%",""):yp;
						ypsy = (yprl * yp)/100;
						if(value == 0){
							return '<div class="progress">'+
										'<div class="progress-value"  style="background-color:red;width: ' + (yp || 0) + 'px;">'+
											'<div class="progress-text">' + (yp+"%" || "") + "</div>" +
										"</div>" + 
										"<div style='position: relative;left: 105px; top: -18px;'>"+parseFloat(ypsy.toFixed(2))+dw+" / "+parseFloat(parseFloat(yprl).toFixed(2))+dw+"(总)</div>"+
									"</div>";						
						}else {
							return '<div class="progress">'+
										'<div class="progress-value"  style="width: ' + (yp || 0) + 'px;">'+
											'<div class="progress-text">' + (yp+"%" || "") + "</div>" +
										"</div>" + 
										"<div style='position: relative;left: 105px; top: -18px;'>"+parseFloat(ypsy.toFixed(2))+dw+" / "+parseFloat(parseFloat(yprl).toFixed(2))+dw+"(总)</div>"+
									"</div>";
						} 
					}else{
						return "未知";
					}
					
				}	
			},
			{field:'harddisktotal',title:'硬盘总容量(G)',hidden:true,width:120},
			{field:'harddiskusage',title:'硬盘使用率',hidden:true,width:120},
			
		]]
	});
}

//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}

