$(document).ready(function () {
    let $newPassword = $("#new_password");
    let $repeatPassword = $("#repeat_new_password");
    let $letter = $("#letter");
    let $capital = $("#capital");
    let $number = $("#number");
    let $symbol = $("#symbol");
    let $length = $("#length");
    let $feedback = $("#feedback");
    let $retypeMessage = $("#repeat_password_message");

    $newPassword.on("focus", function () {
        $("#new_password_message").show();
    });

    $newPassword.on("blur", function () {
        $("#new_password_message").hide();
    });

    $newPassword.on("keyup", function () {
        let value = $newPassword.val();

        // Validasi huruf kecil
        if (/[a-z]/.test(value)) {
            $letter.removeClass("invalid").addClass("valid");
        } else {
            $letter.removeClass("valid").addClass("invalid");
        }

        // Validasi huruf besar
        if (/[A-Z]/.test(value)) {
            $capital.removeClass("invalid").addClass("valid");
        } else {
            $capital.removeClass("valid").addClass("invalid");
        }

        // Validasi angka
        if (/[0-9]/.test(value)) {
            $number.removeClass("invalid").addClass("valid");
        } else {
            $number.removeClass("valid").addClass("invalid");
        }

        // Validasi panjang minimal
        if (value.length >= 8) {
            $length.removeClass("invalid").addClass("valid");
        } else {
            $length.removeClass("valid").addClass("invalid");
        }

        // Validasi simbol
        if (/[!$#%@]/.test(value)) {
            $symbol.removeClass("invalid").addClass("valid");
        } else {
            $symbol.removeClass("valid").addClass("invalid");
        }
    });

    // Validasi konfirmasi password
    $repeatPassword.on("focus", function () {
        $feedback.show();
    });

    $repeatPassword.on("blur", function () {
        $feedback.hide();
    });

    $repeatPassword.on("keyup", function () {
        let repeatValue = $repeatPassword.val();
        let newValue = $newPassword.val();

        if (repeatValue.length === 0) {
            $repeatPassword.removeClass("is-valid").addClass("is-invalid");
            $feedback.addClass("text-danger");
            $retypeMessage.html("Silakan masukkan ulang <b>Kata sandi baru!</b>");
        } else if (newValue === repeatValue) {
            $repeatPassword.removeClass("is-invalid").addClass("is-valid");
            $feedback.removeClass("text-danger").addClass("text-success");
            $retypeMessage.html("Kata sandi <b>cocok!</b>");
        } else {
            $repeatPassword.removeClass("is-valid").addClass("is-invalid");
            $feedback.removeClass("text-success").addClass("text-danger");
            $retypeMessage.html("Kata sandi <b>tidak cocok!</b>");
        }
    });
});
