<?php
include "../../root.class.php";

$html = new html();
$html->add_styles_page();

$db = new db_safeguard();

/* NAV DATA */
$stock_nav_res = $db->query("stock_types", "SELECT * FROM stock_types");

/* PAGE DATA */
$stock_list_res = $db->query("stock_types", "SELECT * FROM stock_types");
?>

<style>
    /* ===== BASE ===== */
    body {
        margin: 0;
        font-family: "Segoe UI", Roboto, Arial, sans-serif;
        background: #f8fafc;
        color: #0f172a;
    }

    h1 {
        margin: 3rem 0 1rem;
        font-size: 3rem;
        color: #1e3a8a;
        scroll-margin-top: 90px;
    }

    /* ===== NAVBAR ===== */
    .stock-navbar {
        position: sticky;
        top: 0;
        z-index: 1000;
        background: #ffffff;
        border-bottom: 3px solid #1e3a8a;
    }

    .stock-nav-container {
        max-width: 1400px;
        margin: auto;
        padding: 0.75rem 1.5rem;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .stock-nav-links {
        display: flex;
        gap: 1rem;
        flex-wrap: wrap;
    }

    .stock-nav-links a {
        text-decoration: none;
        font-size: 1rem;
        font-weight: 700;
        color: #1e3a8a;
        padding: 0.45rem 0.65rem;
        border-radius: 4px;
        transition: 0.2s ease;
    }

    .stock-nav-links a:hover {
        background: #eff6ff;
        color: #f97316;
    }

    /* DOWNLOAD BUTTON */
    .stock-nav-action button {
        background: #f97316;
        color: #fff;
        border: none;
        padding: 0.45rem 0.9rem;
        font-size: 1rem;
        font-weight: 700;
        border-radius: 4px;
        cursor: pointer;
        transition: 0.2s ease;
    }

    .stock-nav-action button:hover {
        background: #ea580c;
    }

    /* ===== CONTENT ===== */
    .form_down {
        max-width: 1400px;
        margin: auto;
        padding: 1.5rem;
    }

    table {
        border-collapse: collapse;
        width: 100%;
        background: #ffffff;
        font-size: 1rem;
    }

    th,
    td {
        border: 1px solid #e5e7eb;
        padding: 0.6rem;
        text-align: left;
    }

    th {
        background: #1e3a8a;
        color: #ffffff;
        font-weight: 700;
    }

    tr:nth-child(even) td {
        background: #f8fafc;
    }

    tr:hover td {
        background: #eff6ff;
    }

    /* ACTION BUTTON */
    .submit_btn {
        background: #1e3a8a;
        color: #ffffff;
        border: none;
        padding: 0.4rem 0.7rem;
        font-size: 1rem;
        font-weight: 700;
        cursor: pointer;
        border-radius: 4px;

    }

    .submit_btn:hover {
        background: #f97316;
    }

    /* SMOOTH SCROLL */
    html {
        scroll-behavior: smooth;
    }
</style>


<body>

    <!-- ===== NAVBAR ===== -->
    <nav class="stock-navbar">
        <div class="stock-nav-container">

            <div class="stock-nav-links">
                <?php while ($nav = $stock_nav_res->fetch_assoc()) { ?>
                    <a href="#stock_<?= $nav['record_id']; ?>">
                        <?= strtoupper($nav['name']); ?>
                    </a>
                <?php } ?>
            </div>

            <div class="stock-nav-action">
                <form action="generate_stock_pdf.php" method="post" target="_blank">
                    <button type="submit" style="margin-top: 2vw;">Download PDF</button>
                </form>
            </div>

        </div>
    </nav>

    <!-- ===== CONTENT ===== -->
    <div class="form_down">

        <?php
        while ($stock_list = $stock_list_res->fetch_assoc()) {

            $table_data = "";

            $all_stock_res = $db->query(
                "stock",
                "SELECT * FROM stock WHERE stock_type_id = '{$stock_list['record_id']}'"
            );

            if ($all_stock_res->num_rows > 0) {

                while ($stock = $all_stock_res->fetch_assoc()) {

                    $stock_data = $db->query(
                        "stock",
                        "SELECT * FROM stock WHERE stock_no = '{$stock['stock_no']}'"
                    )->fetch_assoc();

                    $supplier = $db->query(
                        "suppliers",
                        "SELECT * FROM suppliers WHERE record_id = '{$stock_data['supplier_id']}'"
                    )->fetch_assoc();

                    $stock_trans = $db->query(
                        "stock_trans",
                        "SELECT * FROM stock_trans WHERE stock_no = '{$stock['stock_no']}'"
                    )->fetch_assoc();

                    if ($stock_data['stock_type_id'] == '1') {
                        $table_data .= "
                            <tr>
                                <td>{$stock_data['stock_no']}</td>
                                <td>{$stock_data['item_name']}</td>";

                        if ($stock_data['vehicle_type'] == 'ALL') {
                            $table_data .= "<td>ALL</td>";
                        } else if ($stock_data['vehicle_type'] == 'T') {
                            $table_data .= "<td>TRUCK</td>";
                        } else if ($stock_data['vehicle_type'] == 'D') {
                            $table_data .= "<td>DONKIE</td>";
                        } else if ($stock_data['vehicle_type'] == 'C') {
                            $table_data .= "<td>COMP</td>";
                        } else if ($stock_data['vehicle_type'] == 'CT') {
                            $table_data .= "<td>COMP TRUCK</td>";
                        } else if ($stock_data['vehicle_type'] == 'OTHER') {
                            $table_data .= "<td>OTHER</td>";
                        } else {
                            $table_data .= "<td>{$stock_data['vehicle_type']}</td>";
                        }

                        $table_data .= "
                                <td>{$supplier['name']}</td>
                                <td>{$stock_trans['quantity']}</td>
                                <td>{$stock_data['date_time']}</td>
                                <td>
                                    <input type='button' class='submit_btn'
                                    value='EDIT STOCK' style='width: 90%; max-width: none'
                                    onclick='window.location.href=\"../stock_control/edit_stock.php?record_id={$stock_data['record_id']}\"'>
                                </td>
                            </tr>";
                    } else if ($stock_data['stock_type_id'] == '2' || $stock_data['stock_type_id'] == '5') {

                        $table_data .= "
                            <tr>
                                <td>{$stock_data['stock_no']}</td>
                                <td>{$stock_data['item_name']}</td>
                                <td>{$stock_data['type']}</td>
                                <td>{$stock_data['size']}</td>
                                <td>{$supplier['name']}</td>
                                <td>{$stock_trans['quantity']}</td>
                                <td>{$stock_data['date_time']}</td>
                                <td>
                                    <input type='button' class='submit_btn'
                                    value='EDIT STOCK' style='width: 90%; max-width: none'
                                    onclick='window.location.href=\"../stock_control/edit_stock.php?record_id={$stock_data['record_id']}\"'>
                                </td>
                            </tr>";
                    } else if ($stock_data['stock_type_id'] == '3') {

                        $table_data .= "
                            <tr>
                                <td>{$stock_data['stock_no']}</td>
                                <td>{$stock_data['item_name']}</td>
                                <td>{$stock_data['pump_code']}</td>
                                <td>{$supplier['name']}</td>
                                <td>{$stock_trans['quantity']}</td>
                                <td>{$stock_data['date_time']}</td>
                                <td>
                                    <input type='button' class='submit_btn'
                                    value='EDIT STOCK' style='width: 90%; max-width: none'
                                    onclick='window.location.href=\"../stock_control/edit_stock.php?record_id={$stock_data['record_id']}\"'>
                                </td>
                            </tr>";
                    } else if ($stock_data['stock_type_id'] == '4') {

                        $table_data .= "
                            <tr>
                                <td>{$stock_data['stock_no']}</td>
                                <td>{$stock_data['item_name']}</td>
                                <td>{$stock_data['serial_number']}</td>
                                <td>{$supplier['name']}</td>
                                <td>{$stock_trans['quantity']}</td>
                                <td>{$stock_data['date_time']}</td>
                                <td>
                                    <input type='button' class='submit_btn'
                                    value='EDIT STOCK' style='width: 90%; max-width: none'
                                    onclick='window.location.href=\"../stock_control/edit_stock.php?record_id={$stock_data['record_id']}\"'>
                                </td>
                            </tr>";
                    } else {
                        $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>{$stock_data['date_time']}</td>
                                <td>
                                    <input type='button' class='submit_btn'
                                    value='EDIT STOCK' style='width: 90%; max-width: none'
                                    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>";
            }
            ?>

            <h1 id="stock_<?= $stock_list['record_id']; ?>">
                <?= $stock_list['name']; ?> STOCK
            </h1>

            <?php
            if ($stock_list['name'] == 'OILS') {
                ?>
                <table>
                    <tr>
                        <th>STOCK NO</th>
                        <th>ITEM NAME</th>
                        <th>VEHICLE TYPE</th>
                        <th>SUPPLIER</th>
                        <th>QUANTITY</th>
                        <th>DATE</th>
                        <th></th>
                    </tr>
                    <?= $table_data; ?>
                </table>
                <?php
            } else if ($stock_list['name'] == 'HYDRAULICS' || $stock_list['name'] == 'BOLTS & NUTS') {
                ?>
                    <table>
                        <tr>
                            <th>STOCK NO</th>
                            <th>ITEM NAME</th>
                            <th>TYPE</th>
                            <th>SIZE</th>
                            <th>SUPPLIER</th>
                            <th>QUANTITY</th>
                            <th>DATE</th>
                            <th></th>
                        </tr>
                    <?= $table_data; ?>
                    </table>
                <?php
            } else if ($stock_list['name'] == 'PUMPS') {
                ?>
                        <table>
                            <tr>
                                <th>STOCK NO</th>
                                <th>ITEM NAME</th>
                                <th>PUMP CODE</th>
                                <th>SUPPLIER</th>
                                <th>QUANTITY</th>
                                <th>DATE</th>
                                <th></th>
                            </tr>
                    <?= $table_data; ?>
                        </table>
                <?php
            } else if ($stock_list['name'] == 'HAMMER, BYTEL OR RIEMER') {
                ?>
                            <table>
                                <tr>
                                    <th>STOCK NO</th>
                                    <th>ITEM NAME</th>
                                    <th>SERIAL NUMBER</th>
                                    <th>SUPPLIER</th>
                                    <th>QUANTITY</th>
                                    <th>DATE</th>
                                    <th></th>
                                </tr>
                    <?= $table_data; ?>
                            </table>
                <?php
            } else {
                ?>
                            <table>
                                <tr>
                                    <th>STOCK NO</th>
                                    <th>ITEM NAME</th>
                                    <th>STOCK TYPE</th>
                                    <th>SUPPLIER</th>
                                    <th>QUANTITY</th>
                                    <th>DATE</th>
                                    <th></th>
                                </tr>
                    <?= $table_data; ?>
                            </table>
                <?php
            }
            ?>
            <?php
        }
        ?>

    </div>

</body>