var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
//	var startTime = $('#startTime').datetimebox("getValue");	
//	var endTime = $('#endTime').datetimebox("getValue");
	
	sParam.LIKEmachinename = $('#LIKEmachinename').textbox("getValue");
//	sParam.startTime = startTime;
//	sParam.endTime = endTime;
	sParam.order = " order by createtime desc"
	
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywmachinecurrentstateAction!listYwmachinecurrentstateByPage.do',
		columns : [[
		   /* {
				field: 'A',
				checkbox:'true',
				width:30
			},*/		    
			{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){
					}else {
						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 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:240,
				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;
							sync = (ncrl * nc)/100;
							sync = parseFloat(sync.toFixed(2));
							ncrl = parseFloat(parseFloat(ncrl).toFixed(2));
							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>";
								
							}
						//	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: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;
							ypsy = (yprl * yp)/100;
							ypsy = parseFloat(ypsy.toFixed(2));
							yprl = parseFloat(parseFloat(yprl).toFixed(2));
							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:'currentstateid',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 doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywmachinecurrentstateAction!delete_Ywmachinecurrentstate.do',
			data:{
				currentstateid	:	selRows.currentstateid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].currentstateid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywmachinecurrentstateAction!batch_delete_Ywmachinecurrentstate.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(!$.isNull(flag)){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywgl/ywmachinecurrentstate_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywgl/ywmachinecurrentstate_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	//console.log(model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(toEdit){//修改
		model.XGTJcurrentstateid = jq('#currentstateid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywmachinecurrentstateAction!update_Ywmachinecurrentstate.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywmachinecurrentstateAction!insert_Ywmachinecurrentstate.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var downFile = baseUrl+"/down/down.xls";
	window.location.href= downFile;
}
//导入
function doImport(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'导入',
		 width	:  	400,
		 height	: 	130,
		 url	:	sysUrl+"/im/xx/ywmachinecurrentstate_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywmachinecurrentstateAction!count_Ywmachinecurrentstate.do',
		success : function(value) {
			if(!$.isNull(value)){
				countExport(value);
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
// 导出传参条件
function countExport(totalSize) {
	var limit = 5000;
	if (totalSize > 5000) {
		top.$.dlg.confirm('记录过多支持前5000条,确定要将记录导出Excel为表格吗?',function(){ 
			execExport();
		});		
	} else {
		top.$.dlg.confirm('确认导出?',function(){ 
			execExport();
		})
	}
}
// 调用后台
function execExport() {
	var model = getQueryParams();
	var downParam = "";
	document.excelForm.action = 'ywmachinecurrentstateAction!exp_Ywmachinecurrentstate.do?'+downParam;
	document.excelForm.submit(); 
}

// 查看历史记录
function lookHistory(index){
	
	//console.log("历史跳转：",index);
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'历史记录',
		width	:  	800,
		height	: 	500, 
		data  :  selRows,
		url	:	sysUrl+"/yw/recordalarm/ywmachinecurrentstate/ywmachineinfo.jsp",
//		onClose : closeRecordVideo
	});
}

