<?php
include_once ("../config.php");

if (isset($_POST["email_notification_store"])) {
    $email = mysqli_real_escape_string($db, $_POST["emailAddress"]);

    $duplicateQuery = "SELECT * FROM leads WHERE email = '$email'";

    $checkResult = mysqli_num_rows(mysqli_query($db, $duplicateQuery));

    if ($checkResult) {
        echo json_encode(array("success" => true, "message" => "Notification is already set"));
        exit();
    }

    $query = "INSERT INTO leads (`email`) VALUES ('$email')";

    $result = mysqli_query($db, $query);

    if ($result) {
        echo json_encode(array("success" => true, "message" => "Notification set successfully"));
    } else {
        echo json_encode(array("success" => false, "message" => "Notification set failed"));
    }
}

?>
