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

<div class="container">
    <div class="subHeader">
        <h1 class="logo">안동시 교통정보센터</h1>
        <div class="pageLocation">
            <%@ include file="/WEB-INF/jsp/egovframework/include/adminSubMenu.jsp" %>
        </div>
    </div>
    <div class="subContent">
        <%@ include file="/WEB-INF/jsp/egovframework/include/adminMenu.jsp" %>
        <div class="pageTitle">
            <h2>관리자 정보변경</h2>
            <p>역사와 미래가 공존하는 <span>안동</span></p>
        </div>
        <div class="adminWrap">
            <h2 class="admin-tit">
                <strong>안동시 첨단교통센터</strong> 관리자 정보입니다.<br>
                원하시는 비밀번호로 변경하세요.<br>
                관리자 비밀번호는 정기적으로 변경해주세요.
            </h2>
            <ul class="login-form">
                <li>
                    <input id="prePw" type="password" name="prePw" value="" placeholder="기존 비밀번호" title="기존 비밀번호">
                </li>
                <li>
                    <input id="afterPw" type="password" name="afterPw" value="" placeholder="신규 비밀번호" title="신규 비밀번호">
                </li>
                <li>
                    <input id="afterPw2" type="password" name="afterPw2" value="" placeholder="비밀번호 확인" title="비밀번호 확인">
                </li>
                <li><button type="button" name="" class="formBtn" onclick="changePwd()">변경하기</button></li>
            </ul>
        </div>
    </div>
</div>

<script type="text/javascript">
    const prePw    = $("#prePw");
    const afterPw  = $("#afterPw");
    const afterPw2 = $("#afterPw2");
    prePw.focus();
    function changePwd() {
        const prePwVal    = prePw.val();
        const afterPwVal  = afterPw.val();
        const afterPw2Val = afterPw2.val();
        // if (!validationCheck(prePw)) return;
        if (prePwVal === "") {
            alert("기존 비밀번호를 입력해주세요.");
            return prePw.focus();
        }
        if (!validationCheck(afterPw)) return;
        if (!validationCheck(afterPw2)) return;

        if (afterPwVal === prePwVal) {
            alert("기존 비밀번호와 신규 비밀번호를 다르게 입력해주세요..");
            return afterPw.focus();
        }

        if (afterPwVal !== afterPw2Val) {
            alert("신규 비밀번호를 확인란과 동일하게 입력해주세요.");
            return afterPw2.focus();
        }

        $.ajax({
            type: "POST",
            url: '/admin/checkPrePw.do',
            contentType: 'application/x-www-form-urlencoded; charset=utf-8',
            data: {
                originPwd : encodeURIComponent(prePwVal),
                newPwd : encodeURIComponent(afterPwVal),
                checkPwd : encodeURIComponent(afterPw2Val)
            },
            success: function (data) {
                if (data) {
                    if (data.code === 'S') {
                        prePw.val("");
                        afterPw.val("");
                        afterPw2.val("");
                    }
                    alert(data.message);
                }
            },
            error: function (request,status,error) {
                alert("code:"+request.status+"\n"+"error:"+error);
            }
        });

    }

    function validationCheck(target) {
        const pwd   = target.val();
        const check = /^(?=.*[a-zA-Z])(?=.*[!@#$%^*+=-])(?=.*[0-9]).{9,12}$/.test(pwd);
        const text  = target.attr('placeholder');

        let end = '를';

        if (isEndWithConsonant(text)) end = '을';

        if (pwd === null || pwd === undefined || pwd.length < 9 || pwd.length > 12) {
            alert(text + end + " 9자리 ~ 12자리 이내로 입력해주세요.");
            target.focus();
            return false;
        }

        if (pwd.search(/₩s/) != -1) {
            alert(text + end + " 공백없이 입력해주세요.");
            target.focus();
            return false;
        }

        if (!check) {
            alert(text + end + " 영문, 숫자, 특수문자 혼합하여 입력해주세요.");
            target.focus();
            return false;
        }

        return true;
    }

    function isEndWithConsonant(korStr) {
        const finalChrCode = korStr.charCodeAt(korStr.length - 1)
        // 0 = 받침 없음, 그 외 = 받침 있음
        const finalConsonantCode = (finalChrCode - 44032) % 28
        return finalConsonantCode !== 0
    };

    prePw.keypress(function (event) {
        if (event.which == 13) {
           afterPw.focus();
        }
    });

    afterPw.keypress(function (event) {
        if (event.which == 13) {
            afterPw2.focus();
        }
    });

    afterPw2.keypress(function (event) {
        if (event.which == 13) {
            changePwd();
        }
    });

</script>