var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var num=0;
$(function(){
	initDefault();	
	initDetailTab();
});
//默认
function initDefault(){
	
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEmachinename = $('#LIKEmachinename').textbox("getValue");
	sParam.order = "order by createtime desc "
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [
	{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	doAdd
    },{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    },{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete_batch
    },{
        text	:	'启用',
        iconCls	:	'icon-start',
        handler	:	switchOpen
    },{
        text	:	'停止',
        iconCls	:	'icon-end',
        handler	:	switchClose
    }];
	$('#searchGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywmachineinfoAction!listYwmachineinfoByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'machineid',title:'machineid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
			{field:'machinename',title:'服务器名称',hidden:false,width:120},
			{field:'machineip',title:'服务器ip',hidden:false,width:100},
			{field:'csmc',title:'是否启用',hidden:false,width:80}
		]],		
		onClickRow:function(index,row){
			num = index
			setchecked();
		},
		onLoadSuccess:function(){
			$("#searchGrid").datagrid("selectRow",num);
			setchecked();
			
		}
	});
}

// 点击 左侧 查询右侧信息
function setchecked(){
	
	var choseId = $('#detailtab').tabs('getSelected').find("iframe").attr("id");
//	console.log("choseId: ===========",choseId);
	if("cpuInfo" == choseId){
		choseSrc(0);
	}else if("memoryInfo" == choseId){
		choseSrc(1);
	}else if("diskInfo" == choseId){
		choseSrc(2);
	}
}

function initDetailTab(){
	detailtab = $("#detailtab").tabs({
		onSelect:function(title,index){
//			console.log("title 和 index",title,index);
			if(index){
				setTimeout(function(){choseSrc(index)},500);
			}
		}
	});
}

function choseSrc(index){
	var selRows = $('#searchGrid').datagrid('getSelected');
//	console.log("choseSrc：",selRows);
	var machineid = '';
	if(selRows != null){
		machineid = selRows.machineid;
	}
	if("0" == index){      //cpu
		$("#cpuInfo").prop("src",'../../machinemanage/machinecpu/ywmachinecpu.jsp?machineid='+machineid);		
	}else if('1' == index){			// 内存
		$("#memoryInfo").prop("src",'../../machinemanage/machinememory/ywmachinememory.jsp?machineid='+machineid);			
	}else if('2' == index){			// 硬盘
		$("#diskInfo").prop("src",'../../machinemanage/machineharddisk/ywmachineharddisk.jsp?machineid='+machineid);		
	}
}

//删除
function doDelete(){
	var selRows = $('#searchGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywmachineinfoAction!delete_Ywmachineinfo.do',
			data:{
				machineid	:	selRows.machineid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#searchGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].machineid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywmachineinfoAction!batch_delete_Ywmachineinfo.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(flag){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#searchGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	430,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/machinemanage/machineinfo/ywmachineinfo_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#searchGrid').datagrid('getRows')[index];
//	console.log("选择：",selRows);
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	430,
		data   	:   selRows,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/machinemanage/machineinfo/ywmachineinfo_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;
	}
	model.machinedescribe = jq("#machinedescribe").val();
	if(toEdit){//修改
		model.XGTJmachineid = jq('#machineid').val();
		//model.checkparam = " and xh='"+model.xh+"' ";
		console.log("修改：",model);
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywmachineinfoAction!update_Ywmachineinfo.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+"' ";
		console.log("增加：",model);
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywmachineinfoAction!insert_Ywmachineinfo.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			},
			error:function(e){
				console.log("出错了：",e)
			}
		});
	}
}
//打开修改
function doEdit(){
	var selRows = $('#searchGrid').datagrid('getChecked');
	if(selRows.length>1 || selRows===null){
		$.showMsg('请选择一行! ');
	}else{
		var selIndex = $('#searchGrid').datagrid('getRowIndex', $("#searchGrid").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,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 url	:	sysUrl+"/im/xx/ywmachineinfo_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywmachineinfoAction!count_Ywmachineinfo.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 = 'ywmachineinfoAction!exp_Ywmachineinfo.do?'+downParam;
	document.excelForm.submit(); 
}


function switchOpen(){				// 开启
	var selRows = $('#searchGrid').datagrid('getChecked');
//	console.log("selRows：",selRows);
	var ids = '';
	if(selRows===null || selRows.length==0){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		selRows.forEach(function(item){
			ids += item.machineid+',';
		})
	    $.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'ywmachineinfoAction!edit_Ywmachineinfo.do',
			 data:{'ids':ids,'detectionswitch':1},
			 success:function(res){
			 	if(res){
					$.showMsg("操作成功！");
				 	doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		 });
	}
}

function switchClose(){				// 关闭
	var selRows = $('#searchGrid').datagrid('getChecked');
	console.log("selRows：",selRows);
	var ids = '';
	if(selRows===null){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		selRows.forEach(function(item){
			ids += item.machineid+',';
		})
	    $.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'ywmachineinfoAction!edit_Ywmachineinfo.do',
			 data:{'ids':ids,'detectionswitch':0},
			 success:function(res){
			 	if(res){
					$.showMsg("操作成功！");
				 	doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		 });		
	}
}

