(function ($) {
  "use strict";

  function togglePassword(selector, $btn) {
    const $input = $(selector);
    if (!$input.length) return;

    const isPassword = $input.attr("type") === "password";
    $input.attr("type", isPassword ? "text" : "password");
    $btn.find("i").attr("class", isPassword ? "bi bi-eye-slash" : "bi bi-eye");
  }

  $(function () {

    const $form = $("#AdminLoginForm");
    const $feedback = $("#formFeedback");

    $(".toggle-pass").on("click", function () {
      togglePassword($(this).data("target"), $(this));
    });

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

      const email = $("#email")[0];
      const emailError = $("#emailError");

      if (email.validity.valueMissing) {
        emailError.text("Enter Email Id.");
      } 
      else if (email.validity.typeMismatch) {
        emailError.text("Enter Valid Email Id.");
      }

      if (!this.checkValidity()) {

        e.preventDefault();
        e.stopPropagation();

        $form.addClass("was-validated");

        $feedback
          .removeClass("d-none alert-success")
          .addClass("alert-danger")
          .text("Enter Your Email Id and Password.");

        return;
      }

      $form.addClass("was-validated");

    });

  });

})(jQuery);