$("#notify-me-button").on("click", async function () {
  const emailAddress = $("#user-email-address-for-notifications").val();

  if (!emailAddress) {
    return Toastify({
      text: "Please enter email address",
      style: {
        borderRadius: "8px",
      },
    }).showToast();
  }

  if (!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(emailAddress)) {
    return Toastify({
      text: "Invalid email address",
      style: {
        borderRadius: "8px",
      },
    }).showToast();
  }

  const baseURL = new URL(location.href).origin;

  $("#notify-me-button").attr("disabled", "true");
  $("#notify-me-button").html("Please wait");

  $.ajax({
    url: `${baseURL}/server/requests.php`,
    // url: `server/requests.php`,
    type: "POST",
    data: { email_notification_store: "yes", emailAddress },
    success: function (response) {
      console.log(response);
      const JSONResponse = JSON.parse(response);


      Toastify({
        text: JSONResponse.message,
        style: {
          borderRadius: "8px",
        },
      }).showToast();
    },
    error: function (error) {
      Toastify({
        text: error.message,
        style: {
          borderRadius: "8px",
        },
      }).showToast();
    },
    complete: function () {
      $("#notify-me-button").attr("disabled", "false");
      $("#notify-me-button").html(
        '<i class="mdi mdi-bell-ring-outline me-2"></i>Notify Me'
      );
    },
  });
});
