<%@ page language="java" contentType="text/html; charset=utf-8"%>

<%@page import="com.eactive.eai.rms.common.datasource.DataSourceType"%>
<%@page import="com.eactive.eai.rms.common.datasource.DataSourceTypeManager"%>
<%@page import="com.eactive.eai.common.util.SystemUtil"%>
<%@page import="com.eactive.eai.rms.env.EmsConfig"%>
<%@ 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" %>
<c:set var="themeColor" value="<%=System.getProperty(\"theme.color\")%>" scope="session" />

<%
	String sysMode = (String)System.getProperty("eai.systemmode");
	String sysModeName = "";
	/* System.out.println("##### SERVER_TYPE ====>"+sysMode); */
	if( sysMode == null ) {
		sysMode = "";
	}
	if (sysMode.equals("T")) {
		sysModeName = "테스트";
	} else if (sysMode.equals("D")){
		sysModeName = "개발";
	}else {
		sysModeName = "";
	}

	String systemModeTitle = SystemUtil.getSystemModeTitle();
	String drmode = System.getProperty(EmsConfig.DR_MODE);
	if ("Y".equals(drmode)) {
		systemModeTitle = "[DR] ";
	}
%>

<%
	String resultMsg = (String)session.getAttribute("resultMsg");
	session.removeAttribute("resultMsg");
	session.removeAttribute("SSO_ID");
	request.removeAttribute("SSO_ID");


	if( resultMsg == null ) {
		resultMsg = "";
	}
%>

<html>
<head>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
	<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
	<META HTTP-EQUIV="Cache-control" CONTENT="no-cache">
	<link rel="icon"
		  href="<c:url value="/img/ic_eai_favi_${themeColor}.png"/>"
		  type="image/ico" sizes="16x16" />
	<title><%=systemModeTitle%><%=localeMessage.getString("screen.title")%></title>
	<link rel="stylesheet" href="<c:url value="/css/login-style.css"/>">
	<link rel="stylesheet" href="<c:url value="/css/login-fontawesome_all.css"/>">
	<link rel="stylesheet" href="<c:url value="/addon/bootstrap-4.6.2/css/bootstrap.min.css"/>">
	<script src="<c:url value="/js/jquery-1.12.1.min.js"/>"></script>
	<script src="<c:url value="/addon/bootstrap-4.6.2/js/bootstrap.bundle.min.js"/>"></script>
	<script type="text/javascript">
		if( self.name == 'mainFrame' || self.name == 'leftFrame' || self.name == 'topFrame') {
			parent.window.location.href='<c:url value="/"/>';
		}

		// SMS 인증 관련 변수
		var smsAuthTimer = null;
		var resendTimer = null;

		function fncPreMain() {
			if ($("input[name=userId]").val() ==null || $("input[name=userId]").val().length == 0 ){
				alert("<%= localeMessage.getString("login.checkid") %>");
				$("input[name=userId]").trigger("focus");
				return;
			}
			if ($("input[name=password]").val() ==null || $("input[name=password]").val().length == 0 ){
				alert("<%= localeMessage.getString("login.checkpwd1") %>");
				$("input[name=password]").trigger("focus");
				return;
			}
			if ($("select[name=serviceType]").val() ==null|| $("select[name=serviceType]").val().length == 0 ){
				alert("<%= localeMessage.getString("login.checkservicetype") %>");
				$("select[name=serviceType]").trigger("focus");
				return;
			}
			sessionStorage["serviceType"]= $("select[name=serviceType]").val();

			// AJAX 로그인 요청
			$.ajax({
				url: '<c:url value="/login-ajax.do"/>',
				type: 'POST',
				data: $('#loginForm').serialize(),
				dataType: 'json',
				success: function(response) {
					if (response.changePassword) {
						showChangeInitPassword(response);
					}
					else if (response.smsAuthRequired) {
						// SMS 인증 필요
						showSmsAuthModal(response);
					} else if (response.success) {
						// 직접 로그인 성공
						window.location.href = response.redirectUrl;
					} else {
						// 로그인 실패
						alert(response.errorMessage || '로그인에 실패했습니다.');
					}
				},
				error: function(xhr, status, error) {
					console.error('Login error:', error);
					alert('로그인 처리 중 오류가 발생했습니다.');
				}
			});
		}
		
		function showChangeInitPassword(data) {
			$('input[name="resetUserId"]').val($('input[name="userId"]').val());
			$('input[name="resetPassword"]').val($('input[name="password"]').val());
			$('#changeInitPassword').hide();
			$('#pwdChgModal').modal('show');
		}

		// SMS 인증 모달 표시
		function showSmsAuthModal(data) {
			$('#smsAuthMaskedPhone').text(data.maskedPhone);
			$('#smsAuthCode').val('');
			startExpiryTimer(data.expiresAt);
			startResendTimer(data.resendableAt);
			$('#smsAuthModal').modal('show');
		}

		// 유효시간 타이머
		function startExpiryTimer(expiresAt) {
			clearInterval(smsAuthTimer);
			updateRemainingTime(expiresAt);
			smsAuthTimer = setInterval(function() {
				var remaining = Math.max(0, Math.floor((expiresAt - Date.now()) / 1000));
				$('#smsAuthRemainingTime').text(remaining);
				if (remaining <= 0) {
					clearInterval(smsAuthTimer);
					alert('인증 시간이 만료되었습니다. 다시 시도해주세요.');
					$('#smsAuthModal').modal('hide');
				}
			}, 1000);
		}

		function updateRemainingTime(expiresAt) {
			var remaining = Math.max(0, Math.floor((expiresAt - Date.now()) / 1000));
			$('#smsAuthRemainingTime').text(remaining);
		}

		// 재발송 타이머
		function startResendTimer(resendableAt) {
			$('#btnSmsResend').prop('disabled', true);
			clearInterval(resendTimer);
			updateResendButtonText(resendableAt);
			resendTimer = setInterval(function() {
				var remaining = Math.max(0, Math.ceil((resendableAt - Date.now()) / 1000));
				if (remaining <= 0) {
					$('#btnSmsResend').prop('disabled', false).text('재발송');
					clearInterval(resendTimer);
				} else {
					$('#btnSmsResend').text('재발송 (' + remaining + '초)');
				}
			}, 1000);
		}

		function updateResendButtonText(resendableAt) {
			var remaining = Math.max(0, Math.ceil((resendableAt - Date.now()) / 1000));
			if (remaining > 0) {
				$('#btnSmsResend').text('재발송 (' + remaining + '초)');
			} else {
				$('#btnSmsResend').text('재발송');
			}
		}

		// SMS 인증번호 재발송
		function resendSmsAuthCode() {
			$.ajax({
				url: '<c:url value="/sms-auth/resend.json"/>',
				type: 'POST',
				dataType: 'json',
				success: function(data) {
					if (data.sent) {
						startExpiryTimer(data.expiresAt);
						startResendTimer(data.resendableAt);
						alert('인증번호가 재발송되었습니다.');
					} else {
						alert(data.errorMessage || '재발송에 실패했습니다.');
					}
				},
				error: function() {
					alert('재발송 요청 중 오류가 발생했습니다.');
				}
			});
		}

		// SMS 인증번호 확인
		function verifySmsAuthCode() {
			var code = $('#smsAuthCode').val();
			if (!code || code.length !== 6) {
				alert('6자리 인증번호를 입력해주세요.');
				$('#smsAuthCode').focus();
				return;
			}

			$.ajax({
				url: '<c:url value="/sms-auth/verify.json"/>',
				type: 'POST',
				data: { authCode: code },
				dataType: 'json',
				success: function(data) {
					if (data.success) {
						$('#smsAuthModal').modal('hide');
						window.location.href = data.redirectUrl;
					} else {
						alert(data.errorMessage || '인증에 실패했습니다.');
					}
				},
				error: function() {
					alert('인증 요청 중 오류가 발생했습니다.');
				}
			});
		}

		// SMS 인증 취소
		function cancelSmsAuth() {
			clearInterval(smsAuthTimer);
			clearInterval(resendTimer);
			$('#smsAuthModal').modal('hide');
		}
		
		function checkPwd(){
			if ($("input[name=resetUserId]").val() == null || $("input[name=resetUserId]").val().length == 0 ){
				alert("<%= localeMessage.getString("login.checkid") %>");
				$("input[name=resetUserId]").trigger("focus");
				return false;
			}
			if ($("input[name=resetPassword]").val() == null || $("input[name=resetPassword]").val().trim().length == 0 ){
				alert("<%= localeMessage.getString("login.checkpwd1") %>");
				$("input[name=resetPassword]").trigger("focus");
				return false;
			}
			if ($("input[name=changePassword]").val() == null || $("input[name=changePassword]").val().trim().length == 0 ){
				alert("<%= localeMessage.getString("login.checkpwd2") %>");
				$("input[name=changePassword]").trigger("focus");
				return false;
			}
			if ($("input[name=confirmPassword]").val() == null || $("input[name=confirmPassword]").val().trim().length == 0 ){
				alert("<%= localeMessage.getString("login.checkpwd3") %>");
				return false;
			}
			if ($("input[name=changePassword]").val() != $("input[name=confirmPassword]").val()){
				alert("<%= localeMessage.getString("login.checkpwd4") %>");
				$("input[name=confirmPassword]").trigger("focus");
				return false;
			}
			if ($("input[name=resetUserId]").val() == $("input[name=changePassword]").val()){
				alert("<%= localeMessage.getString("login.checkpwd5") %>");
				$("input[name=changePassword]").trigger("focus");
				return false;
			}

			return true;
		}
		
		$(document).ready(function() {
			$("input[name=userId]").keydown(function(event){
				if ( event.which == 13 ) {
					fncPreMain();
				}
			});
			$("input[name=password]").keydown(function(event){
				if ( event.which == 13 ) {
					fncPreMain();
				}
			});
			$("input[name=changePassword]").keydown(function(event){
				if ( event.which == 13 ) {
					$('#modalLoginForm').submit();
				}
			});
			$("input[name=confirmPassword]").keydown(function(event){
				if ( event.which == 13 ) {
					$('#modalLoginForm').submit();
				}
			});
			// Password Change 링크로 직접 열 때만 초기화 (JS에서 modal('show') 호출 시 relatedTarget 없음)
			$('#pwdChgModal').on('show.bs.modal', function(event) {
				if (event.relatedTarget) {
					$('#modalLoginForm')[0].reset();
					$('#changeInitPassword').show();
				}
			});

			$("select[name=serviceType]").change(function(){
				fncPreMain();
			});

			$("#btn_login").click(function(){
				fncPreMain();
			});
			
			$("#btn_sso_login").click(function(){
				fncSsoLogin();
			});

			// SMS 인증번호 입력 필드에서 엔터키 누르면 확인
			$("#smsAuthCode").keydown(function(event){
				if ( event.which == 13 ) {
					verifySmsAuthCode();
				}
			});
		});

		function fncSsoLogin() {
			sessionStorage["serviceType"] = $("select[name=serviceType]").val();
			location.href = '<c:url value="/sso/login.do"/>';
		}
	</script>
</head>
<body>

<div class="container mt-5">
	<div class="row justify-content-left mb-3">
		<div class="col-md-12 text-center">
			<img style="width: 20%;opacity: 0.4;" src='<c:url value="/img/logo_eactive.png"/>'/>
		</div>
	</div>
	<div class="row justify-content-center">
		<div class="col-md-8 text-center">
			<h1 style="vertical-align: middle !important" class="h1"><span class="text-danger">e</span><span class="text-info">Link Monitoring Admin</span></h1>
		</div>
	</div>
	<div class="row justify-content-center">
		<div class="col-md-7 col-lg-5">
			<div class="login-wrap p-4 p-md-5">
				<div class="icon text-danger d-flex align-items-center justify-content-center">
					<img style="width: 40px;" src='<c:url value="/img/login/user.png"/>'/>
				</div>
				<h3 class="text-center mb-4">Log In</h3>
				<form name="form" id="loginForm" action="<c:url value="/login.do"/>" method="post">
					<div class="form-group">
						<input type="text" name="userId" class="form-control rounded-left" placeholder="Id" required>
					</div>
					<div class="form-group d-flex">
						<input type="password" name="password" class="form-control rounded-left" placeholder="Password" autocomplete="off" required>
					</div>
					<div class="form-group">
						<select  name="serviceType" class="form-control rounded-left" placeholder="system" required>
							<%
								for (DataSourceType dt : DataSourceTypeManager.getDynamicDataSourceTypes()){
							%>
							<option value="<%=dt.getName()%>"><%=dt.getText()%></option>
							<%
								}
							%>
						</select>
					</div>
					<div class="form-group">
						<input type="button" id ="btn_login" class="form-control btn btn-primary rounded submit px-3" value="Login">
					</div>
					<div style="text-align: right !important;" class="form-group">
						<a style="font-size: 12px !important;" href="#pwdChgModal" data-toggle="modal" >Password Change</a>
					</div>
					<!-- SSO 로그인 버튼 (비상모드 시 숨김) -->
					<c:if test="${not emergencyMode}">
					<!-- <div class="form-group">
						<input type="button" id="btn_sso_login" class="form-control btn btn-success rounded submit px-3"
							   value="SSO Login">
					</div> -->
					</c:if>
					<p style="color:red;"><%=resultMsg %></p>
				</form>

			</div>
		</div>
	</div>
</div>

<!-- 비밀번호 변경 모달 -->
<div class="modal fade" id="pwdChgModal" tabindex="-1" role="dialog" aria-labelledby="pwdChgModalLabel" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="pwdChgModalLabel">Password 설정</h5>
				<button class="close" type="button" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">X</span>
				</button>
			</div>
			<form  id="modalLoginForm" action="<c:url value="/changePassword.do"/>" method="post" onsubmit="return checkPwd()" novalidate>
				<div class="modal-body">
					<div id="changeInitPassword">
						<div class="form-group d-flex">
							<input type="text" name="resetUserId" class="form-control rounded-left" placeholder="User Id" required>
						</div>
						<div class="form-group d-flex">
							<input type="password" name="resetPassword" class="form-control rounded-left" placeholder="<%= localeMessage.getString("login.placeholderCurrentPassword") %>" autocomplete="off" required>
						</div>
					</div>
					<p><%= localeMessage.getString("login.toChangePasswordMessage") %></p>
					<div class="form-group d-flex">
						<input type="password" name="changePassword" class="form-control rounded-left" placeholder="<%= localeMessage.getString("login.placeholderChangePassword") %>" autocomplete="off" required>
					</div>
					<div class="form-group d-flex">
						<input type="password" name="confirmPassword" class="form-control rounded-left" placeholder="<%= localeMessage.getString("login.placeholderConfirmationPassword") %>" autocomplete="off" required>
					</div>
					<span style="font-size:0.8em; color:red">
						※ 7글자 이상 & 영문/숫자/특수문자 2종류 이상
					</span>
				</div>
				<div class="modal-footer">
					<button type="submit" id="changePwd" class="form-control btn btn-primary rounded submit px-3">변경</button>
				</div>
			</form>
		</div>
	</div>
</div>

<!-- SMS 인증 모달 -->
<div class="modal fade" id="smsAuthModal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">SMS 인증</h5>
			</div>
			<div class="modal-body">
				<p>인증번호가 <strong><span id="smsAuthMaskedPhone"></span></strong>로 발송되었습니다.</p>
				<div class="form-group">
					<input type="text" id="smsAuthCode" class="form-control"
						   placeholder="인증번호 6자리" maxlength="6"
						   onkeypress="return event.charCode >= 48 && event.charCode <= 57">
				</div>
				<p class="text-muted">남은 시간: <span id="smsAuthRemainingTime" class="text-danger font-weight-bold">60</span>초</p>
			</div>
			<div class="modal-footer">
				<button type="button" id="btnSmsResend" class="btn btn-secondary" onclick="resendSmsAuthCode()" disabled>재발송</button>
				<button type="button" class="btn btn-primary" onclick="verifySmsAuthCode()">확인</button>
				<button type="button" class="btn btn-danger" onclick="cancelSmsAuth()">취소</button>
			</div>
		</div>
	</div>
</div>
</body>

</html>

