<%@ 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/admin/common/adapterInstMan.json" />';
var url_view = '<c:url value="/onl/admin/common/adapterInstMan.view" />';

var arr = new Array();
var selAdptGrpName = "";
function linkFormatter(cellvalue, options, rowObject){
	if (cellvalue==null|| cellvalue==undefined) return "";
	var ip = rowObject['EAISEVRIP'];
	var port = rowObject['SOCKETPORT'];
	var adptName = rowObject['adapterName'];
	var name = options["colModel"].name;
	var type="sendRecv";
	if(name=="error"){
		type="error";
	}else if (name=="adapterName"){
		type="show"
	}
	var res = "<a onClick='linkModal(\""+ip+"\",\""+port+"\",\""+selAdptGrpName+"\",\""+adptName+"\",\""+type+"\")' style='text-decoration: underline; cursor:pointer;'>"+cellvalue+"</a>";
	return res;
}	
function linkModal(ip,port,adptGrp,adptName,type){
	var url = "http://"+ip+":"+port+"/adapter/"+type+"?adapter="+adptGrp+"."+adptName;
	var args = new Object();
	showModal(url,args,1200,600,'','scroll:on');
}
function startStopFormater(cellvalue, options, rowObject){
	var adptName = rowObject['adapterName'];
	var inst = rowObject["EAISEVRINSTNCNAME"];
	if(rowObject["state"]=="STOPPED"){
		return "<img name='startStop_start' src='<c:url value='/images/bt/bt_boot_sm.gif'/>' style='cursor:pointer;'"+
				"onClick='startStop(\"start\",\""+inst+"\",\""+selAdptGrpName+"\",\""+adptName+"\")'/>";
	}else{
		return "<img name='startStop_start' src='<c:url value='/images/bt/bt_end_sm.gif'/>' style='cursor:pointer;'"+
				"onClick='startStop(\"stop\",\""+inst+"\",\""+selAdptGrpName+"\",\""+adptName+"\")'/>";
	}
}
function startStop(status,inst,adptGrpName,adptName) {
	var postData = [];
	postData.push({ name : "cmd", value : "TRANSACTION_STARTSTOP" });
	postData.push({ name : "control", value : status });
	postData.push({ name : "eaiInstanceName", value : inst });
	postData.push({ name : "adapterGroupName", value : adptGrpName });
	postData.push({ name : "adapterName", value : adptName });
	$.ajax({
		type : "POST",
		url : url,
		dataType : "json",
		data : postData,
		success : function(json) {
			$("#grid2").setGridParam({ url :url, postData: {cmd : "LIST_SUB", searchAdptGrpName : selAdptGrpName.toUpperCase()}}).trigger("reloadGrid");
		},
		error : function(e) {
			alert(e.responseText);
		}
	});
}
function traceFormater(cellvalue, options, rowObject){
	var adptName = rowObject['adapterName'];
	var inst = rowObject["EAISEVRINSTNCNAME"];
	
	var def 	= " <a onClick='traceLevel(\"0\",\""+inst+"\",\""+selAdptGrpName+"\",\""+adptName+"\")' style='text-decoration: underline; cursor:pointer;' name='trace'>0</a> ";
	var info 	= " <a onClick='traceLevel(\"1\",\""+inst+"\",\""+selAdptGrpName+"\",\""+adptName+"\")' style='text-decoration: underline; cursor:pointer;' name='trace'>1</a> ";
	var debug	= " <a onClick='traceLevel(\"2\",\""+inst+"\",\""+selAdptGrpName+"\",\""+adptName+"\") 'style='text-decoration: underline; cursor:pointer;' name='trace'>2</a> ";
	var trace 	= " <a onClick='traceLevel(\"3\",\""+inst+"\",\""+selAdptGrpName+"\",\""+adptName+"\") 'style='text-decoration: underline; cursor:pointer;' name='trace'>3</a> ";
	if(cellvalue==0){
		return "0(DEFAULT) "+ info + debug + trace ;
	}else if(cellvalue==1){
		return def + "1(INFO)" + debug + trace ;
	}else if(cellvalue==2){
		return def + info + "2(DEBUG)" + trace ;
	}else if(cellvalue==3){
		return def + info + debug + "3(TRACE)" ;
	}
}
function traceLevel(level,inst,adptGrpName,adptName) {
	var postData = [];
	postData.push({ name : "cmd", value : "TRANSACTION_STARTSTOP" });
	postData.push({ name : "level", value : level });
	postData.push({ name : "eaiInstanceName", value : inst });
	postData.push({ name : "adapterGroupName", value : adptGrpName });
	postData.push({ name : "adapterName", value : adptName });
	$.ajax({
		type : "POST",
		url : url,
		dataType : "json",
		data : postData,
		success : function(json) {
			$("#grid2").setGridParam({ url :url, postData: {cmd : "LIST_SUB", searchAdptGrpName : selAdptGrpName.toUpperCase()}}).trigger("reloadGrid");
		},
		error : function(e) {
			alert(e.responseText);
		}
	});
}

function colorFormatter(cellvalue, options, rowObject){
	var delay = rowObject['delay'];
	if(delay=="O"){
		return "<span style='color:red'>"+cellvalue+"</span>";
	}else{
		return cellvalue;
	}
}
$(document).ready(function() {	
	var gridPostData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로 
	$('#grid').jqGrid({
		mtype: "POST",
		datatype:"json",
		colNames:[
                  '<%= localeMessage.getString("adptInMan.adptGrpNm") %>'
                  ],
		colModel:[ 
				{ name : 'ADPTRBZWKGROUPNAME',    align:'left' , width: 120 }
				],
		          
        jsonReader: {
             repeatitems:false
        },	          
		rowNum : 10000,
	    height: 350,
		width: 320,
		viewrecords: true,
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});	//그리드 헤더 화살표 삭제(정렬X)		
			}
		},loadComplete : function (d) {
        },loadBeforeSend : function (xhr,settings){
			if(this.p.url ==null || this.p.url == ""){
				this.p.loadBeforeSend = null;
				return false;
			}
		},
        onSelectRow : function(rowId){
       		var rowData = $(this).getRowData(rowId);
        	selAdptGrpName = rowData["ADPTRBZWKGROUPNAME"];
        	$('#grid2').jqGrid('clearGridData');
        	$('#grid3').jqGrid('clearGridData');
        	$('#dataText').val('');
        	$("#grid2").setGridParam({ url :url, postData: {cmd : "LIST_SUB", searchAdptGrpName : selAdptGrpName.toUpperCase()} ,page : "1"  }).trigger("reloadGrid");
        }
	});
	$('#grid2').jqGrid({
		mtype: "POST",
		datatype:"json",
		colNames:[
				  '<%= localeMessage.getString("adptInMan.instNm") %>',
				  '<%= localeMessage.getString("adptInMan.adptNm") %>',
                  '<%= localeMessage.getString("adptInMan.sta") %>',
                  '<%= localeMessage.getString("adptInMan.stat") %>',
                  '<%= localeMessage.getString("adptInMan.staTm") %>',
                  'pool',
                  '<%= localeMessage.getString("adptInMan.recv") %>',
                  '<%= localeMessage.getString("adptInMan.send") %>',
                  '<%= localeMessage.getString("adptInMan.err") %>',
                  '<%= localeMessage.getString("adptInMan.traLv") %>',
                  '<%= localeMessage.getString("adptInMan.staUp") %>',
                  'data',
                  'normal',
                  'EAISEVRIP',
                  'SOCKETPORT'
                  ],
		colModel:[ 
				{ name : 'EAISEVRINSTNCNAME',   	align:'left' 	, sortable:false, width : 30},
				{ name : 'adapterName',   	align:'left' 	, sortable:false, width : 60,	formatter:linkFormatter},
				{ name : 'state',       	align:'center'  , width: 20},
				{ name : 'status',     		align:'center'	, width: 15},
				{ name : 'uptime',     		align:'center'	, width: 20},
				{ name : 'pool',       		align:'center'  , width: 10},
				{ name : 'recv',            align:'center'  , width: 10,	formatter:linkFormatter},
				{ name : 'send',            align:'center' 	, width: 10,	formatter:linkFormatter},
				{ name : 'error',           align:'center' 	, width: 10,	formatter:linkFormatter},
				{ name : 'trace',           align:'center' 	, width: 30,	formatter:traceFormater},
				{ name : 'button',         	align:'center'	, width: 20,	formatter:startStopFormater},
				{ name : 'data',          	hidden : true},
				{ name : 'normal',          hidden : true},
				{ name : 'EAISEVRIP',       hidden : true},
				{ name : 'SOCKETPORT',      hidden : true}
				
				],
		          
        jsonReader: {
             repeatitems:false
        },	          	
		rowNum : 10000,
	    height: 350,
	    width: 1350,
		viewrecords: true,
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});	//그리드 헤더 화살표 삭제(정렬X)		
			}
		
		},loadComplete : function (data) {
			arr = data.rows; 
			$("#grid2").jqGrid('setSelection',001,true);
        },loadBeforeSend : function (xhr,settings){
			if(this.p.url ==null || this.p.url == ""){
				this.p.loadBeforeSend = null;
				return false;
			}
		},loadError: function (jqXHR, textStatus, errorThrown) {
        	//debugger;
        	if(jqXHR.status == 401) {
	    		alert('HTTP status code: ' + jqXHR.status + '\n' +
	                    '에러:  세션이 종료되었습니다. ');
	    		parent.location.href='<%=request.getContextPath()%>/';	
	    	}
    	},
        onSelectRow : function(rowId){
        	$('#grid3').jqGrid('clearGridData');
       		var rowData = $(this).getRowData(rowId);
       		$('#dataText').val('');
       		$("#dataText").val(rowData['data']);
       		var cnt = 0;
       		for(var i=0; i<arr.length; i++){
       			var sessionInfo = arr[i]['normal'];
       			var delay = arr[i]['delay'];
       			var addData = new Array();
           		if(sessionInfo.length==0){
           			$("#grid3").addRowData(0,{"normal":"세션정보 없음","EAISEVRINSTNCNAME":arr[i]['EAISEVRINSTNCNAME'],"delay":"X"});
           		}else{
           			if(delay.length>0){
	           			for(var j=0; j<delay.length;j++){
	            			//addData.push({ "normal" :  delay[j] ,"EAISEVRINSTNCNAME":arr[i]['EAISEVRINSTNCNAME'],"delay":"O"});
	            			//$("#grid3").addRowData(cnt,addData[j]);
	            			$("#grid3").addRowData(cnt,{ "normal" :  delay[j] ,"EAISEVRINSTNCNAME":arr[i]['EAISEVRINSTNCNAME'],"delay":"O"});
	            			cnt++;
	            		}
           			}
            		for(var j=0; j<sessionInfo.length;j++){
            			//addData.push({ "normal" :  sessionInfo[j] ,"EAISEVRINSTNCNAME":arr[i]['EAISEVRINSTNCNAME'],"delay":"X"});
            			//$("#grid3").addRowData(cnt,addData[j]);
            			$("#grid3").addRowData(cnt,{ "normal" :  sessionInfo[j] ,"EAISEVRINSTNCNAME":arr[i]['EAISEVRINSTNCNAME'],"delay":"X"});
            			cnt++;
            		}
           		}
       		}
        }
	});
	$('#grid3').jqGrid({
		mtype: "POST",
		datatype:"json",
		colNames:[
					'<%= localeMessage.getString("adptInMan.instNm") %>',
					'<%= localeMessage.getString("adptInMan.sessInfo") %>',
					'delay'
                  ],
		colModel:[ 
				{ name : 'EAISEVRINSTNCNAME',   align:'left' , sortable:false, width:30},
				{ name : 'normal',   align:'left' , sortable:false, formatter : colorFormatter},
				{ name : 'delay',   hidden:true}
				],
		          
        jsonReader: {
             repeatitems:false
        },	          	
		rowNum : 10000,
	    height: 200,
	    autowidth: true,
		viewrecords: true,
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});	//그리드 헤더 화살표 삭제(정렬X)		
			}
		
		},loadComplete : function (data) {
			//debugger;
        },loadBeforeSend : function (xhr,settings){
			if(this.p.url ==null || this.p.url == ""){
				this.p.loadBeforeSend = null;
				return false;
			}
		}
	});
	$("#btn_search").click(function(){
		var adptGrpName = $("input[name=searchAdptGrpName]").val();
		var portNum = $("input[name=searchPortNum]").val();
		$('#grid2').jqGrid('clearGridData');
		$('#grid3').jqGrid('clearGridData');
		$('#dataText').val('');
		$("#grid").setGridParam({ url :url, 
				postData: {cmd : "LIST", searchAdptGrpName : adptGrpName.toUpperCase(), searchPortNum : portNum} ,page : "1"  }).trigger("reloadGrid");
		
	});
	$("#btn_new").click(function(){
		var url2 = url_view;
		url2 += '?cmd=DETAIL';
		url2 += '&page='+$("#grid").getGridParam("page");
		url2 += '&returnUrl='+getReturnUrl();
        url2 += '&menuId='+'${param.menuId}';
		//검색값
        //url = url + '&'+getSearchUrl();
		
        goNav(url2);
	});
	buttonControl();	
	$("input[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			$("#btn_search").click();
		}
	});
	$("#btn_up").hide();
	$("#btn_up").click(function(){
		$("#dataText").show();
		$(this).hide();
		$("#btn_down").show();
	});
	$("#btn_down").click(function(){
		$("#dataText").hide();
		$(this).hide();
		$("#btn_up").show();
	});
});

</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" id="content_middle">
				<div class="search_wrap">
					<button type="button" class="cssbtn" id="btn_search" level="R"><i class="material-icons">search</i> <%= localeMessage.getString("button.search") %></button>
<%-- 					<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" />				 --%>
				</div>
				<div class="title"><%= localeMessage.getString("adptInMan.title") %> </div>
					<form id="ajaxForm"  onsubmit="return false;">
						<table class="search_condition" cellspacing=0;>
							<tbody>
								<tr>
									<th style="width:180px;"><%= localeMessage.getString("adptInMan.adptGrpNm") %></th>
									<td>
										<div class="select-style">
											<input type="text" name="searchAdptGrpName" value="${param.searchAdptGrpName}"/>
										</div>
									</td>
									<th style="width:180px;"><%= localeMessage.getString("adptInMan.portNum") %></th>
									<td>
										<div class="select-style">
											<input type="text" name="searchPortNum" value="${param.searchPortNum}"/>
										</div>
									</td>
								</tr>
							</tbody>
						</table>
					</form>
					<div >
						<div id="gridDiv" style="display:inline-block; vertical-align:top;">
							<span class="gridtitle"><%= localeMessage.getString("adptInMan.adptGrpNm") %></span>
							<table id="grid" ></table>
						</div>
						<div id="grid2Div" style="display:inline-block; vertical-align:top; width:77%" >
							<span class="gridtitle"><%= localeMessage.getString("adptInMan.instAdpt") %></span>
							<table id="grid2"></table>
							<div id="dataDiv" style="position: absolute; top: 400px; height: 120px; width: 1350px;">
								<textarea id="dataText" name="normal" style="height:100%; width:100%;" readonly></textarea>
								<img src="<c:url value="/img/btn_grid_up.png"/>" alt="" id="btn_up" level="R" style="position: relative; bottom: -80%; left: 1310px;" />
								<img src="<c:url value="/img/btn_grid_down.png"/>" alt="" id="btn_down" level="R" style="position: relative; bottom: 100%; left: 1310px;"/>	
							</div>
						</div>
						<div style="margin-top:10px;">
							<table id="grid3" ></table>
						</div>
					</div>
			</div><!-- end content_middle -->
		</div><!-- end right_box -->				
	</body>	
</html>

