<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();

$stock_data_res = $db->query("stock", "SELECT * FROM stock 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;");

$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']}'");

$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;");
$serial_number->value_from_db("stock", "serial_number", "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%;");

$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;">EDIT 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_types", "SELECT * FROM stock_types WHERE record_id = '{$stock_data['stock_type_id']}'");
                    $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" onchange="change_stock_type()" 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 id="hydrolic_stock" style="display: none; flex-direction: column; width: 100%;">
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="" class="labels" style="font-size: 1.5em; width: 30%;">TYPE : </label>
                        <select name="type" id="type" onchange="change_type();" class="inputs"
                            style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;">
                            <option value="" <?= ($stock_data['type'] == '') ? 'selected' : '' ?>></option>
                            <option value="Pipe" <?= ($stock_data['type'] == 'Pipe') ? 'selected' : '' ?>>PIPE</option>
                            <option value="Fitting" <?= ($stock_data['type'] == 'Female elbow' || $stock_data['type'] == 'Male Straight' || $stock_data['type'] == 'Female Straight') ? 'selected' : '' ?>>FITTING</option>
                        </select>
                    </div>

                    <div id="pipe_section" style="display: none; flex-direction: row; width: 100%;">
                        <div style="display: flex; flex-direction: row; width: 100%;">
                            <label for="size_labels" class="labels" style="font-size: 1.5em; width: 30%;">PIPE SIZE :
                            </label>
                            <select name="pipe_size" id="pipe_size" class="inputs"
                                style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;" onchange="change_item_name()">
                                <option value="" <?= ($stock_data['size'] == '') ? 'selected' : '' ?>></option>
                                <option value="2" <?= ($stock_data['size'] == '2') ? 'selected' : '' ?>>2"</option>
                                <option value="1 1/2" <?= ($stock_data['size'] == '1 1/2') ? 'selected' : '' ?>>1 1/2"
                                </option>
                                <option value="1 p.m 2 wire" <?= ($stock_data['size'] == '1 p.m 2 wire') ? 'selected' : '' ?>>1" p.m 2 wire</option>
                                <option value="1 p.m 4 wire" <?= ($stock_data['size'] == '1 p.m 4 wire') ? 'selected' : '' ?>>1" p.m 4 wire</option>
                                <option value="1/2 p.m" <?= ($stock_data['size'] == '1/2 p.m') ? 'selected' : '' ?>>1/2"
                                    p.m</option>
                                <option value="3/4 p.m" <?= ($stock_data['size'] == '3/4 p.m') ? 'selected' : '' ?>>3/4"
                                    p.m</option>
                                <option value="3/8 p.m" <?= ($stock_data['size'] == '3/8 p.m') ? 'selected' : '' ?>>3/8"
                                    p.m</option>
                                <option value="5/8 p.m" <?= ($stock_data['size'] == '5/8 p.m') ? 'selected' : '' ?>>5/8"
                                    p.m</option>
                            </select>
                        </div>
                    </div>

                    <div id="fittings_section" style="display: none; flex-direction: column; width: 100%;">
                        <div style="display: flex; flex-direction: row; width: 100%; align-items: baseline;">
                            <label for="size_labels" class="labels" style="font-size: 1.5em; width: 30%;">COLLAR SIZE :
                            </label>
                            <select name="collar_size" id="collar_size" class="inputs"
                                style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;" onchange="change_item_name()">
                                <option value="" <?= ($stock_data['size'] == '') ? 'selected' : '' ?>></option>
                                <option value="2" <?= ($stock_data['size'] == '2') ? 'selected' : '' ?>>2"</option>
                                <option value="1 1/2" <?= ($stock_data['size'] == '1 1/2') ? 'selected' : '' ?>>1 1/2"
                                </option>
                                <option value="1 p.m 2 wire" <?= ($stock_data['size'] == '1 p.m 2 wire') ? 'selected' : '' ?>>1" p.m 2 wire</option>
                                <option value="1 p.m 4 wire" <?= ($stock_data['size'] == '1 p.m 4 wire') ? 'selected' : '' ?>>1" p.m 4 wire</option>
                                <option value="1/2 p.m" <?= ($stock_data['size'] == '1/2 p.m') ? 'selected' : '' ?>>1/2"
                                    p.m</option>
                                <option value="3/4 p.m" <?= ($stock_data['size'] == '3/4 p.m') ? 'selected' : '' ?>>3/4"
                                    p.m</option>
                                <option value="3/8 p.m" <?= ($stock_data['size'] == '3/8 p.m') ? 'selected' : '' ?>>3/8"
                                    p.m</option>
                                <option value="5/8 p.m" <?= ($stock_data['size'] == '5/8 p.m') ? 'selected' : '' ?>>5/8"
                                    p.m</option>
                            </select>
                        </div>

                        <div style="display: flex; flex-direction: row; width: 100%; align-items: baseline;">
                            <label for="type_labels" class="labels" style="font-size: 1.5em; width: 30%;">CONNECTOR TYPE
                                : </label>
                            <select name="type_size" id="type_size" class="inputs"
                                style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;" onchange="change_item_name()">
                                <option value=""></option>
                                <option value="Female straight" <?= ($stock_data['type'] == 'Female straight') ? 'selected' : '' ?>>Female Straight</option>
                                <option value="Female elbow" <?= ($stock_data['type'] == 'Female elbow') ? 'selected' : '' ?>>Female Elbow</option>
                                <option value="Male straight" <?= ($stock_data['type'] == 'Male straight') ? 'selected' : '' ?>>Male Straight</option>
                            </select>
                        </div>
                    </div>

                    <script>

                        change_type();

                        function change_type() {
                            var type = document.getElementById("type").value;

                            if (type == "Pipe") {
                                var size = document.getElementById("pipe_size").value;
                                var pipe_section = document.getElementById("pipe_section");
                                var fittings_section = document.getElementById("fittings_section");

                                pipe_section.style.display = "flex";
                                fittings_section.style.display = "none";

                                let xhr = new XMLHttpRequest();
                                xhr.open("POST", "get_type.ajax.php", true);
                                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                                xhr.onreadystatechange = function () {
                                    if (xhr.readyState === 4 && xhr.status === 200) {
                                        console.log(xhr.responseText);
                                    }
                                };

                                xhr.send("size=" + size);

                            } else if (type == "Fitting") {

                                var size = document.getElementById("collar_size").value;
                                var pipe_section = document.getElementById("pipe_section");
                                var fittings_section = document.getElementById("fittings_section");

                                pipe_section.style.display = "none";
                                fittings_section.style.display = "flex";

                                let xhr = new XMLHttpRequest();
                                xhr.open("POST", "get_type.ajax.php", true);
                                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                                xhr.onreadystatechange = function () {
                                    if (xhr.readyState === 4 && xhr.status === 200) {
                                        console.log(xhr.responseText);
                                    }
                                };

                                xhr.send("size=" + size);

                            }
                        }
                    </script>

                </div>

                <div id="item_div" style="display: flex; flex-direction: row; width: 100%;">
                    <?php
                    $item_name_label->add();
                    $item_name->add();
                    ?>
                </div>

                <div id="bn_stock" style="display: none; flex-direction: column; width: 100%;">
                    <div style="display: flex; flex-direction: row; width: 100%; align-items: baseline;">
                        <label for="" class="labels" style="font-size: 1.5em; width: 30%;">SIZE : </label>
                        <select name="bn_type" id="bn_type" onchange="change_item_name()" class="inputs"
                            style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;">
                            <option value="" <?= ($stock_data['type'] == '') ? 'selected' : '' ?>></option>
                            <option value="M6" <?= ($stock_data['type'] == 'M6') ? 'selected' : '' ?>>M6</option>
                            <option value="M8" <?= ($stock_data['type'] == 'M8') ? 'selected' : '' ?>>M8</option>
                            <option value="M10" <?= ($stock_data['type'] == 'M10') ? 'selected' : '' ?>>M10</option>
                            <option value="M12" <?= ($stock_data['type'] == 'M12') ? 'selected' : '' ?>>M12</option>
                            <option value="M16" <?= ($stock_data['type'] == 'M16') ? 'selected' : '' ?>>M16</option>
                        </select>
                    </div>

                    <div style="display: flex; flex-direction: row; width: 100%; align-items: baseline;">
                        <label for="" class="labels" style="font-size: 1.5em; width: 30%;">TYPE : </label>
                        <select name="bn_size" id="bn_size" onchange="change_item_name()" class="inputs"
                            style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;">
                            <option value="" <?= ($stock_data['size'] == '') ? 'selected' : '' ?>></option>
                            <option value="25" <?= ($stock_data['size'] == '25') ? 'selected' : '' ?>>25</option>
                            <option value="45" <?= ($stock_data['size'] == '45') ? 'selected' : '' ?>>45</option>
                            <option value="70" <?= ($stock_data['size'] == '70') ? 'selected' : '' ?>>70</option>
                            <option value="100" <?= ($stock_data['size'] == '100') ? 'selected' : '' ?>>100</option>
                            <option value="Nuts" <?= ($stock_data['size'] == 'Nuts') ? 'selected' : '' ?>>Nuts</option>
                            <option value="Lock Nuts" <?= ($stock_data['size'] == 'Lock Nuts') ? 'selected' : '' ?>>Lock
                                Nuts
                            </option>
                            <option value="Flat Washers" <?= ($stock_data['size'] == 'Flat Washers') ? 'selected' : '' ?>>
                                Flat
                                Washers</option>
                            <option value="Spring Washers" <?= ($stock_data['size'] == 'Spring Washers') ? 'selected' : '' ?>>Spring
                                Washers</option>
                        </select>
                    </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_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>

                        <?php
                        $stock_quantity_res = $db->query("stock_trans", "SELECT * FROM stock_trans WHERE stock_no = '{$stock_data['stock_no']}'");
                        // echo "SELECT * FROM stock_trans WHERE stock_no = '{$stock_data['stock_no']}'";
                        $stock_quantity = $stock_quantity_res->fetch_assoc();
                        ?>
                        <div style="display: flex; flex-direction: row; width: 100%;">
                            <label for="quantity_label" class="labels" style="font-size: 1.5em; width: 30%;">STOCK
                                QUANTITY
                                : </label>
                            <input type="number" name="quantity" id="quantity" class="inputs"
                                style="font-size: 1.5em; width: 80%;margin: 0.5vw auto;"
                                value="<?php echo $stock_quantity['quantity'] ?>">
                        </div>
                        <div style="display: flex; flex-direction: row; width: 100%;">
                            <label for="quantity_label" class="labels" style="font-size: 1.5em; width: 30%;">UNIT TYPE :
                            </label>
                            <select name="unit_of_measure" id="unit_of_measure" class="inputs"
                                style="font-size: 1.5em; width: 80%;margin: 0.5vw auto;">
                                <option value=""></option>
                                <option value="liters" <?= ($stock_data['unit_of_measure'] === 'liters') ? 'selected' : '' ?>>
                                    liters</option>
                                <option value="kg" <?= ($stock_data['unit_of_measure'] === 'kg') ? 'selected' : '' ?>>kg
                                </option>
                                <option value="meters" <?= ($stock_data['unit_of_measure'] === 'meters') ? 'selected' : '' ?>>
                                    meters</option>

                            </select>
                        </div>
                        <div style="display: flex; flex-direction: row; width: 100%;">
                            <label for="cost_labels" class="labels" style="font-size: 1.5em; width: 30%;">COST :
                            </label>
                            <input type="number" name="item_price" id="item_price" step="0.01" class="inputs"
                                style="width: 80%; font-size: 1.5em; margin: 0.5vw auto;"
                                value="<?php echo $stock_data['item_price'] ?>">
                        </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%;">VEHICLE TYPE
                                :
                            </label>
                            <select name="vehicle_type" id="vehicle_type" class="inputs"
                                style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;">
                                <option value="" <?= ($stock_data['vehicle_type'] == '') ? 'selected' : '' ?>></option>
                                <option value="ALL" <?= ($stock_data['vehicle_type'] == 'ALL') ? 'selected' : '' ?>>ALL
                                </option>
                                <option value="T" <?= ($stock_data['vehicle_type'] == 'T') ? 'selected' : '' ?>>TRUCK
                                </option>
                                <option value="D" <?= ($stock_data['vehicle_type'] == 'D') ? 'selected' : '' ?>>DONKIE
                                </option>
                                <option value="C" <?= ($stock_data['vehicle_type'] == 'C') ? 'selected' : '' ?>>COMP
                                </option>
                                <option value="CT" <?= ($stock_data['vehicle_type'] == 'CT') ? 'selected' : '' ?>>COMP
                                    TRUCK
                                </option>
                                <option value="OTHER" <?= ($stock_data['vehicle_type'] == 'OTHER') ? 'selected' : '' ?>>
                                    OTHER
                                </option>
                            </select>
                        </div>
                    </div>
                </div>

                <div id="pump_stock" style="display: none; flex-direction: column; ">
                    <div style="display: flex; flex-direction: row; width: 100%;">
                        <label for="code_label" class="labels" style="font-size: 1.5em; width: 30%;">PUMP CODE :
                        </label>
                        <input type="text" id="pump_code" name="pump_code" class="inputs"
                            style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;"
                            value="<?php echo $stock_data['pump_code'] ?>">
                    </div>
                </div>

                <div style="display: flex; flex-direction: row; width: 100%;">
                    <?php
                    $status_label->add();
                    ?>
                    <select name="status" id="status" class="inputs"
                        style="font-size: 1.5em; width: 80%; margin: 0.5vw auto;">
                        <option value=""></option>
                        <option value="0" <?= ($stock_data['status'] == '0') ? 'selected' : '' ?>>AVAILABLE</option>
                        <option value="1" <?= ($stock_data['status'] == '1') ? 'selected' : '' ?>>UNAVAILABLE</option>
                    </select>
                </div>
                <?php
                $submit_btn->add();
                ?>
            </div>
        </div>

        <script>
            change_stock_type();

            function change_stock_type() {
                var stock_type = document.getElementById("stock_type_id").value;

                var item_name_input = document.getElementById("item_name");
                var supplier_input = document.getElementById("supplier_id");
                var quantity_input = document.getElementById("quantity");
                var unit_of_measure_input = document.getElementById("unit_of_measure");
                var item_price_input = document.getElementById("item_price");
                var vehicle_type_input = document.getElementById("vehicle_type");
                var pump_code_input = document.getElementById("pump_code");
                var status_input = document.getElementById("status");

                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";
                    document.getElementById("hydrolic_stock").style.display = "none";
                    document.getElementById("bn_stock").style.display = "none";

                } 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";
                    document.getElementById("hydrolic_stock").style.display = "none";
                    document.getElementById("bn_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";
                    document.getElementById("hydrolic_stock").style.display = "none";
                    document.getElementById("bn_stock").style.display = "none";
                } else if (stock_type == "2") {
                    document.getElementById("pump_stock").style.display = "none";
                    document.getElementById("hammer_stock").style.display = "none";
                    document.getElementById("default_stock").style.display = "flex";
                    document.getElementById("oil_stock").style.display = "none";
                    document.getElementById("hydrolic_stock").style.display = "flex";
                    document.getElementById("bn_stock").style.display = "none";

                    let xhr = new XMLHttpRequest();
                    xhr.open("POST", "get_type.ajax.php", true);
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                    xhr.onreadystatechange = function () {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                            console.log(xhr.responseText);
                        }
                    };

                    xhr.send("stock_type=" + stock_type);

                } else if (stock_type == "5") {
                    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";
                    document.getElementById("bn_stock").style.display = "flex";
                    document.getElementById("hydrolic_stock").style.display = "none";

                } else if (stock_type == "6") {
                    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";
                    document.getElementById("bn_stock").style.display = "none";
                    document.getElementById("hydrolic_stock").style.display = "none";

                } 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 = "none";
                    document.getElementById("hydrolic_stock").style.display = "none";
                    document.getElementById("bn_stock").style.display = "none";

                }

                //add a listener function to stock type change to clear all inputs
                document.getElementById("stock_type_id").addEventListener("change", function () {
                    item_name_input.value = "";
                    supplier_input.value = "";
                    quantity_input.value = "";
                    unit_of_measure_input.value = "";
                    item_price_input.value = "";
                    vehicle_type_input.value = "";
                    pump_code_input.value = "";
                    status_input.value = "";
                });
            }

            function change_item_name() {
                var stock_type = document.getElementById("stock_type_id").value;
                var type = document.getElementById("type").value;
                var pipe_size = document.getElementById("pipe_size").value;
                var collar_size = document.getElementById("collar_size").value;
                var type_size = document.getElementById("type_size").value;
                var bn_type = document.getElementById("bn_type").value;
                var bn_size = document.getElementById("bn_size").value;

                let item_name = "";

                if (stock_type === "2") {
                    if (type === "Pipe") {
                        item_name = pipe_size + " Pipe";
                    } else {
                        item_name = collar_size + " " + type_size;
                    }
                } else if (stock_type === "5") {
                    item_name = bn_type + " X " +  bn_size;
                }

                // ✅ Update the input field
                document.getElementById("item_name").value = item_name;
            }

        </script>

</form>