<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" session="false"%>
<%@ include file="/WEB-INF/jsp/include/taglib.jsp"%>
<!DOCTYPE html>
<html
		lang="kr"
		class="light-style layout-navbar-fixed layout-menu-fixed "
		dir="ltr"
		data-theme="theme-default"
		data-assets-path="<c:url value="/webjars/"/>"
		data-template="vertical-menu-template-starter">
<head>
	<meta charset="UTF-8">
	<title>북촌 통행제한 관리 시스템</title>

	<!-- Favicon -->
	<link rel="icon" type="image/x-icon" href="<c:url value="/webjars/image/favicon.ico"/>" />

	<!-- Fonts -->
	<link rel="stylesheet" href="<c:url value="/webjars/applib/font/notoSansKr/fontface.css"/>" />

	<!-- Icons. Uncomment required icon fonts -->
	<link rel="stylesheet" href="<c:url value="/webjars/3rd-party/sneat/fonts/boxicons.css"/>" />
	<link rel="stylesheet" href="<c:url value="/webjars/3rd-party/sneat/fonts/fontawesome.css"/>" />
	<link rel="stylesheet" href="<c:url value="/resources/fonts/xit-icon.css"/>" />
	<!-- Core CSS -->
	<link rel="stylesheet" href="<c:url value="/webjars/3rd-party/sneat/css/rtl/core.css"/>" class="template-customizer-core-css" />
	<link rel="stylesheet" href="<c:url value="/webjars/3rd-party/sneat/css/rtl/theme-default.css"/>" class="template-customizer-theme-css" />
	<link rel="stylesheet" href="<c:url value="/webjars/css/styles.css"/>" />

	<link rel="stylesheet" href="<c:url value="/webjars/applib/css/overwrite.css?${ver}"/>" />
	<link rel="stylesheet" href="<c:url value="/webjars/applib/css/applib.css?${ver}"/>" />

	<link rel="stylesheet" href="<c:url value="/resources/css/fims.css?${ver}"/>" />

	<link rel="stylesheet" href="<c:url value="/resources/css/fims-login.css?${ver}"/>" />
	<!-- Vendors CSS -->
	<link rel="stylesheet" href="<c:url value="/webjars/3rd-party/sneat/libs/perfect-scrollbar/perfect-scrollbar.css"/>" />
	<link rel="stylesheet" href="<c:url value="/webjars/3rd-party/sneat/libs/bootstrap-datepicker/bootstrap-datepicker.css"/>" />

</head>
<body>
<div class="login_bg" hidden>
	<div class="d-flex flex-row justify-content-between" style="padding: 2rem 1rem">
		<div>
			<img src="<c:url value='resources/img/login/mainLogo_01.png'/>" alt="Logo Image" style="width: 205px !;" />
			<span>북촌 통행제한 관리 시스템</span>
		</div>
		<%-- <button id="btnDownMenual" type="button" onclick="FimsSupport.fnMenualDownload();" class="btn btn-primary" hidden>사용자 메뉴얼</button> --%>
	</div>

	<div class="login_text">
		<p class="main_img"><img src="<c:url value='/'/>resources/img/login/loginDeco_02.png" alt=""></img></p>
		<form id="formAuthentication" method="post">
			<input type="hidden" id="cntnSeCd" name="cntnSeCd" />
			<div class="input_wrap">
				<div class="input-group" id="orgDiv">

				</div>
				<div class="input-group">
					<input type="text" id="userId" value="${cookie['userAccount'].getValue()}"
						required title="계정 입력" class="typeText required form-control inp-long" placeholder="계정" autofocus/>
				</div>
				<div class="input-group input-group-merge mt-2">
					<input type="password" id="password"
						required title="비밀번호 입력" class="typePassword required form-control inp-long" placeholder="비밀번호" autocomplete="false" />
					<span class="input-group-text cursor-pointer"><i class="bx bxs-lock"></i></span>
				</div>
				<div class="mt-3 form-chcek">
	            	<input id="remember" type="checkbox" class="form-check-input">
	            	<label class="form-check-label">로그인 정보 유지</label>
	            </div>
				<div class="input_wrap2">
					<!--
					<a href="#" onclick="javascript:fn_FindId(); return false;">아이디 찾기</a>
					<a href="#" onclick="javascript:fn_FindPw(); return false;" class="line">비밀번호 찾기</a>
					-->
					<a href="#" onclick="javascript:fnOpenSignup_step1(); return false;" class="line">회원가입</a>
				</div>
			</div>
			<input type="hidden" name="message" value="${message}" />
			<input type="hidden" name="userSe"  value="USR"/>
			<input name="j_username" type="hidden"/>
		</form>
		<div class="btn_wrap">
			<input type="button" onclick="login()" class="typeButton" title="로그인" value="로그인" />
		</div>
	</div>
</div>

<template id="pbofcLoginTemplate">
	<select id="orgID" name="orgID" required title="시군구명" class="form-select mb-2 h-px-40 no-max-w">
		<option value="">[기관 선택]</option>
		<option value="ADMIN">시스템관리</option>
		<c:forEach items="${sggList}" var="item">
			<option value="${item.SGG_CD}"
			<c:if test="${item.USER_USE_YN == 'N'}"> disabled </c:if>
			>${item.SGG_NM}</option>
		</c:forEach>
	</select>
</template>
<template id="bzentLoginTemplate">
	<input type="hidden" id="orgID" name="orgID" required title="시군구명" value="${sysSggCd}" />
</template>

<template id="selectOrgDialogTemplate">
	<select class="form-select form-control-sm" id="selectOrg">
		<option value="">선택</option>
		<c:forEach items="${sggList}" var="item">
			<option value="${item.SGG_CD}">${item.SGG_NM}</option>
		</c:forEach>
	</select>
</template>

<jsp:include page="/WEB-INF/jsp/include/tail.jsp" />

<script type="text/javascript">

${functions}

let sysSgg = "${sysSggCd}";
let cookieOrg = "${cookie['userOrg'].getValue()}";
let cookieUserId = "${cookie['userAccount'].getValue()}";


function login() {
	if (!$("#formAuthentication input,select").validInputs()) return;

	var params = {
		account: $("#userId").val().toUpperCase(),
		password:$("#password").val(),
		orgID: $("#orgID").val(),
		cntnSeCd: $("#cntnSeCd").val(),
		rememberCredentials: $("#remember").prop("checked")
	};

	json.post({
		url:wctx.url("/login.do"),
		data:params,
		success:function(resp) {
			if (!(resp.authenticated)) {
				dialog.alert({
					content:resp.reason,
					init : function() {
						AppSupport.setDialogZindex();
						focusClose();
					},
					onClose:() => $("#userId").focus()
				});
				return;
			}

			if(resp.message){
				dialog.alert({
					content : resp.message,
					init : function() {
						AppSupport.setDialogZindex();
						focusClose();
					},
					onOK : function() {
						wctx.home();
					}
				});
				return;
			}

			wctx.home();

		}
	});
}

/**
 * 회원가입 팝업
 */
function fnOpenSignup_step1(){
	if($("#cntnSeCd").val() == "01"){
		dialog.open({
			id : "selectOrgDialog",
			title : "기관 선택",
			content : document.getElementById("selectOrgDialogTemplate").innerHTML,
			size : "sm",
			init : () => {
			},
			onOK : () => {
				if($("#selectOrg").val() == ""){
					alert("기관을 선택하세요.");
					return;
				}
				dialog.close("selectOrgDialog");

				fnOpenSignup_step2($("#selectOrg").val(),"01");
			},
			onClose : () => {}
		});
	} else if($("#cntnSeCd").val() == "02"){
		fnOpenSignup_step2("${sysSggCd}","02");
	}

}

function fnOpenSignup_step2(sggCd,cntnSeCd){
	var popupWidth = 950;
	var popupHeight = 500;
	var popupX = (window.screen.width / 2) - (popupWidth / 2);
	var popupY= (window.screen.height / 2) - (popupHeight / 2);

	var popup = window.open(wctx.url("/signupPage.do")+"?sggCd="+sggCd+"&cntnSeCd="+cntnSeCd,
			"regAccount",
			"width="+popupWidth+", height="+popupHeight+", left="+popupX+", top="+popupY);

	$.blockUI({message : ""});
	popup.onload = () => {
		popup.addEventListener('beforeunload', function() {
			$.unblockUI();
		});
	};
}

/**
 * 아이디 찾기 팝업
 */
function fn_FindId(){
	window.open(".do", "findId", {width: 500, height:300});
}

/**
 * 비밀번호 찾기 팝업
 */
function fn_FindPw(){
	window.open(".do", "findPw", {width: 500, height:330});
}

function fnIpInfo(flag){
	$("#cntnSeCd").val("");
	$("#orgDiv").html("");
	$("#userId").val("");
	$("#password").val("");
	$("#btnDownMenual").attr("hidden","hidden");
	$("#userFor").attr("hidden","hidden");
	$("#remember").prop("checked", false);

	let data = {};
	if(flag != null){
		data.flag = flag;
	}

	PBOFC_login();

}

//공무원 로그인
function PBOFC_login(){

	$(".login_bg").removeAttr("hidden");
	$("#btnDownMenual").removeAttr("hidden");

	$("#cntnSeCd").val("01");
	$("#orgDiv").html(document.getElementById("pbofcLoginTemplate").innerHTML);

	let hasCookie = (cookieOrg != "" && cookieUserId != "");

	let selectValue = "";
	let isSelectable = false;

	if(hasCookie){
		selectValue = cookieOrg;
		isSelectable = $("#orgID option[value='"+selectValue+"']").not("[disabled]").length;
		if(isSelectable){
			$("#orgID").val(cookieOrg);
			$("#userId").val(cookieUserId);
			$("#remember").prop("checked", true);
			$("#password").focus();
			return;
		}
	}

	selectValue = sysSgg;
	isSelectable = $("#orgID option[value='"+selectValue+"']").not("[disabled]").length;
	if(isSelectable){
		$("#orgID").val(selectValue);
		$("#userId").focus();
	} else {
		$("#orgID").focus();
	}
}




$("#formAuthentication input").onEnterPress(login);


$(function(){
	fnIpInfo();
});

</script>
</body>
</html>
