<?php include "../../root.class.php";
$html = new html();
$db = new db_safeguard();
$html->add_styles_page();
// $html->check_user_type("ADMIN");

$batch_res = $db->query("batches", "SELECT * FROM batches");
// $batch_res = $db->query("batches", "SELECT * FROM batches WHERE record_id = {$_GET['batch_id']}");
$batch = $batch_res->fetch_assoc();

$variety_res = $db->query('variety', "SELECT * FROM variety WHERE record_id = {$batch['variety_id']}");
$variety = $variety_res->fetch_assoc();

$bins_trans_res = $db->query('bins_trans', "SELECT * FROM bins_trans");
$bins_trans = $bins_trans_res->fetch_assoc();

$drying_bins_res = $db->query('drying_bins', "SELECT * FROM drying_bins");
$drying_bins = $drying_bins_res->fetch_assoc();

if ($drying_bins['variety_id'] = $batch['variety_id']) {
    $bins_res = $db->query('drying_bins', "SELECT * FROM drying_bins WHERE variety_id = {$batch['variety_id']} OR variety_id = 0");
    $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->query('drying_bins', "SELECT * FROM drying_bins");
    $bins_list = '<option></option>';

    while ($bins = $bins_res->fetch_assoc()) {
        $bins_list = $bins_list . "<option value='{$bins['record_id']}'>{$bins['name']}</option>";
    }
}

$user_id = new input();
$user_id->type("hidden");
$user_id->name("user_id");
$user_id->id("user_id");
$user_id->value($_SESSION['user_id']);

$batch_id = new input();
$batch_id->type("hidden");
$batch_id->name("batch_id");
$batch_id->id("batch_id");
// $batch_id->value($_GET['batch_id']);

$date_received_label = new label();
$date_received_label->for("date_received");
$date_received_label->value("DATE RECEIVED");

$date_received = new input();
$date_received->type("datetime-local");
$date_received->name("date_received");
$date_received->id("date_received");
$date_received->value($batch['date']);

$variety_label = new label();
$variety_label->for("variety_id");
$variety_label->value("VARIETY");

$variety_id = new input();
$variety_id->type("hidden");
$variety_id->name("variety_id");
$variety_id->id("variety_id");
$variety_id->value($batch['variety_id']);

$weight = new input();
$weight->type("number");
$weight->name("weight");
$weight->id("weight");

$notes_label = new label();
$notes_label->for("notes");
$notes_label->value("ADDITIONAL NOTES");

$notes = new input();
$notes->type("text");
$notes->name("notes");
$notes->id("notes");
$notes->style("width: 50%;");

$drying_bin_label = new label();
$drying_bin_label->for("bins");
$drying_bin_label->value("MOVE TO DRYING BIN");

$submit_btn = new button();
$submit_btn->value("SUBMIT");
$submit_btn->onclick("ajax_func();");

?>

<div class="form_down">
    <h1>DEHUSKING</h1>
    <?php
    $user_id->add();
    // $batch_id->add();
    $date_received_label->add();
    $date_received->add();
    $variety_label->add();
    $variety_id->add();
    $weight->add();
    $drying_bin_label->add();
    ?>

    <select name="bins" id="bins" class="inputs">
        <?php echo $bins_list; ?>
    </select>

    <?php
    $notes_label->add();
    $notes->add();
    $submit_btn->add();
    ?>
</div>

<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>