<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();

// generate a stock id using 3
$stock_res = $db->query("stock_control", "SELECT * FROM stock_control ORDER BY stock_no DESC LIMIT 1");

if (!$stock_res) {
    die("Database query failed: ");
}

if ($stock = $stock_res->fetch_assoc()) {
    //get the last 3 digits in the stock_id
    $last_three_digits = substr($stock["stock_no"], -3);
    $next_three_digits = $last_three_digits + 1;
    $stock_no = "ST" . str_pad($next_three_digits, 3, "0", STR_PAD_LEFT);

} else {
    $stock_no = "ST001";
}

$stock_id_label = new label();
$stock_id_label->for("stock_no_label");
$stock_id_label->value("STOCK NO : ");
$stock_id_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$stock_id = new input();
$stock_id->type("text");
$stock_id->name("stock_no");
$stock_id->id("stock_no");
$stock_id->value($stock_no);
$stock_id->readonly();
$stock_id->style("font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$item_name_label = new label();
$item_name_label->for("item_name_label");
$item_name_label->value("ITEM NAME : ");
$item_name_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$item_name = new input();
$item_name->type("text");
$item_name->name("item_name");
$item_name->id("item_name");
$item_name->style("font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$product_name_label = new label();
$product_name_label->for("product_name_label");
$product_name_label->value("PRODUCT NAME : ");
$product_name_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$product_name = new input();
$product_name->type("text");
$product_name->name("product_name");
$product_name->id("product_name");
$product_name->style("font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$unit_of_measure_label = new label();
$unit_of_measure_label->for("quantity_type_label");
$unit_of_measure_label->value("UNIT TYPE : ");
$unit_of_measure_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$measuring_unit = new select();
$measuring_unit->name("unit_of_measure");
$measuring_unit->id("unit_of_measure");
$measuring_unit->add_option("liters", "liters");
$measuring_unit->add_option("ml", "ml");
$measuring_unit->add_option("kg", "kg");
$measuring_unit->add_option("g", "g");
$measuring_unit->add_option("inches", "inches");
$measuring_unit->add_option("meters", "meters");
$measuring_unit->add_option("cm", "cm");
$measuring_unit->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$quantity_label = new label();
$quantity_label->for("quantity_label");
$quantity_label->value("STOCK QUANTITY : ");
$quantity_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$quantity = new input();
$quantity->type("number");
$quantity->name("quantity");
$quantity->id("quantity");
$quantity->style("font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$weight_label = new label();
$weight_label->for("weight_label");
$weight_label->value("AMOUNT : ");
$weight_label->addAttribute("style", "font-size: 1.5em; width: 20%;");

$weight = new input();
$weight->type("number");
$weight->name("weight");
$weight->id("weight");
$weight->style("font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$item_price_label = new label();
$item_price_label->for("item_price_label");
$item_price_label->value("COST PER UNIT : ");
$item_price_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$status = new input();
$status->type("hidden");
$status->name("status");
$status->id("status");
$status->value(0);

$type_label = new label();
$type_label->for("type_label");
$type_label->value("STOCK TYPE : ");
$type_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$type = new select();
$type->name("stock_type");
$type->id("stock_type");
$type->style("width: 20vw;");
$type->fill_from_db("stock_type", "record_id", "name");
$type->onchange("change_stock_type()");
$type->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$vehicle_type_label = new label();
$vehicle_type_label->for("vehicle_type_label");
$vehicle_type_label->value("VEHICLE TYPE : ");
$vehicle_type_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$vehicle_type = new select();
$vehicle_type->class("inputs");
$vehicle_type->name("vehicle_type");
$vehicle_type->id("vehicle_type");
$vehicle_type->add_option("ALL", "ALL");
$vehicle_type->add_option("T", "TRUCK");
$vehicle_type->add_option("D", "DONKIE");
$vehicle_type->add_option("C", "COMP");
$vehicle_type->add_option("CT", "COMP TRUCK");
$vehicle_type->add_option("OTHER", "OTHER");
$vehicle_type->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$supplier_label = new label();
$supplier_label->for("supplier_label");
$supplier_label->value("SUPPLIER : ");
$supplier_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$supplier = new select();
$supplier->class("inputs");
$supplier->name("supplier_id");
$supplier->id("supplier_id");
$supplier->fill_from_db("suppliers", "record_id", "name");
$supplier->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$serial_number_label = new label();
$serial_number_label->for("serial_number_label");
$serial_number_label->value("SERIAL NUMBER : ");
$serial_number_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$serial_number = new input();
$serial_number->type("text");
$serial_number->name("serial_number");
$serial_number->id("serial_number");
$serial_number->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$submit_btn = new button();
$submit_btn->value("SUBMIT");
?>

<form action="add_stock.ajax.php" method="POST">
    <div class="form_down">
        <h1 style="font-size: 3em; margin-bottom: 0.5vw; margin-top: 1.5vw;">ADD STOCK</h1>

        <div style="width: 95%; background-color: white; border-radius: 2vw; padding: 1vw;">
            <div>
                <?php
                $status->add();
                ?>
                <div style="display: flex; flex-direction: row; width: 100%;">
                    <?php
                    $stock_id_label->add();
                    $stock_id->add();
                    ?>
                </div>

                <div style="display: flex; flex-direction: row; width: 100%;">
                    <?php
                    $type_label->add();
                    $type->add();
                    ?>
                </div>

                <div style="display: flex; flex-direction: row; width: 100%;">
                    <?php
                    $item_name_label->add();
                    $item_name->add();
                    ?>
                </div>
            </div>

            <div id="default_stock" style="display: flex; flex-direction: column; width: 100%;">
                <div>
                    <div id="hammer_stock" style="display: none; flex-direction: row; width: 100%;">
                        <?php
                        $serial_number_label->add();
                        $serial_number->add();
                        ?>
                    </div>

                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <?php
                        $supplier_label->add();
                        $supplier->add();
                        ?>
                    </div>
                    
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <?php
                        $quantity_label->add();
                        $quantity->add();
                        ?>
                    </div>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <?php
                        $unit_of_measure_label->add();
                        $measuring_unit->add();
                        ?>
                    </div>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <?php
                        $item_price_label->add();
                        ?>
                        <input type="number" name="item_price" id="item_price" class="inputs" step="0.01"
                            style="width: 80%; font-size: 1.5em; margin: 0.5vw auto;">
                    </div>


                    <div id="oil_stock" style="display: none; flex-direction: row; width: 100%;">
                        <?php
                        $vehicle_type_label->add();
                        $vehicle_type->add();
                        ?>
                    </div>
                </div>
            </div>

            <div id="pump_stock" style="display: none; flex-direction: column; ">

            </div>
        </div>

        <?php
        $submit_btn->add();
        ?>

    </div>

    <script>
        function change_stock_type() {
            var stock_type = document.getElementById("stock_type").value;

            if (stock_type == "4") {
                document.getElementById("pump_stock").style.display = "none";
                document.getElementById("hammer_stock").style.display = "flex";
                document.getElementById("oil_stock").style.display = "none";
                document.getElementById("default_stock").style.display = "flex";

            } else if (stock_type == "3") {
                document.getElementById("pump_stock").style.display = "flex";
                document.getElementById("hammer_stock").style.display = "none";
                document.getElementById("oil_stock").style.display = "none";
                document.getElementById("default_stock").style.display = "none";

            } else if (stock_type == "1") {
                document.getElementById("pump_stock").style.display = "none";
                document.getElementById("hammer_stock").style.display = "none";
                document.getElementById("oil_stock").style.display = "flex";
                document.getElementById("default_stock").style.display = "flex";

            } else {
                document.getElementById("pump_stock").style.display = "none";
                document.getElementById("hammer_stock").style.display = "none";
                document.getElementById("oil_stock").style.display = "none";
                document.getElementById("default_stock").style.display = "flex";

            }
        }
    </script>

</form>