<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();

$stock_data_res = $db->query("stock_control", "SELECT * FROM stock_control WHERE record_id = {$_GET['record_id']}");
// echo "SELECT * FROM stock_control WHERE record_id = {$_GET['record_id']}";
$stock_data = $stock_data_res->fetch_assoc();

$record_id = new input();
$record_id->type("hidden");
$record_id->name("record_id");
$record_id->id("record_id");
$record_id->value($_GET['record_id']);

$stock_id_label = new label();
$stock_id_label->for("stock_no");
$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_from_db('stock', 'stock_no', "stock_no = '{$stock_data['stock_no']}'");
$stock_id->readonly();
$stock_id->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$item_name_label = new label();
$item_name_label->for("item_name");
$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->value_from_db('stock', 'item_name', "stock_no = '{$stock_data['stock_no']}'");
$item_name->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");



// $description_label = new label();
// $description_label->for("description");
// $description_label->value("DESCRIPTION");

// $description = new input();
// $description->type("text");
// $description->name("description");
// $description->id("description");
// $description->style("width:30vw;");
// $description->value_from_db('stock', 'description', "stock_no = '{$stock_data['stock_no']}'");
// $description->style("width:40vw;");

// $quantity_label = new label();
// $quantity_label->for("quantity");
// $quantity_label->value("QUANTITY");

// $quantity = new input();
// $quantity->type("number");
// $quantity->name("quantity");
// $quantity->id("quantity");
// $quantity->style("width:30vw;");
// $quantity->value_from_db('stock', 'quantity', "stock_no = '{$stock_data['stock_no']}'");

$status_label = new label();
$status_label->for("status");
$status_label->value("STATUS");
$status_label->addAttribute("style", "font-size: 1.5em; width: 30%;");

$status = new select();
$status->name("status");
$status->id("status");
$status->add_option("0", "AVAILABLE");
$status->add_option("1", "BOOKED");
$status->add_option("2", "UNAVAILABLE");
$status->addAttribute("style", "font-size: 1.5em; width: 80%; margin: 0.5vw auto;");

$submit_btn = new button();
$submit_btn->value("UPDATE");
$submit_btn->onclick("update_stock()");

?>

<form action="update_stock.ajax.php" method="post">
    <div class="form_down">
        <h1 style="font-size: 3em; margin-bottom: 0.5vw; margin-top: 1.5vw;">UPDATE STOCK</h1>
        <?php
        $record_id->add();
        ?>

        <div style="width: 95%; background-color: white; border-radius: 2vw; padding: 1vw;">
            <div>
                <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
                    // echo "SELECT * FROM stock_type WHERE stock_type_id = '{$stock_data['stock_type_id']}'";
                    
                    $stock_type_res = $db->query("stock_type", "SELECT * FROM stock_type WHERE record_id = '{$stock_data['stock_type']}'");
                    // echo "SELECT * FROM stock_type WHERE record_id = '{$stock_data['stock_type']}'";
                    
                    $stock_type = $stock_type_res->fetch_assoc();
                    ?>

                    <label for="stock_type_label" class="labels" style="font-size: 1.5em; width: 30%;">STOCK TYPE :
                    </label>
                    <select name="stock_type_id" id="stock_type_id" class="inputs"
                        style="width: 80%; font-size: 1.5em; margin: 0.5vw auto;">
                        <option value=""></option>
                        <option value="1" <?= ($stock_type['record_id'] == "1") ? 'selected' : '' ?>>OILS</option>
                        <option value="2" <?= ($stock_type['record_id'] == "2") ? 'selected' : '' ?>>HYDRAULICS</option>
                        <option value="3" <?= ($stock_type['record_id'] == "3") ? 'selected' : '' ?>>PUMPS</option>
                        <option value="4" <?= ($stock_type['record_id'] == "4") ? 'selected' : '' ?>>HAMMER, BYTEL OR
                            RIEMER
                        </option>
                        <option value="5" <?= ($stock_type['record_id'] == "5") ? 'selected' : '' ?>>BOLTS & NUTS</option>
                        <option value="6" <?= ($stock_type['record_id'] == "6") ? 'selected' : '' ?>>CONSUMABLES</option>
                    </select>
                </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

                        ?>
                    </div>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <?php
                        $supplier_id_res = $db->query("suppliers", "SELECT * FROM suppliers WHERE record_id = '{$stock_data['supplier_id']}'");
                        // echo "SELECT * FROM suppliers WHERE record_id = '{$stock_data['supplier_id']}'";
                        
                        $supplier_id = $supplier_id_res->fetch_assoc();
                        ?>

                        <label for="supplier_label" class="labels" style="font-size: 1.5em; width: 30%;">SUPPLIERS :
                        </label>
                        <select name="supplier_id" id="supplier_id" class="inputs"
                            style="width: 80%; font-size: 1.5em; margin: 0.5vw auto;">
                            <option value="0" <?= ($supplier_id['record_id'] == 0) ? 'selected' : '' ?>></option>
                            <option value="1" <?= ($supplier_id['record_id'] == 1) ? 'selected' : '' ?>>HURRICAN</option>
                            <option value="2" <?= ($supplier_id['record_id'] == 2) ? 'selected' : '' ?>>MEPS</option>
                            <option value="3" <?= ($supplier_id['record_id'] == 3) ? 'selected' : '' ?>>STEWARTS & LLOYDS
                            </option>
                            <option value="4" <?= ($supplier_id['record_id'] == 4) ? 'selected' : '' ?>>TRANS ACTIVE
                            </option>
                            <option value="5" <?= ($supplier_id['record_id'] == 5) ? 'selected' : '' ?>>NUS AFRICA</option>
                            <option value="6" <?= ($supplier_id['record_id'] == 6) ? 'selected' : '' ?>>WATER DRILLING
                            </option>
                            <option value="7" <?= ($supplier_id['record_id'] == 7) ? 'selected' : '' ?>>DRILLING PROJECTS
                            </option>
                            <option value="8" <?= ($supplier_id['record_id'] == 8) ? 'selected' : '' ?>>NESLPRUIT FASTENERS
                            </option>
                            <option value="9" <?= ($supplier_id['record_id'] == 9) ? 'selected' : '' ?>>APPLIED GAS
                            </option>
                            <option value="10" <?= ($supplier_id['record_id'] == 10) ? 'selected' : '' ?>>AUTOZONE</option>
                            <option value="11" <?= ($supplier_id['record_id'] == 11) ? 'selected' : '' ?>>NELSPRUIT BRAKE &
                                CLUTH
                            </option>
                            <option value="12" <?= ($supplier_id['record_id'] == 12) ? 'selected' : '' ?>>AF TRADING
                            </option>
                            <option value="13" <?= ($supplier_id['record_id'] == 13) ? 'selected' : '' ?>>OTHER</option>
                        </select>
                        
                    </div>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="quantity_label" class="labels" style="font-size: 1.5em; width: 30%;">QUANTITY : </label>
                        <input type="number" name="quantity" id="quantity" class="inputs"
                            style="font-size: 1.5em; width: 80%;" value="<?= $stock_data['quantity'] ?>">
                    </div>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="weight_label" class="inputs" style="font-size: 1.5em; width: 30%;">AMOUNT</label>
                        <input type="number" id="weight" name="weight" class="inputs" style="font-size: 1.5em; width: 80%;" value="<?= $stock_data['weight'] ?>">
                    </div>
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="" class="labels" style=""></label>
                        <input type="number" name="" id="" class="inputs" styles="">
                    </div>
                    <div id="oil_stock" style="display: none; flex-direction: row; width: 100%;">
                        <label for="vehicle_label" class="labels" style="font-size: 1.5em; width: 30%;"></label>
                        <select name="vehicle_type" id="vehicle_type" class="inputs"
                            style="font-size: 1.5em; width: 30%;">
                            <option value=""></option>
                            <option value="ALL">ALL</option>
                            <option value="T">TRUCK</option>
                            <option value="D">DONKIE</option>
                            <option value="C">COMP</option>
                            <option value="CT">COMP TRUCK</option>
                            <option value="OTHER">OTHER</option>
                        </select>
                    </div>
                </div>
            </div>

            <div style="display: flex; flex-direction: row; width: 100%;">
                <?php
                $status_label->add();
                $status->add();
                ?>
            </div>
            <?php
            $submit_btn->add();
            ?>
        </div>
    </div>

    <script>
        change_stock_type();

        function change_stock_type() {
            var stock_type = document.getElementById("stock_type_id").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>


<?php
$ajax = new js_ajax();
$ajax->function_name("update_stock");
$ajax->update("stock_control");
$ajax->on_success("STOCK UPDATED");
$ajax->redirect("../stock_control/home.php");