var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;

$(function(){
	initDefault();	
});
//默认
function initDefault(){
	// 默认时间
	$("#startTime").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#endTime").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEmachinename = $("#LIKEmachinename").textbox("getValue");
	sParam.startTime = $('#startTime').datetimebox("getValue");	
	sParam.endTime = $('#endTime').datetimebox("getValue");
	
	sParam.order = "order by currentstatetime desc"
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	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{
						return "<div style='height:13px;width:13px;background-color:red;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";				
					}
				}	
			},			
			{field:'repairstate',title:'维修状态',hidden:true,width:60},			
			{field:'machinestate',title:'主机状态',hidden:false,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:'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 progress-textRed">' + (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 memorytotal = record.memorytotal;
					if(memorytotal == null || memorytotal == ''){
						if(nc != null){
							nc = nc.indexOf('%')>0 ? nc.replace("%",""):nc;
							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>";
							}else {
								return '<div class="progress">'+
											'<div class="progress-value"  style="width: ' + (nc || 0) + 'px;">'+
												'<div class="progress-text">' + (nc+"%" || "") + "</div>" +
											"</div>" +
										"</div>";
							}
						}
					}else{
						var ncrl = record.memorytotal.toUpperCase();
						var dw = "G";
						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;
							ncrl = parseFloat(ncrl);
							sync = (ncrl * nc)/100;
							var syncLength=sync.toString().split(".")[1];  
							if(syncLength&&syncLength.length>2){
								sync=sync.toString().split(".")[0]+"."+syncLength.substring(0,2);
							}
							sync = parseFloat(sync);
							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;'>"+sync+dw+" / "+ncrl+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;'>"+sync+dw+" / "+ncrl+dw+"(总)</div>"+
										"</div>";
								
							}
						}else{
							return "未知";
						}	
					}								 
				}	
			},
			{field:'memorytotal',title:'内存总容量(G)',hidden:true,width:120},
			{field:'memoryusage',title:'内存使用率',hidden:true,width:120},
			{field:'harddiskstate',title:'硬盘状态',hidden:false,width:250,
				formatter:function(value,record,num){ 
					var yp = record.harddiskusage;
					var harddisktotal = record.harddisktotal;
					if(harddisktotal == null || harddisktotal == ''){
						if(yp != null){
							yp = yp.indexOf('%')>0 ? yp.replace("%",""):yp;
							if(value == 0){
								return '<div class="progress">'+
											'<div class="progress-value"  style="background-color:red;width: ' + (yp || 0) + 'px;">'+
												'<div class="progress-text progress-textRed">' + (yp+"%" || "") + "</div>" +
											"</div>" + 
										"</div>";						
							}else {
								return '<div class="progress">'+
											'<div class="progress-value"  style="width: ' + (yp || 0) + 'px;">'+
												'<div class="progress-text">' + (yp+"%" || "") + "</div>" +
											"</div>" + 
										"</div>";
							} 
						}
					}else{
						var yprl = record.harddisktotal.toUpperCase();
						var dw = "G";
						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";
						}					
						var ypsy="";					
						if(yp != null){
							yp = yp.indexOf('%')>0 ? yp.replace("%",""):yp;
							yprl = parseFloat(yprl);
							ypsy = (yprl * yp)/100;
							var ypsyLength=ypsy.toString().split(".")[1];  
							if(ypsyLength&&ypsyLength.length>2){
								ypsy=ypsy.toString().split(".")[0]+"."+ypsyLength.substring(0,2);
							}
							ypsy = parseFloat(ypsy);
							if(value == 0){
								return '<div class="progress">'+
											'<div class="progress-value"  style="background-color:red;width: ' + (yp || 0) + 'px;">'+
												'<div class="progress-text progress-textRed">' + (yp+"%" || "") + "</div>" +
											"</div>" + 
											"<div style='position: relative;left: 105px; top: -18px;'>"+ypsy+dw+" / "+yprl+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;'>"+ypsy+dw+" / "+yprl+dw+"(总)</div>"+
										"</div>";
							} 
						}else{
							return "未知";
						}
					}
				}	
			},
			{field:'harddisktotal',title:'硬盘总容量(G)',hidden:true,width:120},
			{field:'harddiskusage',title:'硬盘使用率',hidden:true,width:120},
			{field:'machineid',title:'操作',hidden:false,width:70, 
				formatter:function showClick(value,record,num){ 
        			return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="lookHistory('+num+')"  group="" id=""><span class="l-btn-left"><span class="l-btn-text">详情</span></span></a>';
				}
 			}
		]]
	});
}

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

//查看历史记录
function lookHistory(index){
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'历史详情',
		width	:  	800,
		height	: 	500, 
		data  :  selRows,
		url	:	sysUrl+"/yw/history/machine/ywmachinehistorystate_show.jsp",
//		onClose : closeRecordVideo
	});
}
