<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ include file="/jsp/common/include/localemessage.jsp" %>
<%
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setHeader("Expires", "0");

%>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="/jsp/common/include/css.jsp"/>
<jsp:include page="/jsp/common/include/script.jsp"/>
<script language="javascript" >
var url      = '<c:url value="/onl/adapter/http/httpStatus.json" />';
var url_view = '<c:url value="/onl/adapter/http/httpStatus.view" />';
var url_excel= '<c:url value="/onl/adapter/http/httpStatus.excel" />';
var timer;
function list(){
	var postData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로
	postData["adapterType"]="HTT,RST";	// REST 타입 추가
	$("#grid").setGridParam({ url:url,postData: postData }).trigger("reloadGrid");
}
function unformatterFunction(cellvalue, options, rowObject) {
	return cellvalue;
}

function formatterFunction(cellvalue, options, rowObject) {
	var rowId = options["rowId"];
	if ("${rmsMenuAuth}" =="W"){
		if (cellvalue=="03"){
// 			return "<img id='btn_stop' src='<c:url value='/images/bt/bt_end_sm.gif'/>' name='img_"+rowId+"' />";
			return "<button type='button' class='cssbtn smallBtn2' id='btn_stop' style='vertical-align:middle; font-weight:bold;' name='img_" + rowId + "'>stop</button>";
		}else if (cellvalue=="01"){
// 			return "<img id='btn_start' src='<c:url value='/images/bt/bt_boot_sm.gif'/>' name='img_"+rowId+"' />";
			return "<button type='button' class='cssbtn smallBtn2' id='btn_start' style='vertical-align:middle; font-weight:bold;' name='img_" + rowId + "'>start</button>";
		}
	}
	else {
		return "";
	}
}	

function fontFormatterFunction(cellvalue, options, rowObject) {
	
	var rowId = options["rowId"];
	if (cellvalue=='normal'){
		return "<font color='#0000FF'>"+cellvalue+"</font>";
	}else if (cellvalue=='abnormal'){
		return "<font color='#FF0000'>"+cellvalue+"</font>";
	}
}	
function startStop(postData) {
	$.ajax({
		type : "POST",
		url : url,
		dataType : "json",
		data : postData,
		success : function(json) {
			list();
		},
		error : function(e) {
			alert(e.responseText);
		}
	});

}

$(document).ready(function() {	
	
	$('#grid').jqGrid({
		datatype : "json",
		mtype : 'POST',
		//url : url,
		//postData : gridPostData,
		colNames : [ 
		             '<%= localeMessage.getString("adapterStat.adapterGroup") %>[TX]',
		             '<%= localeMessage.getString("adapterStat.instanceName") %>',
		             '<%= localeMessage.getString("adapterStat.adapterName") %>',
		             '<%= localeMessage.getString("adapterStat.adapterDesc") %>',
		             '<%= localeMessage.getString("adapterStat.adapterDv") %>',
		             '<%= localeMessage.getString("adapterStat.status") %>',
		             '<%= localeMessage.getString("adapterStat.startStat") %>',
		             '<%= localeMessage.getString("adapterStat.startup") %>',
		             'adapterGroupSimple'
		              ],
		colModel : [ 
		             { name : 'adapterGroupName'   , align : 'left'   , width:'160'  },
		             { name : 'eaiSvrInstNm'       , align : 'left'   , width:'80' , sortable:true },
		             { name : 'adapterName'        , align : 'left'   , width:'150'  },
		             { name : 'adapterGroupDesc'   , align : 'left'   },
		             { name : 'adapterKind'        , align : 'center' , width:'60' },
		             { name : 'adapterStatus'      , align : 'center' , width:'60' , unformat : unformatterFunction, formatter : fontFormatterFunction },
		             { name : 'adapterStartStatus' , align : 'center' , width:'60' },
		             { name : 'adapterStartStop'   , align : 'center' , width:'60' , unformat : unformatterFunction, formatter : formatterFunction },
		             { name : 'adapterGroupSimple' , hidden:true }
		              ],
		jsonReader : {
			repeatitems : false
		},
		rowNum: 10000,
		autoheight : true,
		height : '700',
		autowidth : true,
		viewrecords : true,
		ondblClickRow : function(rowId) {
		},
	    loadComplete:function (d){
			$("button[name*=img]").unbind("click");
				    
			$("button[name*=img]").click(function() {
				
				var name = $(this).attr("name");
				var rowId = name.split("_")[1];
				var control =$(this).attr("id").split("_")[1];
				var data = $('#grid').jqGrid('getRowData', rowId);
				var postData = [];
				postData.push({name:"cmd"              ,value:"TRANSACTION_STARTSTOP"});
				postData.push({name:"control"          ,value:control.toUpperCase()});
				postData.push({name:"eaiInstanceName"  ,value:data["eaiSvrInstNm"]});
				postData.push({name:"adapterGroupName" ,value:data["adapterGroupSimple"]});
				postData.push({name:"adapterName"      ,value:data["adapterName"]});
				postData.push({name:"adapterType"      ,value:data["adapterGroupSimple"].split('_')[3]});	// REST 타입 추가
				// postData.push({name:"adapterType"      ,value:"HTT"});
				//debugger;
				var msgStartStop = control=='start'?'<%= localeMessage.getString("adapterStat.start") %>':'<%= localeMessage.getString("adapterStat.close") %>';
				var resultConfirm = confirm(replaceMsg("<%= localeMessage.getString("common.confirmMsg1") %>",msgStartStop,data["adapterGroupName"],data["adapterName"])); 
// 				// data["adapterGroupName"]+ " "+ data["adapterName"]+ "을\n\n"+ msgStartStop+ " 하시겠습니까?"

				if (resultConfirm != true)
					return;
				startStop(postData);
				
			});	    	
	    	
	    },
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});			
			}
		
		},
		loadError: function (jqXHR, textStatus, errorThrown) {
			if(jqXHR.status == 401) {
	    		alert('HTTP status code: ' + jqXHR.status + '\n에러:  세션이 종료되었습니다. ');
	    		parent.location.href="<%=request.getContextPath()%>/";	
	    	}
// 			alert(errorThrown);
    	}
	});
	$("#btn_search").click(function(){
		list();
	});
	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			list();
		}
	});
	
	$("#btn_excel").click(function(event) {
        event.stopPropagation(); // Do not propagate the event.
        // Create an object that will manage to download the file.
        var target = "excelDown" ; // iframe의 name
        //(url,cmd,gridObj,formObject,fileName,merge)
		gridToExcelSubmit(url_excel,"LIST_GRID_TO_EXCEL",$("#grid"),$("#ajaxForm"),'http adapter');
		return false;
	});
// 	buttonControl();
	setTimeout(function() {
		list();	
	}, 400)
});
 
</script>
</head>
	<body>		
		<div class="right_box">
			<div class="content_top">
				<ul class="path">
					<li><a href="#">${rmsMenuPath}</a></li>					
				</ul>						
			</div><!-- end content_top -->
			<div class="content_middle">
				<div class="search_wrap">
					<button type="button" class="cssbtn" id="btn_excel" level="W" ><i class="material-icons">table_view</i> <%= localeMessage.getString("button.excel") %></button>
					<button type="button" class="cssbtn" id="btn_search" level="R" ><i class="material-icons">search</i> <%= localeMessage.getString("button.search") %></button>		
<%-- 					<img id="btn_excel" src="<c:url value="/img/btn_excel.png"/>" level="W" status="DETAIL"/> --%>
<%-- 					<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" /> --%>
				</div>
				<div class="title"><%= localeMessage.getString("httpStat.title") %></div>
				<table class="search_condition" cellspacing=0;>
					<tbody>
						<tr>
							<th width="180px"><%= localeMessage.getString("httpStat.search") %></th>
							<td>
								<input type="text" name="searchAdapterGroup" value="${param.searchAdapterGroup}">
								<input type="hidden" name="adapterType" value="HTT" style="width:100%">
							</td>
						</tr>
					</tbody>
				</table>
				<table id="grid" ></table>
				<div id="pager"></div>
				
			</div><!-- end content_middle -->
		</div><!-- end right_box -->				
	</body>		
</html>