<?php
include "classes/html.class.php";
session_start();
$db = new db();
$html = new html("receive nuts");

$variety_res = $db->exec_query('variety', ["*"]);

$clients_res = $db->exec_query('clients', ["*"]);

$bins_res = $db->exec_query('drying_bins', ["*"]);

$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>";
}

$bin_list = '<option></option>';
while ($bins = $bins_res->fetch_assoc()) {
    $bin_list = $bin_list . "<option value='{$bins['record_id']}'>{$bins['name']}</option>";
}

?>
<div class='form'>
    <br>
    <input type="text" name="user_id" id="user_id" value="<?php echo $_SESSION['user_id']; ?>" class="form_input"
        hidden>
    <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>

    <div>
        <input type="text" name="batch_v" id="batch_v" value="" class="form_input" hidden>
        <script>
            document.getElementById('variety').value = document.getElementById('batch_v').value
        </script>

    </div>

    <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>
    <select name='sound_not_sound' id='sound_not_sound' 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">Client: </label><br>
    <select name='client' id='client' class='form_input'>
        <?php echo $client_list; ?>
    </select>
    <br><br>
    <hr>
    <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()' />
    <br><br>
</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 notes = document.getElementById('additional_info').value;
        var user_id = document.getElementById('user_id').value;

        if (pv.length < 0) {
            alert("PLEASE ENTER PV VALUE")
        } else {
            if (weight.length < 0) {
                alert("PLEASE ENTER WEIGHT")
            } else {
                if (ffa.length < 0) {
                    alert("PLEASE ENTER FFA VALUE")
                } else {
                    if (variety.length < 0) {
                        alert("PLEASE ENTER VARIETY")
                    } else {

                        // 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);
                                alert('ERROR, BATCH NOT RECEIVED');


                            } else {
                                alert('BATCH RECEIVED');
                                window.location.href = "receive_home.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=' + notes + '&user_id=' + user_id);
                        xhttp.send();
                    }
                }
            }
        }
    }

</script>