<%@ 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_excel = '<c:url value="/common/noauth/excel/listGridToExcel.json" />';

$(document).ready(function() {
	$('#grid').jqGrid({
		datatype:"json",
		mtype: 'POST',
		url: '<c:url value="/bap/adapter/socket/instStatus.json" />',
		postData : { cmd : 'LIST' },
		colNames:[
				  '업무구분',
                  '대외기관',
                  'IP',
                  'PORT',
                  '사용여부',
                  '상태',
                  '연결확인',
                  '연결상태'
                  ],
		colModel:[
				{ name : 'BJOBBZWKNAME'       , align:'left', width:200, sortable:false },
				{ name : 'OSIDINSTINAME'      , align:'left', width:200  },
				{ name : 'LNKGIPINFONAME'     , align:'center'  },
				{ name : 'LNKGPORTINFONAME'   , align:'center'  },
				{ name : 'THISMSGUSEYN'       , align:'center',
				          formatter: function (cellvalue) {
				              if ( cellvalue == '1' ) {
				                  return '사용';
                              } else {
                                  return '<span style="color:red">사용안함</span>';
                              }
                          }
				},
				{ name : 'TCIRTLNKGSTUSCD'    , align:'center' ,
			          formatter: function (cellvalue) {
			              if ( cellvalue == '1' ) {
			                  return '정상';
                        } else {
                            return '<span style="color:red">장애</span>';
                        }
                    }
				},
				{ name : 'serverCheck'    , align:'center' , hidden: true,
					formatter: function (cellValue, options, rowObject) {
						var ip = rowObject.LNKGIPINFONAME;
						var port = rowObject.LNKGPORTINFONAME;
						var used = rowObject.THISMSGUSEYN;
						var be = "";
						if( !isNaN(parseFloat(port)) && used == 1 ) {
							be = "<input style='height:22px; width:60px; padding-top:2px;' type='button' value='실행'";
							be += "onclick=\"serverCheck('"+options.rowId+"');\"";
							be += " />";
						}
						return be;
					}
				},
				{ name : 'serverConnected', align: 'center', hidden: true }
				],
		autowidth: true,
	    autoheight: true,
	    height: $("#container").height(),
	    //height: "500",
	    rowNum: 10000,
        jsonReader: {
             repeatitems:false
        },
		gridComplete: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() {
		$("#grid").trigger("reloadGrid");
	});

	$("#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();
		gridToExcelSubmit(url_excel, "LIST_GRID_TO_EXCEL", $("#grid"), $("#ajaxForm"), "연결유지형 소켓 상태정보", merge);
		return false;
	});

	$("#btn_operate").click(function() {
		var list = $('#grid').getDataIDs();
		for( var i = 1; i <= list.length; i++ ) {
			serverCheck(i);
		}
	});

	buttonControl();
});

function serverCheck(rowId) {
	var url ='<c:url value="/bap/admin/work/systemInstMan.json" />';

	var data = $('#grid').getRowData(rowId);
	var ip = data.LNKGIPINFONAME;
	var port = data.LNKGPORTINFONAME;
	var used = data.THISMSGUSEYN;

	$('#grid').jqGrid('setCell', rowId, 'serverConnected', '-');

	if( isNaN(parseFloat(port)) || used.indexOf('사용안함') > -1  ) return false;

	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		data: { cmd: 'CHECK_CONNECTION',
				ip: ip,
				port: port
		},
		success: function(json) {
			if( json.result.indexOf("Connected") > -1 ) {
				$('#grid').jqGrid('setCell', rowId, 'serverConnected', '<span style="color:blue;">성공</span>');
			} else {
				$('#grid').jqGrid('setCell', rowId, 'serverConnected', '<span style="color:red;">실패</span>');
			}
		},
		error: function(e) {
			$('#grid').jqGrid('setCell', rowId, 'serverConnected', '<span style="color:red;">실패</span>');
		}
	});
}
</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_operate.png"/>" id="btn_operate" level="R"/> --%>
				<%-- <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>
		<!-- grid -->
		<table id="grid"></table>
		<div id="pager"></div>
		<!-- end content_middle -->
	</div>
	<!-- end right_box -->
</body>
</html>