<?php
include "classes/html.class.php";

$db = new db();
$html = new html("CHECK BATCH");

$batch_res = $db->exec_query('batches', ['*'], '', '', '', '', "record_id = '{$_GET['batch_id']}'", '', false);
$batch = $batch_res->fetch_assoc();

$drying_bins_res = $db->exec_query('drying_bins', ['*']);
$drying_bins = $drying_bins_res->fetch_assoc();

if ($drying_bins['variety_id'] = $batch['variety_id']) {
    $bins_res = $db->exec_query('drying_bins', ["*"], '', '', '', '', "variety_id = {$batch['variety_id']} OR variety_id = 0", '', false);
    $bins_list = '<option></option>';

    while ($bins = $bins_res->fetch_assoc()) {
        $bins_list = $bins_list . "<option value='{$bins['record_id']}'>{$bins['name']}</option>";
    }

} else {
    $bins_res = $db->exec_query('drying_bins', ["*"], '', '', '', '', '', '', false);
    $bins_list = '<option></option>';

    while ($bins = $bins_res->fetch_assoc()) {
        $bins_list = $bins_list . "<option value='{$bins['record_id']}'>{$bins['name']}</option>";
    }
}

?>

<div class='form'>
    <br>

    <input type="text" name="variety_id" id="variety_id" value="<?php echo $batch['variety_id']; ?>" hidden>

    <input type="text" name="shell" id="shell" value="<?php echo $batch['shell']; ?>" hidden>

    <label style="font-size:1.5vw">Batch ID: </label><br>
    <input type='text' name='batch' id='batch' value="BA <?php echo $_GET['batch_id']; ?>" class='form_input'
        readonly />
    <hr>

    <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">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>

    <?php
    if ($batch['shell'] == "NO") {
        ?>
        <label style="font-size:1.5vw">Bins: </label><br>
        <select name="bins" id="bins" class="form_input">
            <?php echo $bins_list; ?>
        </select>
        <br><br>
        <?php
    }
    ?>

    <input type='submit' value='SUBMIT' class='form_btn' onclick='ajax_func()' />
    <br><br>

</div>
<br><br>

<script>

    function ajax_func() {
        var batch_id = <?php echo $_GET['batch_id']; ?>;
        var pv = document.getElementById('pv').value;
        var ffa = document.getElementById('ffa').value;
        var weight = document.getElementById('weight').value;
        var moisture = document.getElementById('moisture').value;
        var variety_id = document.getElementById('variety_id').value;
        var sound_not_sound = document.getElementById('sound_not_sound').value;
        var shell = document.getElementById('shell').value;

        if (shell == "NO") {
            var bins = document.getElementById('bins').value;
        }

        if (weight.length < 0) {
            alert("PLEASE ENTER THE WEIGHT");
        } else {
            if (moisture.length < 0) {
                alert("PLEASE ENTER THE MOISTURE");
            } 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);

                    } else {
                        alert('BATCH RECORDED');
                        window.location.href = "batch.php";
                    }
                }

                xhttp.open("GET", "ajax/confirm_batch.ajax.php?batch_id=" + batch_id + '&pv=' + pv + '&ffa=' + ffa + '&weight=' + weight + '&moisture=' + moisture + '&bins=' + bins + '&variety_id=' + variety_id + '&shell=' + shell + '&sound_not_sound=' + sound_not_sound);
                xhttp.send();
            }
        }

    }

</script>