<?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 WHERE record_id = '{$_GET['batch_id']}'");
$batch = $batch_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>";
    }
}

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

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

$batch_id_label = new label();
$batch_id_label->value("Batch ID");
$batch_id_label->for("batch_id");

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

$pv = new input();
$pv->type("text");
$pv->name("pv");
$pv->id("pv");
$pv->placeholder("PV (Mq)");

$ffa = new input();
$ffa->type("text");
$ffa->name("ffa");
$ffa->id("ffa");
$ffa->placeholder("FFA (%)");

$weight = new input();
$weight->type("text");
$weight->name("weight");
$weight->id("weight");
$weight->placeholder("Weight");

$moisture_label = new label();
$moisture_label->value("Moisture %");
$moisture_label->for("moisture");

$moisture = new input();
$moisture->type("text");
$moisture->name("moisture");
$moisture->id("moisture");
$moisture->placeholder("Moisture (%)");

$sound_not_sound_label = new label();
$sound_not_sound_label->value("Sound Not Sound");
$sound_not_sound_label->for("sound_not_sound");

$sound_not_sound = new select();
$sound_not_sound->name("sound_not_sound");
$sound_not_sound->id("sound_not_sound");
$sound_not_sound->add_option("YES", "YES");
$sound_not_sound->add_option("NO", "NO");

$bins_label = new label();
$bins_label->value("Bins");
$bins_label->for("bins");

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

?>

<div class="form_down">
    <h1>CHECK BATCH</h1>
    <?php
    $variety_id->add();
    $shell->add();
    $batch_id_label->add();
    $batch_id->add();
    ?>

    <div style="width: 40%; display: flex;">
        <?php
        $pv->add();
        $ffa->add();
        ?>
    </div>

    <?php
    $weight->add();
    $moisture_label->add();
    $moisture->add();
    $sound_not_sound_label->add();
    $sound_not_sound->add();

    if ($batch['shell'] == "NO") {
        $bins_label->add();
        ?>
        <select name="bins" id="bins" class="form_input">
            <?php echo $bins_list; ?>
        </select>
        <br><br>
        <?php
    }
    $submit_btn->add();
    ?>
</div>

<?php

function Sound($shell)
{
    
    if($shell === "NO"){
        $batch_ajax = new js_ajax();
        $batch_ajax->function_name("receive_batch");
        $batch_ajax->insert("batch");
        $batch_ajax->selected_div("store");
        $batch_ajax->add_column_and_value("drying_bin_id=0, silo_id=0");
        $batch_ajax->on_success("BATCH RECEIVED");
        $batch_ajax->redirect("../receive/check.php");
    } else{
        $batch_ajax = new js_ajax();
        $batch_ajax->function_name("receive_batch");
        $batch_ajax->insert("batch");
        $batch_ajax->update("drying_bins");
        $batch_ajax->selected_div("store");
        $batch_ajax->add_column_and_value("drying_bin_id=0, silo_id=0");
        $batch_ajax->on_success("BATCH RECEIVED");
        $batch_ajax->redirect("../receive/check.php");

        $bin_ajax = new js_ajax();
        $bin_ajax->function_name("drying_bin");
        $bin_ajax->insert("bins");
        $bin_ajax->update("batches");
        $bin_ajax->selected_div("bins");
        $bin_ajax->add_column_and_value("status=DRYING BINS");
        $bin_ajax->on_success("BATCH RECEIVED");
        $bin_ajax->redirect("../receive/check.php");
    }
}

function Not_sound()
{
    $receive_ajax = new js_ajax();
    $receive_ajax->function_name("receive_nuts");
    $receive_ajax->submit_btn_id("submit");
    $receive_ajax->update("batches");
    $receive_ajax->selected_div("test");
    $receive_ajax->add_column_and_value("status=NOT SOUND, sound_not_sound=NO");
    $receive_ajax->on_success("BATCH RECEIVED");
    $receive_ajax->redirect("../receive/check.php");

}

?>

<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;
    //     }

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