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

		$(document).ready(function() {
			$("input[name=startDate], input[name=endDate]").inputmask("9999-99-99 99:99", {'autoUnmask': true});

			var typingTimer;
			var doneTypingInterval = 500; // 1초

			$("input[name=startDate]").on('input', function() {
				clearTimeout(typingTimer);
				typingTimer = setTimeout(function() {
					updateEndTime();
				}, doneTypingInterval);
			});

			function updateEndTime() {
				var startDateStr = $("input[name=startDate]").val();
				if (isValidDateTimeFormat(startDateStr)) {
					var startDate = parseDate(startDateStr);
					var endDate = new Date(startDate.getTime() + 10 * 60000); // 10분 추가

					var endDateStr = formatDate(endDate);
					$("input[name=endDate]").val(endDateStr);
				}
			}

			function isValidDateTimeFormat(dateStr) {
				return /^\d{12}$/.test(dateStr);
			}

			function parseDate(dateStr) {
				var year = parseInt(dateStr.substr(0, 4));
				var month = parseInt(dateStr.substr(4, 2)) - 1; // JavaScript의 월은 0-11
				var day = parseInt(dateStr.substr(6, 2));
				var hour = parseInt(dateStr.substr(8, 2));
				var minute = parseInt(dateStr.substr(10, 2));

				return new Date(year, month, day, hour, minute);
			}

			function formatDate(date) {
				return date.getFullYear() +
						String(date.getMonth() + 1).padStart(2, '0') +
						String(date.getDate()).padStart(2, '0') +
						String(date.getHours()).padStart(2, '0') +
						String(date.getMinutes()).padStart(2, '0');
			}

			function setCurrentTime() {
				var now = new Date();
				var year = now.getFullYear();
				var month = String(now.getMonth() + 1).padStart(2, '0');
				var day = String(now.getDate()).padStart(2, '0');
				var hour = String(now.getHours()).padStart(2, '0');
				var minute = String(Math.floor(now.getMinutes() / 10) * 10).padStart(2, '0');

				var startDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
				$("input[name=startDate]").val(startDate);

				// 시작 시간을 Date 객체로 변환
				var startDateTime = new Date(year, month - 1, day, hour, minute);
				// 10분을 추가
				var endDateTime = new Date(startDateTime.getTime() + 10 * 60000);

				var endYear = endDateTime.getFullYear();
				var endMonth = String(endDateTime.getMonth() + 1).padStart(2, '0');
				var endDay = String(endDateTime.getDate()).padStart(2, '0');
				var endHour = String(endDateTime.getHours()).padStart(2, '0');
				var endMinute = String(endDateTime.getMinutes()).padStart(2, '0');

				var endDateStr = endYear + '-' + endMonth + '-' + endDay + ' ' + endHour + ':' + endMinute;
				$("input[name=endDate]").val(endDateStr);
			}

			$("#btn_current").click(function() {
				setCurrentTime();
			});

			setCurrentTime(); // 초기 로드 시 현재 시간 설정

			$("#btn_new").click(function(){
				var startDate = $("input[name=startDate]").val().replace(/[^0-9]/g, '').slice(0, 12);

				var postData = {
					cmd: "INSERT",
					startDate: startDate
				};

				$.ajax({
					type: "POST",
					url: url,
					data: postData,
					success: function(response) {
						if (response.success) {
							if (response.dataExists) {
								alert(response.message + " 저장된 데이터 수: " + response.count);
							} else {
								alert(response.message);
							}
						} else {
							alert("처리 중 오류가 발생했습니다: " + response.message);
						}
					},
					error: function(e){
						alert("서버 통신 오류: " + e.responseText);
					}
				});
			});

			$("input[name^=search]").keydown(function(key){
				if (key.keyCode == 13){
					$("#btn_new").click();
				}
			});

			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_new" level="W"><i class="material-icons">add</i> 등록</button>
		</div>
		<div class="title">로그 통계조회</div>

		<table class="search_condition" cellspacing=0;>
			<tbody>
			<tr>
				<th style="width:10%; min-width:90px;">10분 통계</th>
				<td>
					<input type="text" name="startDate" data-required data-warning="시작 날짜와 시간을 입력하여 주십시오." style="width:150px;"/>
					~
					<input type="text" name="endDate" data-required data-warning="종료 날짜와 시간을 입력하여 주십시오." style="width:150px;" disabled="disabled" />
					<button type="button" id="btn_current" class="cssbtn">현재</button>
				</td>
			</tr>
			</tbody>
		</table>

		<table id="grid"></table>

	</div><!-- end content_middle -->
</div><!-- end right_box -->
</body>
</html>