<?php

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

$html = new html("");

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

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

<div class="column width_80 background_1 border_radius">

    <h1>EDIT CLIENT</h1>
    <div class="row width_80">
        <div class="column width_50">
            <label>NAME</label>
            <input type="text" class="width_80" id='name' name='name' value="<?php echo $client['name']; ?>" />
            <input type="text" hidden class="width_80" id='record_id' name='record_id'
                value="<?php echo $client['record_id']; ?>" />
            <label>CODE (first letter of each word)</label>
            <input type="text" class="width_80" id='code' name='code' value="<?php echo $client['code']; ?>" />
            <p style="color:red; width_50; text-align:center;">CHANGING THE CODE AFTER SUPPLIER ORDERS HAS BEEN CREATED COULD CAUSE PROBLEMS</p>
            <label>ADDRESS</label>
            <input type="text" class="width_80" id='address' name='address' value="<?php echo $client['address']; ?>" />
            <label>EMAIL</label>
            <input type="email" class="width_80" id='email' name='email' value="<?php echo $client['email']; ?>" />
            <label>CONTACT PERSON</label>
            <input type="text" class="width_80" id='contact_person' name='contact_person'
                value="<?php echo $client['contact_person']; ?>" />
            <label>CONTACT NUMBER</label>
            <input type="text" class="width_80" id='contact_number' name='contact_number'
                value="<?php echo $client['contact_number']; ?>" />
        </div>
    </div>
    <br>
    <button class="width_90" onclick="save('update_suppliers.php')">UPDATE</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>