<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	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="/bap/adapter/socket/serverStatus.json" />';
var url_excel = '<c:url value="/common/noauth/excel/listGridToExcel.json" />';

function list() {
	var postData = getSearchForJqgrid("cmd", "LIST"); //jqgrid에서는 object 로
	$("#grid").setGridParam({
		url: '<c:url value="/bap/adapter/socket/serverStatus.json" />',
		postData: postData,
		datatype: 'json'
	}).trigger("reloadGrid");
}

function control(startStop, group, adapter) {
    var startStopStr = '기동';
    if ( startStop == 'STOP_SOCKET_SERVER' ) {
        startStopStr = '중지';
    }
    if ( confirm( "어댑터 그룹[" + group + "], 어댑터 명[" + adapter+ "]을 " + startStopStr + "하시겠습니까?" ) != true ) {
        return;
    }
   	var postData = new Array();
   	postData.push({ name: "cmd"       , value:"CONTROL"});
   	postData.push({ name: "startStop" , value:startStop});
   	postData.push({ name: "group"     , value:group});
   	postData.push({ name: "adapter"   , value:adapter});
   	$.ajax({
		type : "POST",
		url: url,
		data: postData,
		success: function(args) {
			list();
		},
		error: function(e) {
			alert(e.responseText);
		}
	});
}

$(document).ready(function() {
	$('#grid').jqGrid({
		datatype: "json",
		mtype: 'POST',
		url: url,
		postData : getSearchForJqgrid("cmd", "LIST"),
		colNames:['서버인스턴스명',
                  '업무구분',
                  '대외기관',
                  '어댑터그룹',
                  '어댑터명',
                  'IP',
                  'PORT',
                  '최대연결수',
                  '현재연결수',
                  '상태',
                  '상태',
                  '기동/종료'
                  ],
		colModel:[
			    { name : 'serverName'         , align:'center' , sortable : false  },
			    { name : 'processName'        , align:'left'  , width:200, sortable : false },
				{ name : 'organName'          , align:'left'  , width:200 },
				{ name : 'adptrBzwkGroupName' , align:'left'  , width:200 },
				{ name : 'adptrBzwkName'      , align:'left'  , width:200},
				{ name : 'ip'                 , align:'center'  },
				{ name : 'port'               , align:'center', width:70 },
				{ name : 'maxConnectioncount' , align:'center', width:70 ,
						formatter: function (cellvalue) {
				              if ( cellvalue == '0' ) {
				                  return '무제한';
                              } else {
                                  return cellvalue;
                              }
                          }
				},
				{ name : 'curConnectioncount' , align:'center', width:70},
				{ name : 'status'             , align:'center', hidden:true},
				{ name : 'status2'            , align:'center', width:70},
				{ name : 'startStop'          , align:'center'}
				],
		autowidth: true,
	    autoheight: true,
	    height: $("#container").height(),
	    //height: "500",
	    rowNum: 10000,
        jsonReader: {
             repeatitems:false
        },
        gridComplete: function() {
        	var ids = $(this).getDataIDs();
        	for(var i=0;i<ids.length;i++) {
        		var cl = ids[i];
        		var rowData = $(this).getRowData(cl); 
        		var adptrBzwkGroupName = rowData['adptrBzwkGroupName'];
        		var adptrBzwkName      = rowData['adptrBzwkName'];
        		var status             = rowData['status'];
        		var color = 'black';
        		//alert( 'status -->' + status );
        		if ( status == '중지' ) {
        		    be = "<input style='height:22px;width:60px;' type='button' value='기동' onclick=\"control('START_SOCKET_SERVER','"+adptrBzwkGroupName+"','"+adptrBzwkName+"');\" />";
        		    color = 'red';
        		    $(this).setRowData(ids[i],{startStop:be});
        		} else if ( status == '기동중' ) {
        		    be = "<input style='height:22px;width:60px;' type='button' value='중지' onclick=\"control('STOP_SOCKET_SERVER','"+adptrBzwkGroupName+"','"+adptrBzwkName+"');\" />";
        		    color = 'blue';
        		    $(this).setRowData(ids[i],{startStop:be});
        		}
        		$(this).setRowData(ids[i],{status2:'<span class="cellWithoutBackground" style="color:' + color + ';">' + status + '</span>'});
        	} 
        },
		loadComplete:function (d) {
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++) {
				$(this).setColProp(colModel[i].name, {sortable : false});
			}
		},
	});
	
	resizeJqGridWidth('grid','content_middle','1000');

	$("#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 merge = new Array();
		var skipCol = new Array();
		skipCol.push("startStop")
		gridToExcelSubmit(url_excel, "LIST_GRID_TO_EXCEL", $("#grid"), $("#ajaxForm"), "연결유지형 소켓 상태정보", merge, skipCol);
		return false;
	});
	
	buttonControl();
});
 
</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">
				<%-- <img src="<c:url value="/img/btn_excel.png"/>" id="btn_excel" level="R"/> --%>
				<img src="<c:url value="/img/btn_search.png"/>" alt="" id="btn_search" level="R" />
			</div>
		<div class="title">연결유지형 소켓 상태정보</div>

		<table class="search_condition" cellspacing=0;>
			<tbody>
				<tr>
					<th style="width:180px;">어댑터 업무그룹</th>
					<td ><input type="text" name="searchAdapterGroup" value="${param.searchAdapterGroup}" style="width:100%"></td>
					<th style="width:180px;">상태구분</th>
					<td width="10%">
						<div class="select-style">
						<select name="searchType">
							<option value="">전체</option>
							<option value="1">기동중</option>
							<option value="0">중지</option>
						</select>
						</div>
					</td>
					<th style="width:180px;">연결수</th>
					<td width="10%">
						<div class="select-style">
						<select name="searchCount">
							<option value="0">0</option>
							<option value="1">1</option>
							<option value="2">2</option>
							<option value="3">3</option>
							<option value="4">4</option>
						</select>
						</div>
					</td>
				</tr>
			</tbody>
		</table>


		<!-- grid -->
		<table id="grid"></table>
		<div id="pager"></div>
		<!-- end content_middle -->
	</div>
	<!-- end right_box -->
</body>
</html>