<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ page import="java.io.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ 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 >

var url      ='<c:url value="/onl/admin/session/sessionHistory.json" />';
var url_view ='<c:url value="/onl/admin/session/sessionHistory.view" />';

const historyDstcdOptionList = [
	{"text":"전체",		"value":""},
	{"text":"로그인",		"value":"LOGIN"},
	{"text":"로그아웃",	"value":"LOGOUT"},
	{"text":"중복로그인",	"value":"DUPLOGIN"},
	{"text":"자동로그아웃",	"value":"AUTOLOGOUT"},
	{"text":"웹소켓",		"value":"WEBSOCKET"}
];

const rspnsRsltDvsnOptionList = [
	{"text":"전체", 			"value":""},
	{"text":"정상", 			"value":"N"},
	{"text":"오류", 			"value":"E"},
	{"text":"대상시스템연결오류","value":"C"},
	{"text":"타임아웃", 		"value":"T"},
	{"text":"시스템오류", 		"value":"F"}
];

function getStartTime() {
	var now = new Date();
	var tm = right("0" + now.getHours(), 2);
	tm += right("0" + now.getMinutes(), 2);

	tm = (Math.floor(tm.substr(0, 2)) * 60) + Math.floor(right(tm, 2)) - 5;
	tm = right("0" + parseInt(tm/60), 2) + right("0" + tm%60, 2);
	return tm + '00';	
}

function getEndTime() {
	var now = new Date();
	var tm = right("0" + now.getHours(), 2);
	tm += right("0" + now.getMinutes(), 2);
	return tm + '59';
}

function toDateString(d) {
	var dateStr = d.replace(/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/ , '$4:$5:$6 $2/$3/$1');
	return dateStr;
}

//10분후 시간셋팅
function setTime(searchTime) {
	var endYYYYMMDD;
	var endHHMM;
	var yy = searchTime.substr(0,4);
	var mm = searchTime.substr(4,2); 
	var dd = searchTime.substr(6,2);
	var hh = searchTime.substr(8,2);
	var MM = searchTime.substr(10,2);
	var ms = Date.UTC(yy,mm-1,dd,hh,MM,00);
	var date = new Date(ms);
	date.setHours( date.getHours() - 9 );
	
	//10분 후 셋팅
	date.setMinutes( date.getMinutes() + 9 );
	
	endYYYYMMDD = date.getFullYear() + right("0" + (date.getMonth() + 1), 2) + right("0" + (date.getDate()), 2);
	endHHMM = right("0" + date.getHours() , 2) + right("0" + date.getMinutes() , 2) + "59";
	
	$("input[name=searchEndYYYYMMDD]").val(endYYYYMMDD);
	$("input[name=searchEndHHMM]").val(endHHMM);
}

function selectBoxSearchHistoryDstcd() {
	let $select = $(document.createElement('select'));
	$select.attr({ name : "searchHistoryDstcd" });
	
	historyDstcdOptionList.forEach(it => $select.append($(document.createElement('option')).attr({ value : it.value }).text(it.text)));
	
	$("#searchHistoryDstcd").append($select);
}

function historyDstcdFormatter (cellvalue, options, rowObject) {
	
	let option = historyDstcdOptionList.find(it => it.value == cellvalue);
	
	if(!option) {
		return cellvalue;
	} 
	
	return option.text;
}

function selectBoxSearchRspnsRsltDvsn() {
	
	let $select = $(document.createElement('select'));
	$select.attr({ name : "searchRspnsRsltDvsn" });
	
	rspnsRsltDvsnOptionList.forEach(it => $select.append($(document.createElement('option')).attr({ value : it.value }).text(it.text)));
	
	$("#searchRspnsRsltDvsn").append($select);
}

function rspnsRsltDvsnFormatter (cellvalue, options, rowObject){	
	
	let option = rspnsRsltDvsnOptionList.find(it => it.value == cellvalue); 
	
	if(!option) {
		return cellvalue;
	}
	
	return option.text;
}

$(document).ready(function() {
	
	$("input[name=searchStartYYYYMMDD],input[name=searchEndYYYYMMDD]").inputmask("yyyy-mm-dd",{'autoUnmask':true});
	$("input[name=searchStartHHMM],input[name=searchEndHHMM]").inputmask("hh:mm:ss",{'autoUnmask':true});
	
	$("input[name=searchStartYYYYMMDD],input[name=searchEndYYYYMMDD]").each(function(){
		if ($(this).val() == undefined || $(this).val() == null || $(this).val() == ""){			
			$(this).val(getToday());
		}
	});
	$("input[name=searchStartHHMM]").each(function(){
		if ($(this).val() == undefined || $(this).val() == null || $(this).val() == "") {
			//$(this).val(getStartTime());
			$(this).val("000000");
		}
	});
	$("input[name=searchEndHHMM]").each(function(){
		if ($(this).val() == undefined || $(this).val() == null || $(this).val() == "") {
			//$(this).val(getEndTime());
			$(this).val("235959");
		}
	});
	
	var gridPostData = getSearchForJqgrid("cmd","LIST"); //jqgrid에서는 object 로
	gridPostData['searchStartTime'] = $("input[name=searchStartYYYYMMDD]").val().replace(/-/gi,"") + $("input[name=searchStartHHMM]").val();
	gridPostData['searchEndTime'] = $("input[name=searchEndYYYYMMDD]").val().replace(/-/gi,"") + $("input[name=searchEndHHMM]").val();
	
	$('#grid').jqGrid({
		datatype : "json",
		mtype : "POST",
		url : url,
		postData : gridPostData,
		colNames:[
				'거래GUID',
				'사용자ID',
				'사용자UUID',
				'구분',
				'정상여부',
				'거래업무데이터',
				'거래시각'
				],
		colModel:[
				{ name : 'GUID' 		, align:'left'    	, width:120 },
				{ name : 'USERID' 		, align:'center'    , width:50 },
				{ name : 'UUID' 		, align:'left'    	, width:120 },
				{ name : 'HISTORYDSTCD'	, align:'left'    	, width:50, formatter: historyDstcdFormatter },
				{ name : 'RSPNSRSLTDVSN', align:'center'    , width:20, formatter: rspnsRsltDvsnFormatter },
				{ name : 'BODY' 		, align:'left'    	, width:150 },
				{ name : 'TRANTIME' 	, align:'center'	, width:80 }
				],
				
        jsonReader: {
             repeatitems:false
        },	          
        pager : $('#pager'),		
		rowNum : '${rmsDefaultRowNum }',
	    autoheight: true,
	    height: $("#container").height(),
		autowidth: true,
		viewrecords: true,
		rowList : eval('[${rmsDefaultRowList}]'),		
		gridComplete:function (d){
			var colModel = $(this).getGridParam("colModel");
			for(var i = 0 ; i< colModel.length; i++){
				$(this).setColProp(colModel[i].name, {sortable : false});	//그리드 헤더 화살표 삭제(정렬X)		
			}
		}
	});
	
	resizeJqGridWidth('grid','content_middle','1000');
	
	$("#btn_search").click(function() {
		var postData = getSearchForJqgrid("cmd","LIST");
		var startCreateTime = $("input[name=searchStartYYYYMMDD]").val().replace(/-/gi,"") + $("input[name=searchStartHHMM]").val();
		var endCreateTime = $("input[name=searchEndYYYYMMDD]").val().replace(/-/gi,"") + $("input[name=searchEndHHMM]").val();
		postData['searchStartTime'] = startCreateTime;
		postData['searchEndTime'] = endCreateTime;
		$("#grid").setGridParam({ postData: postData, page : "1" }).trigger("reloadGrid");
	});
	
	$("[name^=search]").keydown(function(key){
		if (key.keyCode == 13){
			$("#btn_search").click();
		}
	});
	
	selectBoxSearchHistoryDstcd();
	selectBoxSearchRspnsRsltDvsn();
	
	$("#startDatepicker").datepicker();
	$("#endDatepicker").datepicker();
	
	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">
				<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("sessHis.title") %></div>
			<form id="ajaxForm">
				<table class="search_condition" cellspacing=0;>
					<tbody>
						<tr>
							<th style="width:100px;"><%= localeMessage.getString("sessHis.userID") %></th>   <!-- 3자리만 입력가능하게  -->
							<td style="width:250px;">
								<input type="text" name="searchUserId" value="${param.searchUserId}" >
							</td> 
							<th style="width:100px;"><%= localeMessage.getString("sessHis.div") %></th>
							<td style="width:250px;">
								<div class="select-style" id="searchHistoryDstcd">
								
								</div>
							</td>
							<th style="width:100px;"><%= localeMessage.getString("sessHis.norStat") %></th>
							<td style="width:250px;">
								<div class="select-style" id="searchRspnsRsltDvsn">
								
								</div>
							</td>
							<th style="width:100px;"><%= localeMessage.getString("sessHis.tranTm") %></th>
							<td style="width: 25%;">
								<input type="text" name="searchStartYYYYMMDD" id="startDatepicker" readonly="readonly" value="" size="10" style="width:80px; border:1px solid #ebebec;">
								<input type="text" name="searchStartHHMM" value="" size="8" style="width:80px; border:1px solid #ebebec;">
								~
								<input type="text" name="searchEndYYYYMMDD" value="" id="endDatepicker" size="10" readonly="readonly" style="width:80px; border:1px solid #ebebec;">
								<input type="text" name="searchEndHHMM" value="" size="8" style="width:80px; border:1px solid #ebebec;">
							</td>
						</tr>
					</tbody>
				</table>
			</form>
			<table id="grid" ></table>
			<div id="pager"></div>
		</div>
	</div>
</body>
</html>