<?php include "../../root.class.php";
$html = new html();
$html->add_styles_page();
$db = new db_safeguard();

$stock_list_res = $db->query("stock_type", "SELECT * FROM stock_type");

?>
<div class="form_down">

    <?php

    while ($stock_list = $stock_list_res->fetch_assoc()) {

        $table_data = ""; // ✅ reset per stock type
    
        $all_stock_res = $db->query(
            "stock_control",
            "SELECT * FROM stock_control WHERE stock_type = '{$stock_list['record_id']}'"
        );

        if ($all_stock_res->num_rows > 0) {

            while ($stock = $all_stock_res->fetch_assoc()) {

                $stock_data_res = $db->query(
                    "stock",
                    "SELECT * FROM stock WHERE stock_no = '{$stock['stock_no']}'"
                );
                $stock_data = $stock_data_res->fetch_assoc();

                $supplier_res = $db->query(
                    "suppliers",
                    "SELECT * FROM suppliers WHERE record_id = '{$stock_data['supplier_id']}'"
                );
                $supplier = $supplier_res->fetch_assoc();

                $stock_trans_res = $db->query(
                    "stock_trans",
                    "SELECT * FROM stock_trans WHERE stock_no = '{$stock['stock_no']}'"
                );
                $stock_trans = $stock_trans_res->fetch_assoc();

                // echo "{$stock['record_id']}";

                $table_data .= "
                <tr>
                    <td>{$stock_data['stock_no']}</td>
                    <td>{$stock_data['item_name']}</td>
                    <td>{$stock_list['name']}</td>
                    <td>{$supplier['name']}</td>
                    <td>{$stock_trans['quantity']}</td>
                    <td>
                        <input type='button' class='submit_btn'
                        value='EDIT STOCK'
                        onclick='window.location.href=\"../stock_control/edit_stock.php?record_id={$stock_data['record_id']}\"'>
                    </td>
                </tr>
            ";
            }

        } else {
            $table_data = "
            <tr>
                <td colspan='6' style='text-align:center;'>NO STOCK FOUND</td>
            </tr>
        ";
        }
        ?>

        <style>
            table {
                border-collapse: collapse;
                width: 90%;
                background-color: whitesmoke;
            }

            table,
            th,
            td {
                border: 2px solid black;
                padding: 1vw;
            }

            th {
                padding: 1vw;
            }
        </style>

        <h1><?= $stock_list['name']; ?> STOCK</h1>

        <table>
            <tr>
                <th>STOCK NO</th>
                <th>ITEM NAME</th>
                <th>STOCK TYPE</th>
                <th>SUPPLIER</th>
                <th>QUANTITY</th>
                <th></th>
            </tr>
            <?= $table_data; ?>
        </table>
        <br><br>

    <?php } ?>

</div>