<?php
include "classes/html.class.php";
session_start();
$db = new db();
$html = new html("DEHUSKING");

$wheredata = '1';

$batch_res = $db->exec_query('batches', ['*'], '', '', '', '', "record_id = {$_GET['batch_id']}", '', false);
$batch = $batch_res->fetch_assoc();

$variety_res = $db->exec_query('variety', ["*"], '', '', '', '', "record_id = {$batch['variety_id']}", '', false);
$variety = $variety_res->fetch_assoc();

$bins_trans_res = $db->exec_query('bins_trans', ['*']);
$bins_trans = $bins_trans_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="user_id" id="user_id" value="<?php echo $_SESSION['user_id']; ?>" class="form_input"
        hidden>

    <input type="text" name="batch_id" id="batch_id" value="<?php echo $_GET['batch_id']; ?>" class="form_input" hidden>

    <label style="font-size:1.5vw">DATE RECEIVED: </label><br>
    <input type="DateTime" name="date_received" id="date_received" class="form_input"
        value="<?php echo $batch['date']; ?>" readonly>
    <br><br>

    <label style="font-size:1.5vw">VARIETY: </label><br>
    <input name='variety_id' id='variety_id' class='form_input' value="<?php echo $batch['variety_id']; ?>" hidden />
    <input name='variety' id='variety' class='form_input' value="<?php echo $variety['name']; ?>" readonly />
    <br><br>
    <hr>

    <label style="font-size:1.5vw">WEIGHT: </label><br>
    <input type='text' name='weight' id='weight' class='form_input' />
    <br><br>

    <label style="font-size:1.5vw">Bins: </label><br>
    <select name="bins" id="bins" class="form_input">
        <?php echo $bins_list; ?>
    </select>
    <br><br>

    <label style="font-size:1.5vw">Notes: </label><br>
    <textarea type='text' style="width: 80%;" id='notes' class='form_input'
        oninput="this.style.height = ''; this.style.height = this.scrollHeight + 'px'"></textarea>
    <br><br>

    <input type='submit' value='SUBMIT' class='form_btn' onclick='ajax_func()' />
    <br><br>
</div>
<br><br>

<script>

    function ajax_func() {
        var batch_id = document.getElementById('batch_id').value;
        var date_received = document.getElementById('date_received').value;
        var weight = document.getElementById('weight').value;
        var variety_id = document.getElementById('variety_id').value;
        var notes = document.getElementById('notes').value;
        var user_id = document.getElementById('user_id').value;
        var bins = document.getElementById('bins').value;

        if (weight.length < 0) {
            alert("PLEASE ENTER THE WEIGHT");
        } 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('DEHUSKING RECORDED');
                    window.location.href = "dehusk.php";
                }
            }
            xhttp.open("GET", "ajax/dehusk_nuts.ajax.php?batch_id=" + batch_id + '&weight=' + weight + '&bins=' + bins + '&date_received=' + date_received + '&variety_id=' + variety_id + '&notes=' + notes + '&user_id=' + user_id);
            xhttp.send();
        }
    }

</script>