<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<div class="container">
    <div class="subHeader">
        <h1 class="logo">안동시 교통정보센터</h1>
        <div class="pageLocation">
            <ul>
                <li><a href="/main/main.do" class="home">홈</a></li>
                <li>
                    <div class="dropdown">
                        <button id="Btn1" class="dropbtn">관리자 페이지</button>
                        <div id="Dropdown1" class="dropdown-content">
                        </div>
                    </div>
                </li>
                <li>
                    <div class="dropdown">
                        <button id="Btn2" class="dropbtn">로그인</button>
                        <div id="Dropdown2" class="dropdown-content">
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <div class="subContent">
        <div class="pageTitle">
            <h2>로그인</h2>
            <p>역사와 미래가 공존하는 <span>안동</span></p>
        </div>
        <div class="adminWrap">
            <h2 class="admin-tit">
                홈페이지 관리를 위해서는 관리자 <strong>로그인</strong>이 필요합니다.
            </h2>
            <ul class="login-form">
                <li>
                    <input type="text" id="email" name="email" value="" placeholder="아이디를 입력하세요" title="아이디">
                </li>
                <li>
                    <input type="password" id="password" name="password" value="" placeholder="비밀번호를 입력하세요" title="비밀번호">
                </li>
                <li><button type="button" name="" id="login" class="formBtn">로그인</button></li>
            </ul>
        </div>
    </div>
</div>

<!--//contents-->
<script type="text/javascript">

    $("#login").click(function () {

        if ($("#email").val() == "") {
            alert("아이디를 정확하게 입력하세요.");
            $("#email").focus();
            return false;
        }

        if ($("#password").val() == "") {
            alert("비밀번호를 정확하게 입력하세요.");
            $("#password").focus();
            return false;
        }

        const param = {
            email : encodeURIComponent($("#email").val()),
            password : encodeURIComponent($("#password").val())
        };
        loginCheck(param)
        //$("#loginForm").submit();

    });

    $("#password").keypress(function (event) {
        if (event.which == 13) {
            $("#login").click();
        }
    });

    function loginCheck(param){
        $.ajax({
            type: "POST",
            url: '/admin/login.do',
            data : JSON.stringify(param),
            contentType : "application/json; charset=UTF-8",
            success: function (data) {
                console.log(data);
                loginResult(data);
            },
            error: function (error) {
                alert("로그인 에러");
            }
        })
    }

    function loginResult(data){
        if (data){
            const {msg, code} = data;
            if (code === 'S') {
                location.href = '/admin/index.do';
            }
            else {
                alert(msg);
            }
        }
    }
</script>