<?php
include "classes/html.class.php";
$db = new db();


$html = new html("farms");
if ($_SESSION['farm_id'] == 0) {
    $farms_section_res = $db->exec_query('farm_sections', ["*"]);
} else {
    $farms_section_res = $db->exec_query('farm_sections', ["*"], '', '', '', '', "farm_id = {$_SESSION['farm_id']}");
}
$section_list = '<option></option>';
while ($farm_sections = $farms_section_res->fetch_assoc()) {
    $farm_res = $db->exec_query('farms', ["*"], '', '', '', '', "record_id = {$farm_sections['farm_id']}");
    $farm = $farm_res->fetch_assoc();
    $section_list = $section_list . "<option value='{$farm_sections['record_id']}'>{$farm['farm_name']} -> {$farm_sections['name']}</option>";
}

?>



<div class='form'>
    <h2 style="font-size:2vw">ADD FARM BLOCK:</h2>
    <hr><br>
    <label>FARM: </label><br>
    <select id='section_id' class='form_input'>
        <?php echo $section_list; ?>
    </select><br><br>
    <input type='text' placeholder="FARM BLOCK NAME" id='farm_block_name' class='form_input' /><br><br>

    <input type='submit' value='ADD' class='form_btn' onclick='ajax_func()' />

</div>

<script>
    function ajax_func() {
        // alert("Test");
        section_id = document.getElementById('section_id').value;
        farm_block_name = document.getElementById('farm_block_name').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 = "farm_blocks.php";
            }
        }

        xhttp.open("GET", "ajax/add_farm_block.ajax.php?section_id=" + section_id + "&farm_block_name=" + farm_block_name, true);
        xhttp.send();

    }
</script>


</body>

</html>