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

//get the current date and time
$current_date_time = date("Y-m-d H:i", strtotime("+2 hours"));

//get the last batch number from batch and add 1

$day = date("d");
$month = date("m");
$year = date("Y");
$current_year = substr($year, -2);

//get all batches
$batch_tot_res = $db->query("batches", "SELECT batch_id FROM batches");
$batch_tot = $batch_tot_res->num_rows;

$batch_no_res = $db->query("batches", "SELECT batch_id FROM batches ORDER BY batch_id DESC LIMIT 1");
$no_batch = $batch_no_res->num_rows;

if ($no_batch > 0) {
    if ($batch_no_data = $batch_no_res->fetch_assoc()) {
        $batch_no_count = substr($batch_no_data['batch_id'], strpos($batch_no_data['batch_id'], "-") + 1);

        // echo $batch_no_count;
        $batch_no_count = $batch_no_count + 1;

        // echo $batch_no_count;

        $batch_no = $current_year . "-" . $batch_no_count . "-" . $month . $day . "-" . $batch_tot;
    }
} else {
    $batch_no_count = 01;

    $batch_no = $current_year . "-" . $batch_no_count . "-" . $month . $day . "-" . $batch_tot;
    // echo $batch_no;
}

// HTML FORM ELEMENTS

$batch_no_label = new label();
$batch_no_label->for("batch_no");
$batch_no_label->value("Batch No");

$batch_no_value = new input();
$batch_no_value->type("text");
$batch_no_value->name("batch_id");
$batch_no_value->id("batch_id");
$batch_no_value->value($batch_no);
$batch_no_value->readonly();

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

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

$pv_label = new label();
$pv_label->for("pv");
$pv_label->value("PV (Mq)");

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

$ffa_label = new label();
$ffa_label->for("ffa");
$ffa_label->value("FFA (%)");

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

$weight_label = new label();
$weight_label->for("weight");
$weight_label->value("Weight (Kg)");

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

$size_label = new label();
$size_label->for("size");
$size_label->value("Size (mm)");

$size = new input();
$size->type("number");
$size->name("size");
$size->id("size");
$size->placeholder("Size (mm)");

$variety_label = new label();
$variety_label->for("variety");
$variety_label->value("WHAT IS THE NUTS VARIETY");

$variety_data = new datalist();
$variety_data->name("variety_id");
$variety_data->id("variety_id");
$variety_data->class("inputs");
$variety_data->new_datalist();
$variety_data->setDatalistFromDB("variety", "record_id", "name");

$shell_label = new label();
$shell_label->for("shell");
$shell_label->value("DO THE NUTS HAVE SHELLS");

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

$mold_label = new label();
$mold_label->for("mold");
$mold_label->value("DO THE NUTS HAVE MOLD");

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

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

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

$sound_not_sound_label = new label();
$sound_not_sound_label->for("sound_not_sound");
$sound_not_sound_label->value("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("", "");
$sound_not_sound->add_option("YES", "YES");
$sound_not_sound->add_option("NO", "NO");

$client_label = new label();
$client_label->for("client");
$client_label->value("Client");

$client = new select();
$client->name("client_id");
$client->id("client_id");
$client->add_option("", "");
$client->fill_from_db("clients", "record_id", "client_name");

$notes_label = new label();
$notes_label->for("notes");
$notes_label->value("ADDITIONAL INFORMATION/ COMMENTS");

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

$status = new input();
$status->type("hidden");
$status->name("status");
$status->id("status");
$status->value("BATCH RECEIVED");

$submit_btn = new button();
$submit_btn->onclick("receive_nuts(), insert_batch()");
$submit_btn->value("SUBMIT");
?>

<div class="form_down">
    <h1>RECEIVE NUTS</h1>
    <?php
    $batch_no_label->add();
    $batch_no_value->add();
    ?>

    <!-- <div style="width: 50%; display: flex;justify-content: center;">
        <div style="display: flex; flex-direction: column;">
            <?php
            $pv_label->add();
            $pv->add();
            ?>
        </div>
        <div style="display: flex; flex-direction: column;">
            <?php
            $ffa_label->add();
            $ffa->add();
            ?>
        </div>
    </div> -->
    <!-- <div style="width: 50%; display: flex;justify-content: center;">
        <div style="display: flex; flex-direction: column;">
            <?php
            $weight_label->add();
            $weight->add();
            ?>
        </div>
        <div style="display: flex; flex-direction: column;">
            <?php
            $size_label->add();
            $size->add();
            ?>
        </div>
    </div> -->

    <?php
    $weight_label->add();
    $weight->add();
    $date->add();
    $user_id->add();
    $variety_label->add();
    $variety_data->add();
    $shell_label->add();
    $shell->add();
    $mold_label->add();
    $mold->add();
    $moisture_label->add();
    $moisture->add();
    $sound_not_sound_label->add();
    $sound_not_sound->add();
    $client_label->add();
    $client->add();
    $notes_label->add();
    $notes->add();
    $status->add();

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

<script>
    function checkffa() {
        var ffa = document.getElementById("ffa").value;
        if (ffa > 3) {
            alert("FFA PERCENTAGE CANNOT BE GREATER THAN 2");
            document.getElementById("ffa").style.backgroundColor = '#ff0000a6';
            document.getElementById("ffa").focus();
        } else {
            document.getElementById("ffa").style.backgroundColor = '#ffffff';
        }
    }

    function checkmoisture() {
        var moisture = document.getElementById("moisture").value;
        if (moisture > 16) {
            alert("MOISTURE PERCENTAGE CANNOT BE GREATER THAN 15");
            document.getElementById("moisture").style.backgroundColor = '#ff0000a6';
            document.getElementById("moisture").focus();

        } else {
            document.getElementById("moisture").style.backgroundColor = '#ffffff';
        }
    }

</script>

<?php

$receive_ajax = new js_ajax();
$receive_ajax->function_name("receive_nuts");
$receive_ajax->submit_btn_id("submit");
$receive_ajax->insert("batches");
$receive_ajax->on_success("NUTS RECEIVED");
$receive_ajax->redirect("../receive/home.php");

// $batch_ajax = new js_ajax();
// $batch_ajax->function_name("insert_batch");
// $batch_ajax->submit_btn_id("submit");
// $batch_ajax->selected_div(".data");
// $batch_ajax->insert("batch");
// $batch_ajax->redirect("../receive/check.php");