<?php

include "../../classes/autoload.php";

$html = new html("JOB CARDS");

$app = new inner_app();
$app->quick_bar();
$app->app_start();

$user_res = $db->query("users", "SELECT * FROM users WHERE record_id = '{$_GET['record_id']}'");
$user = $user_res->fetch_assoc();
?>

<div class="column width_80 background_1 border_radius">

    <h1>EDIT USER</h1>
    <div class="row width_80">
        <div class="column width_50">
            <label>Username</label>
            <input type="text" class="width_80" id='username' name='username'
                value="<?php echo $user['username']; ?>" />
            <input type="text" class="width_80" hidden id='record_id' name='record_id'
                value="<?php echo $user['record_id']; ?>" />
            <label>Password </label>
            <span>(Leave Blank If Not Changing)</span>
            <input type="text" class="width_80" id='password' name='password' />
            <label>Initials</label>
            <input type="text" class="width_80" id='initials' name='initials'
                value="<?php echo $user['initials']; ?>" />
            <label>Type</label>

            <select class="width_80" id='user_type' name='user_type'>
                <?php $function->get_user_types(); ?>
            </select>
            <script>
                document.getElementById('user_type').value = <?php echo $user['type']; ?>;
            </script>
            <label>STUATS</label>
            <select class="width_80" id='status' name='status'>
                <?php $function->get_status(); ?>
            </select>
            <script>
                document.getElementById('status').value = <?php echo $user['status']; ?>;
            </script>
            <label>Email</label>
            <input type="email" class="width_80" id='email' name='email' value="<?php echo $user['email']; ?>" />
        </div>
    </div>
    <br>
    <button class="width_90" onclick="save('update_users.php')">SAVE</button>
    <br>

</div>


<script>
    function save(url) {
        var elements = document.getElementsByTagName('input');
        var payload = {};
        for (var i = 0; i < elements.length; i++) {
            var element = elements[i];
            if (element.type === 'text' || element.type === 'date' || element.type === 'email') {
                payload[element.name] = element.value;
            }
        }

        var elements = document.getElementsByTagName('select');
        for (var i = 0; i < elements.length; i++) {
            var element = elements[i];

            payload[element.name] = element.value;

        }

        var form = document.createElement('form');
        form.method = 'POST';
        form.action = url;

        for (var key in payload) {
            if (payload.hasOwnProperty(key)) {
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = key;
                input.value = payload[key];

                form.appendChild(input);
            }
        }

        document.body.appendChild(form);
        form.submit();
    }
</script>