<?php
include "classes/html.class.php";
$html = new html("USERS");
$db = new db();
if ($_SESSION['admin'] == 'YES') {
    $user_res = $db->exec_query('users', ["*"]);
} elseif ($_SESSION['admin'] == 'NO' && $_SESSION['farm_id'] == 0) {
    $user_res = $db->exec_query('users', ["*"], '', '', '', '', "farm_id = 0 AND admin != 'YES'");
} else {
    $user_res = $db->exec_query('users', ["*"], '', '', '', '', "farm_id = {$_SESSION['farm_id']} AND admin != 'YES'", "ORDER BY username ASC");
}
$users_list = '<option></option>';
while ($users = $user_res->fetch_assoc()) {
    $users_list = $users_list . "<option value='{$users['record_id']}'>{$users['username']}</option>";
}
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;" ;>SELECT USER:</h2><br>
    <select onchange="call_ajax_func()" id='users_list' class='form_input' style="font-size: 1.5vw;">
        <?php echo $users_list; ?>
    </select>
    <hr>
    <h2 style="font-size: 1.75vw;">EDIT USER:</h2>
    <hr><br>
    <div style="font-size: 1.5vw;" class="center_screen">
        <div>
            <label>USER NAME:</label><br>
            <input type='text' placeholder="USERNAME" id='username' class='form_input' /><br><br>
        </div>
        <div>
            <label>USER PASSWORD:</label><br>
            <input type='text' placeholder="PASSWORD" id='password_1' class='form_input' /><br><br>
        </div>
        <div>
            <label class='form_label'>STATUS</label><br>
            <select id='status' class='form_input'>
                <option></option>
                <option>ACTIVE</option>
                <option>SUSPENDED</option>
            </select><br><br>
        </div>
        <div>
            <label class='form_label'>USER TYPE</label><br>
            <select id='user_type_1' class='form_input'>
                <option></option>
                <option>MECHANIC</option>
                <option>MANAGER</option>
                <option>FUEL OPERATOR</option>
                <option>SECTION MANAGER</option>
                <option>SECTION MANAGER WITH FUEL</option>
            </select><br><br>
        </div>
        <div>
            <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>
            <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='SAVE' class='form_btn' onclick='ajax_func()' />

    

</div>
<div style="padding-bottom:2vw;"></div>

<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;
        status = document.getElementById('status').value;
        users_id = document.getElementById('users_list').value;
        farm_id = document.getElementById('farm_id').value;
        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 {
                alert("SAVED");

            }
        }

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

    }

    function call_ajax_func() {
        // alert("Test");
        users_list = document.getElementById('users_list').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) {
                values = this.responseText.split(',');
                document.getElementById('username').value = values[0];
                document.getElementById('password_1').value = values[1];
                document.getElementById('status').value = values[2];
                document.getElementById('user_type_1').value = values[3];
                document.getElementById('admin').value = values[5];
                document.getElementById('farm_id').value = values[4];



            } else {
                // window.location.href = "users.php";
                alert("Something Went Wrong");

            }
        }

        xhttp.open("GET", "ajax/get_user.ajax.php?users_list=" + users_list);
        xhttp.send();

    }
</script>


</body>

</html>