(function ($) {
  "use strict";

  $(function () {
    const $form = $("#change_password");
    const $feedback = $("#formFeedback");

    $form.on("submit", function (e) {
      e.preventDefault();

      const newPass = $("#newpassword").val();
      const cnfPass = $("#cnfpassword").val();

      if (!this.checkValidity()) {
        e.stopPropagation();
        $form.addClass("was-validated");

        $feedback
          .removeClass("d-none alert-success")
          .addClass("alert-danger")
          .text("All Fields Are Required.");

        return;
      }

      if (newPass !== cnfPass) {

        $("#cnfpassword").focus().addClass("is-invalid");

        $feedback
          .removeClass("d-none alert-success")
          .addClass("alert-danger")
          .text("New Password and Confirm Password do not match.");

        return;
      }

      $("#cnfpassword").removeClass("is-invalid");

      $feedback
        .removeClass("d-none alert-danger")
        .addClass("alert-success")
        .text("Password changed successfully.");

      this.submit();
    });

    $("#cnfpassword").on("keyup", function () {
      if ($("#newpassword").val() === $("#cnfpassword").val()) {
        $("#cnfpassword").removeClass("is-invalid");
        $("#formFeedback").addClass("d-none");
      }
    });

  });

})(jQuery);