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

//get the current dat and add 2 hours
$date = new DateTime();
$date->modify('+2 hours');
$current_date = $date->format('Y-m-d H:i');

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

$date = new input();
$date->type("hidden");
$date->id("date");
$date->name("date");
$date->value($current_date);


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

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

$weight_label = new label();
$weight_label->for("nuts_weight_label");
$weight_label->value("WEIGHT AMOUNT TO MOVE (Kg)");

$weight = new input();
$weight->type("number");
$weight->class("inputs");
$weight->placeholder("WEIGHT (Kg)");
$weight->id("weight");
$weight->name("weight");
$weight->onchange("weight_amount()");

$current_weight_label = new label();
$current_weight_label->for("current_weight");
$current_weight_label->value("CURRENT WEIGHT AVAILABLE (Kg)");

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

//CHECK IF any amount has been moved to the drying bin

$weight_check_res = $db->query("bins_trans", "SELECT * FROM bins_trans WHERE batch_id = '{$_GET['batch_id']}'");

if ($weight_check_res->num_rows > 0) {
    $weight_check_data = $weight_check_res->fetch_assoc();

    $total_weight_res = $db->query("batches","SELECT * FROM batches WHERE batch_id = {$_GET['batch_id']}");

    $total_weight = $total_weight_res->fetch_assoc();

    

    $current_weight->value($weight_check_data['amount']);
} else {
    // If no previous weight is found, fetch the current batch weight
    $current_weight->value_from_db("batches", "weight", "batch_id = '{$_GET['batch_id']}'");
}



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

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

$data_res = $db->query("batches", "SELECT * FROM batches WHERE batch_id = '{$_GET['batch_id']}'");

// echo "SELECT * FROM batches WHERE batch_id = '{$_GET['batch_id']}'";

$data = $data_res->fetch_assoc();

// echo $data['variety_id'];

//get all the drying bins
$drying_bins_res = $db->query("drying_bins", "SELECT * FROM drying_bins WHERE variety_id = 0 OR variety_id = {$data['variety_id']}");

// echo "SELECT * FROM drying_bins WHERE variety_id = 0 OR variety_id = {$data['variety_id']} ";

$drying_bin_list = $drying_bin_list . "<option></option>";

if ($drying_bins_res->num_rows > 0) {
    while ($drying_bin = $drying_bins_res->fetch_assoc()) {
        $drying_bin_list = $drying_bin_list . "<option value='" . $drying_bin['record_id'] . "'>" . $drying_bin['name'] . "</option>";
    }
}

?>

<div class="form_down">
    <h1>MOVE NUTS TO DRYING BIN

    </h1>
    <?php
    $user_id->add();
    $date->add();
    $batch_id_label->add();
    $batch_id->add();
    $current_weight_label->add();
    $current_weight->add();
    $weight_label->add();
    $weight->add();
    $drying_bin_label->add();
    ?>

    <select class="inputs" id="drying_bin" name="drying_bin">
        <?php echo $drying_bin_list; ?>
    </select>

    <?php
    $submit_btn->add();
    ?>

</div>

<script>
    function weight_amount() {
        var current_weight = parseFloat(document.getElementById("current_weight").value);
        var weight = parseFloat(document.getElementById("weight").value);
        if (weight > current_weight) {
            alert("Weight amount cannot be greater than current weight available.");
            document.getElementById("weight").style.backgroundColor = '#ff0000a6';
            document.getElementById("weight").focus();
        } else {
            document.getElementById("weight").style.backgroundColor = '#ffffff';
        }
    }
</script>

<?php

$drying_bins_ajax = new js_ajax();
$drying_bins_ajax->function_name("drying_bins");
$drying_bins_ajax->submit_btn_id("submit_btn");
$drying_bins_ajax->insert("bins_trans");
$drying_bins_ajax->redirect('../bins/drying_bins.php');
$drying_bins_ajax->on_success("BATCH MOVED TO DRYING BIN");