<%@ 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" %>
<%@ page import="com.eactive.eai.common.util.SystemUtil"%>
<%@ page import="com.eactive.eai.rms.common.login.SessionManager"%>
<%
	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/socket/socketStatus.json" />';
	var url_view = '<c:url value="/onl/adapter/socket/socketStatus.view" />';
	var url_excel = '<c:url value="/onl/adapter/socket/socketStatus.excel" />';
	var timer;
	
	var roleString	= "<%=SessionManager.getRoleIdString(request)%>";
	var stgSvr		= <%=SystemUtil.isStagingServer()%>;

	function init(callback) {
		$.ajax({
			type : "POST",
			url : url,
			dataType : "json",
			data : {
				cmd : 'LIST_INIT_COMBO'
			},
			success : function(json) {
				new makeOptions("CODE", "NAME").setObj(
						$("select[name=searchRealTimeCd]")).setData(
						json.realTimeGroupRows).rendering();

				if (typeof callback === 'function') {
					callback();
				}
			},
			error : function(e) {
				alert(e.responseText);
			}
		});
	}

	function list() {
		var postData = getSearchForJqgrid("cmd", "LIST"); //jqgrid에서는 object 로
		$("#grid").setGridParam({
			url : url,
			postData : postData,
			datatype : 'json'
		}).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 "<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 == "true") {
			return "<font color='#0000FF'>" + cellvalue + "</font>";
		} else if (cellvalue == "false") {
			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);
			}
		});
	}
	
	function goDetail(obj, rowId) {
		var rowData = obj
				.getRowData(rowId);
		var key = rowData['EaiSevrInstncName'];
		var key2 = rowData['AdptrBzwkGroupName'];

		var url2 = '<c:url value="/onl/adapter/socket/socketConnection.view" />';
		url2 += "?cmd=LIST";
		url2 += "&searchEaiSevrInstncName="
				+ key;
		url2 += "&searchAdptrBzwkGroupName="
				+ key2;
		goNav(url2);
	}
	
	function goFilePage(){
		location.href = "<%=request.getContextPath()%>/jsp/adapterMonitorFile.jsp";
	}
	
	//소켓 모니터 화면 추가 
	function goPage() {	
		var sw = screen.width;
		var sh = screen.height;
		var mode = "width="+ sw +",height="+ sh +",left=0,top=0";
//	 	mode = mode + ",channelmode=yes"; //only IE titlebar=yes
//	 	mode = mode + ",,fullsctreen=yes"; // only IE titlebar=no
		mode = mode + ",location=yes,menubar=yes,scrollbars=yes,resizable=yes,status=yes,titlebar=yes,toolbar=yes";
		var win = window.open("<%=request.getContextPath()%>/jsp/adapterMonitorPage.jsp", "_blank", mode);
		win.focus();
	}
	
	$(document).ready( function() {
		//권한체크
	    if ("${rmsMenuAuth}" =="W"){
	    	$("#socketLink ,#socketLink2").show();
	    }							
		
		$("select[name=searchRealTimeCd]").hide();
	
		var mod = sessionStorage["mod"];
	
		if (mod == undefined || mod == "")
			mod = "ALL";
		else
			sessionStorage["mod"] = "";
	
		$("input:radio[name=searchType]:radio[value='"+ mod + "']").attr("checked", true);
	
		$('#grid').jqGrid({
			datatype : "local",
			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") %>' 
						],
			colModel : [
						{ name : 'AdptrBzwkGroupName'	,align : 'left'		,width : '140' },
						{ name : 'EaiSevrInstncName'	,align : 'left'		,width : '100'	,sortable : false },
						{ name : 'AdptrBzwkName'		,align : 'left'		,width : '170' },
						{ name : 'AdptrBzwkDesc'		,align : 'left'		,width : '200' },
						{ name : 'AdptrType'			,align : 'center'	,width : '80' },
						{ name : 'Status'				,align : 'center'	,width : '40'	,unformat : unformatterFunction, formatter : fontFormatterFunction},
						{ name : 'Starting'				,align : 'center'	,width : '55' },
						{ name : 'StartStop'			,align : 'center'	,width : '60'	,unformat : unformatterFunction, formatter : formatterFunction}
					],
			jsonReader : {
				repeatitems : false
			},
			rowNum : 1000,
			height : '500',
			autowidth: true,
			viewrecords : true,
			gridview : true,
			ondblClickRow : function(rowId) {
			console.log("rowId", rowId);
				var rowData = $(this)
						.getRowData(rowId);
				var key = rowData['EaiSevrInstncName'];
				var key2 = rowData['AdptrBzwkGroupName'];
	
				var url2 = '<c:url value="/onl/adapter/socket/socketConnection.view" />';
				url2 += "?cmd=LIST";
				url2 += "&searchEaiSevrInstncName="
						+ key;
				url2 += "&searchAdptrBzwkGroupName="
						+ key2;
	            url2 += '&menuId='+'${param.menuId}';
				goNav(url2);
			},
			loadComplete : function(d) {
				$("button[name*=img]").unbind("click");
	
				$("button[name*=img]").click(function() {
// 					alert('click');
					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 });
					postData.push({ name : "eaiInstanceName"	,value : data["EaiSevrInstncName"]});
					postData.push({ name : "adapterGroupName"	,value : data["AdptrBzwkGroupName"]});
					postData.push({ name : "adapterName"		,value : data["AdptrBzwkName"]});
		
					var msgStartStop = control == "start" ? "<%= localeMessage.getString("adapterStat.start") %>":"<%= localeMessage.getString("adapterStat.close") %>";
		
		               var resultConfirm = confirm(replaceMsg("<%= localeMessage.getString("common.confirmMsg1") %>",msgStartStop,data["AdptrBzwkGroupName"],data["AdptrBzwkName"])); 		
					/* var resultConfirm = confirm(data["AdptrBzwkGroupName"]
							+ " "
							+ data["AdptrBzwkName"]
							+ "을\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(xhr, status,error) {
				alert(error);
				//clearInterval(timer);
			}
		});
	
		$("#btn_search").click(function() {
			list();
		});
		$("input[name^=search]").keydown(function(key) {
			if (key.keyCode == 13) {
				list();
			}
		});
	
		$("input:radio[name=searchType]").change(function() {
			var searchType = $(this).val();
	
			if (searchType == "GRP")
				$("select[name=searchRealTimeCd]").show();
			else
				$("select[name=searchRealTimeCd]").hide();
		});
		$("#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"),'SOCKET_STATUS');
			return false;
		});
		buttonControl();
		//init(list);
		
		console.log(roleString);
		
		if(stgSvr) {
			var strRole		= roleString;
			var strVal		= "admin";
			var arrRoleID	= strRole.split(',');
			var flag		= false;
			
			$(arrRoleID).each(function(i, role) {
				if (role == strVal) {
					flag = true;
					return false;
				}
			});
			
			if(flag)
				$("#socketLink ,#socketLink2").show();
		} 
		
		list();
	
	});
</script>
</head>
<body>
	<div class="right_box">
		<div class="content_top">
			<ul>
			<li><a href=#>${rmsMenuPath}</a></li>
			</ul>						
	</div><!-- end content_top -->
		<div class="content_middle">
			<div class="search_wrap">
			<!--어드민만 보이게  -->
<%-- 					<span id="socketLink" style="cursor:pointer;font-size:13px;display:none;" onClick="goFilePage()"><b>[<%= localeMessage.getString("socketStat.fileDown") %>]</b></span>&nbsp;&nbsp;&nbsp; --%>
<%-- 					<span id="socketLink2" style="cursor:pointer;font-size:13px;display:none;" onClick="goPage()"><b>[<%= localeMessage.getString("socketStat.socketMoniPop") %>]</b></span>&nbsp;&nbsp; --%>
					<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 src="<c:url value="/img/btn_excel.png"/>" id="btn_excel" level="W"/> --%>
<%-- 					<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" /> --%>
			</div>
		<div class="title"><%= localeMessage.getString("socketStat.title") %></div>
		<form id="ajaxForm"  onsubmit="return false;">
			<table class="search_condition" cellspacing=0;>
					<tbody>
						<tr>
							<th style="width:180px;"><%= localeMessage.getString("socketStat.searchOpt") %></th>
							<td>
								<input type="radio" value="ALL" name="searchType" checked="checked"/><%= localeMessage.getString("socketStat.all") %>
								<input type="radio" value="ERR" name="searchType"/><%= localeMessage.getString("socketStat.error") %>  
							</td>
						</tr>
						<tr>
							<th style="width:180px;"><%= localeMessage.getString("adapterStat.adapterGroup") %></th>
							<td><input type="text" name="searchAdapterGroup" value="${param.searchAdapterGroup}" style="width:100%"></td>
						</tr>
					</tbody>
				</table>	
		</form>
			<!-- grid -->
		<table id="grid"></table>
		<div id="pager"></div>
					
		<div class="table_wrap col">
			<table width="100%" height="35px">
			</table>			
		</div>
		<!-- end content_middle -->
	</div>
	<!-- end right_box -->
</body>

</html>

