<?php

include "classes/html.class.php";

$html = new html("EVENTS");


$db = new db();

if ($_SESSION['farm_id'] == "0") {
    $farms_res = $db->exec_query('farms', ["*"]);
} else {
    $farms_res = $db->exec_query('farms', ["*"], '', '', '', '', "record_id = {$_SESSION['farm_id']}");
}
while ($farms = $farms_res->fetch_assoc()) {
    $farm_list = $farm_list . "<option value='{$farms['record_id']}'>{$farms['farm_name']}</option>";
}


?>



<div class='form'>
    <h2 style="font-size:2vw">ADD EMAIL:</h2>
    <hr><br>
    <label style="font-size:1.5vw">EVENT TYPE:</label><br>
    <select id='event_type' class='form_input'>
        <option></option>
        <option>REPORTS</option>
        <option>WARNINGS</option>
        <option>ALL</option>
    </select><br><br>
    <label style="font-size:1.5vw">FARM:</label><br>
    <select id='farm_id' class='form_input'>
        <?php if ($_SESSION['admin'] == 'YES') {
            echo '<option value="0">ALL</option>';
        } ?>
        <? echo $farm_list; ?>
    </select><br><br>
    <input type='text' placeholder="NAME" id='name' class='form_input' /><br><br>
    <input type='text' placeholder="EMAIL" id='email' class='form_input' /><br><br>

    <input type='submit' value='ADD' class='form_btn' onclick='ajax_func()' />

</div>

<script>
    function ajax_func() {
        // alert("Test");
        event_type = document.getElementById('event_type').value;
        farm_id = document.getElementById('farm_id').value;
        name = document.getElementById('name').value;
        email = document.getElementById('email').value;

        // Create an XMLHttpRequest object
        const xhttp = new XMLHttpRequest();
        // Define a callback function
        xhttp.onload = function() {
            // Here you can use the Data
            if (this.responseText != 1) {

                alert(this.responseText);

            } else {
                window.location.href = "emails.php";
            }
        }

        xhttp.open("GET", "ajax/add_email.ajax.php?event_type=" + event_type + "&farm_id=" + farm_id + "&name=" + name + "&email=" + email);
        xhttp.send();

    }
</script>


</body>

</html>