<?php

include "classes/html.class.php";

$html = new html("users");

$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']}");
}
// $farm_list = '<option value="0">ALL</option>';
while ($farms = $farms_res->fetch_assoc()) {
    $farm_list = $farm_list . "<option value='{$farms['record_id']}'>{$farms['farm_name']}</option>";
}

?>



<div class='form' style="padding-bottom: 2vw;">
    <h2 style="font-size: 2vw;" ;>ADD USER:</h2>
    <hr><br>
    <div style="font-size: 1.5vw;" class="center_screen">
        <div class="">
            <label>USER NAME:</label><br>
            <input type='text' placeholder="USERNAME" id='username' class='form_input' /><br><br>
        </div>

        <div class="">
            <label>USER TYPE:</label><br>
            <select id='user_type_1' class='form_input'>
                <option></option>
                <?php if ($_SESSION['user_type'] == 'MANAGER') { ?>
                    <option>MECHANIC</option>
                <? } ?>
                <?php if ($_SESSION['admin'] == 'YES') { ?>
                    <option>MANAGER</option>

                <? } ?>
                <option>SECTION MANAGER</option>

            </select><br><br>
        </div>
        <div class="">
            <label>USER PASSWORD:</label><br>
            <input type='text' placeholder="PASSWORD" id='password_1' class='form_input' /><br><br>
        </div>
        <div class="">
            <label>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>
        </div>
        <div class="" style="grid-area:  3/ 1 / 4 / 3;">
            <label>ADMIN RIGHTS:</label><br>
            <select id='admin' class='form_input'>
                <option>NO</option>
                <?php if ($_SESSION['admin'] == 'YES') { ?>
                    <option>YES</option> <? } ?>
            </select><br><br>
        </div>
    </div>
    <input type='submit' value='ADD' class='form_btn' onclick='ajax_func()' />
</div>
<br>
<br>



<script>
    function ajax_func() {
        // alert("Test");
        username = document.getElementById('username').value;
        password = document.getElementById('password_1').value;
        user_type = document.getElementById('user_type_1').value;
        farm_id = document.getElementById('farm_id').value;
        <?php if ($_SESSION['admin'] == 'NO') { ?>
            admin = '';
        <? } else { ?>
            admin = document.getElementById('admin').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 = "users.php";
            }
        }

        xhttp.open("GET", "ajax/add_user.ajax.php?username=" + username + "&password=" + password + "&user_type=" + user_type + "&farm_id=" + farm_id + "&admin=" + admin);
        xhttp.send();

    }
</script>


</body>

</html>