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

<section class="sub-container">
    <article class="menu-history">
        <ul>
            <li><a href="/main/main.do" class="home">홈</a></li>
            <li><a href="/admin/index.do">관리자 로그인</a></li>
        </ul>
    </article>
    <article class="contents-wrap">
        <div class="title-box">
            <h2 class="h2-title">로그인</h2>
            <p class="title-info">평택시 교통정보센터 <span>관리자</span></p>
        </div>
        <div class='login-form'>
            <h2>홈페이지 관리를 위해서는 관리자 로그인이 필요합니다.</h2>
            <ul>
                <li style="width: 70%;">
                    <p><input
                            type="text"
                            class="form inoff"
                            title="아이디"
                            name="email"
                            id="email"
                            placeholder="아이디를 입력하세요"/></p>
                    <p><input
                            type="password"
                            class="form inoff mt-01"
                            title="비밀번호"
                            name="password"
                            id="password"
                            placeholder="비밀번호를 입력하세요"/></p>

                </li >
                <li style="width: 30%;">
                    <input type="button" id="login" value="로그인" class='btn-login fadio300'/>
                </li>
                <c:if test="${message != null}">
                    <li style="padding:10px;color:red"><c:out value="${message }"/></li>
                </c:if>
            </ul>

            <%--                <ul>--%>
            <%--                    <li style="visibility: hidden" class='id-save' ><label for="idSave"><input type="checkbox" name="" id="idSave" value=""  />아이디 저장</label></li>--%>
            <%--                    <li style="width: 70%;">--%>
            <%--                        <p><input type="text" name="" placeholder="아이디를 입력하세요" value="" class='inoff' title="아이디" /></p>--%>
            <%--                        <p><input type="password" name="" placeholder="비밀번호를 입력하세요" value="" class='inoff mt-01'  title="비밀번호" /></p>--%>
            <%--                    </li>--%>
            <%--                    <li style="width: 30%;"><input type="button" name="" value="로그인" class='btn-login fadio300' onclick="location.href='../admin/admin_notice.html'" /></li>--%>
            <%--                </ul>--%>
        </div>
    </article>
</section>

<!--//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 : $("#email").val(),
            password : $("#password").val()
        };
        console.log(param);
        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) {
                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>