<?php
require_once('includes/load.php');
require_once('includes/database.php');

$page_title = 'Stock Data';
page_require_level(3);

include_once('layouts/header.php');

$db = new MySqli_DB();

// Function to get stock on hand
function getStockOnHand() {
    global $db;
    $sql = "SELECT * FROM stock_table";
    return $db->query($sql);
}

// Function to get in stock transactions
function getInStockTransactions($part_number) {
    global $db;
    $sql = "SELECT * FROM in_stock_table WHERE part_number = '$part_number'";
    return $db->query($sql);
}

// Function to get out stock transactions
function getOutStockTransactions($part_number) {
    global $db;
    $sql = "SELECT * FROM out_stock_table WHERE part_number = '$part_number'";
    return $db->query($sql);
}
?>

<h2>Stock Data</h2>

<ul class="nav nav-tabs" id="stockTabs" role="tablist">
    <li class="nav-item">
        <a class="nav-link active" id="stockOnHand-tab" data-toggle="tab" href="#stockOnHand" role="tab" aria-controls="stockOnHand" aria-selected="true">Stock On Hand</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="inStock-tab" data-toggle="tab" href="#inStock" role="tab" aria-controls="inStock" aria-selected="false">In Stock</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="outStock-tab" data-toggle="tab" href="#outStock" role="tab" aria-controls="outStock" aria-selected="false">Out Stock</a>
    </li>
</ul>

<div class="tab-content" id="stockTabsContent">
    <!-- Stock On Hand Tab -->
    <div class="tab-pane fade show active" id="stockOnHand" role="tabpanel" aria-labelledby="stockOnHand-tab">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Part Number</th>
                    <th>Stock On Hand</th>
                </tr>
            </thead>
            <tbody>
                <?php $stockOnHand = getStockOnHand(); ?>
                <?php while ($row = $db->fetch_assoc($stockOnHand)) : ?>
                    <tr>
                        <td><?php echo $row['id']; ?></td>
                        <td><?php echo htmlspecialchars($row['part_number']); ?></td>
                        <td><?php echo $row['stock_on_hand']; ?></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>

    <!-- In Stock Tab -->
    <div class="tab-pane fade" id="inStock" role="tabpanel" aria-labelledby="inStock-tab">
        <form class="form-inline mb-3" method="get">
            <div class="form-group mr-2">
                <input type="text" class="form-control" name="in_search" placeholder="Search Part Number">
            </div>
            <button type="submit" class="btn btn-primary">Search</button>
        </form>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Part Number</th>
                    <th>In Stock</th>
                    <th>In Date</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (isset($_GET['in_search'])) {
                    $part_number = $db->escape($_GET['in_search']);
                    $inStockTransactions = getInStockTransactions($part_number);
                } else {
                    $inStockTransactions = $db->query("SELECT * FROM in_stock_table");
                }
                ?>
                <?php while ($row = $db->fetch_assoc($inStockTransactions)) : ?>
                    <tr>
                        <td><?php echo $row['id']; ?></td>
                        <td><?php echo htmlspecialchars($row['part_number']); ?></td>
                        <td><?php echo $row['in_stock']; ?></td>
                        <td><?php echo $row['in_date']; ?></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>

    <!-- Out Stock Tab -->
    <div class="tab-pane fade" id="outStock" role="tabpanel" aria-labelledby="outStock-tab">
        <form class="form-inline mb-3" method="get">
            <div class="form-group mr-2">
                <input type="text" class="form-control" name="out_search" placeholder="Search Part Number">
            </div>
            <button type="submit" class="btn btn-primary">Search</button>
        </form>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Part Number</th>
                    <th>Out Stock</th>
                    <th>Out Date</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (isset($_GET['out_search'])) {
                    $part_number = $db->escape($_GET['out_search']);
                    $outStockTransactions = getOutStockTransactions($part_number);
                } else {
                    $outStockTransactions = $db->query("SELECT * FROM out_stock_table");
                }
                ?>
                <?php while ($row = $db->fetch_assoc($outStockTransactions)) : ?>
                    <tr>
                        <td><?php echo $row['id']; ?></td>
                        <td><?php echo htmlspecialchars($row['part_number']); ?></td>
                        <td><?php echo $row['out_stock']; ?></td>
                        <td><?php echo $row['out_date']; ?></td>
                    </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#stockTabs a').on('click', function(e) {
            e.preventDefault();
            $(this).tab('show');
        });
    });
</script>

<?php include_once('layouts/footer.php'); ?>
