<?php
include "classes/html.class.php";
$db = new db();
$html = new html("receive_nuts");

$variety_res = $db->exec_query('variety', ["*"]);

$clients_res = $db->exec_query('clients', ["*"]);

$client_list = '<option></option>';
while ($clients = $clients_res->fetch_assoc()) {
    $client_list = $client_list . "<option value='{$clients['record_id']}'>{$clients['client_name']}</option>";
}

$variety_list = '<option></option>';
while ($varieties = $variety_res->fetch_assoc()) {
    $variety_list = $variety_list . "<option value='{$varieties['record_id']}'>{$varieties['name']}</option>";
}

?>
<div class='form'>
        <br>
        <label style="font-size:1.5vw">PV: </label><br>
        <input type='text' name='pv' id='pv' class='form_input' />
        <br><br>
        <label style="font-size:1.5vw">FFA: </label><br>
        <input type='text' name='ffa' id='ffa' class='form_input' />
        <br><br>
        <label style="font-size:1.5vw">Weight: </label><br>
        <input type='text' name='weight' id='weight' class='form_input' />
        <hr>
        <label style="font-size:1.5vw">Size: </label><br>
        <input type='text' name='size' id='size' class='form_input' />
        <br><br>
        <label style="font-size:1.5vw">Variety: </label><br>
        <select name='variety' id='variety' class='form_input'>
            <?php echo $variety_list; ?>
        </select>
        <br><br>

        <label style="font-size:1.5vw">Shell: </label><br>
        <select name='shell' id='shell' class='form_input'>
            <option value=''></option>
            <option value='yes'>YES</option>
            <option value='no'>NO</option>
        </select>
        <br><br>

        <label style="font-size:1.5vw">Mold: </label><br>
        <select id='mold' name="mold" class='form_input'>
            <option value=''></option>
            <option value='yes'>YES</option>
            <option value='no'>NO</option>
        </select>
        <hr>

        <label style="font-size:1.5vw">Moisture %: </label><br>
        <input type='text' name='moisture' id='moisture' class='form_input' />
        <br><br>

        <label style="font-size:1.5vw">Sound Not Sound: </label><br>
        <input type='text' name='sound_not_sound' id='sound_not_sound' class='form_input' />
        <br><br>

        <label style="font-size:1.5vw">Client: </label><br>
        <select name='client' id='client' class='form_input'>
            <?php echo $client_list; ?>
        </select>
        <br><br>
        <label style="font-size:1.5vw">Additional Info: </label><br>
        <textarea type='text' style="width: 80%;" id='additional_info' class='form_input'
            oninput="this.style.height = ''; this.style.height = this.scrollHeight + 'px'"></textarea>
        <br><br>

        <input type='submit' value='Receive Nuts' class='form_btn' onclick='ajax_func()' />
</div>
<br><br>

<script>

function ajax_func() {
    var pv = document.getElementById('pv').value;
    var ffa = document.getElementById('ffa').value;
    var weight = document.getElementById('weight').value;
    var size = document.getElementById('size').value;
    var variety = document.getElementById('variety').value;
    var shell = document.getElementById('shell').value;
    var mold = document.getElementById('mold').value;
    var moisture = document.getElementById('moisture').value;
    var sound_not_sound = document.getElementById('sound_not_sound').value;
    var client = document.getElementById('client').value;
    var additional_info = document.getElementById('additional_info').value;

        // Create an XMLHttpRequest object
        const xhttp = new XMLHttpRequest();
        // Define a callback function
        xhttp.onload = function () {
            // alert(this.responseText);

            // Here you can use the Data
            if (this.responseText != 1) {

                console.log(this.responseText);

            } else {
                alert('BATCH RECEIVED');
                window.location.href = "lowveld_nuts.php";
            }
        }

        xhttp.open("GET", "ajax/receive_nuts.ajax.php?pv=" + pv + "&ffa=" + ffa + "&weight=" + weight + '&size=' + size + '&variety=' + variety + '&shell=' + shell + '&mold=' + mold + '&moisture=' + moisture + '&sound_not_sound=' + sound_not_sound + '&client=' + client + '&additional_info=' + additional_info);
        xhttp.send();

}

</script>