<?php
$page_title = 'View Barcode IDs';
require_once('includes/load.php');
page_require_level(2);

// Handle search functionality
$search_query = '';
if (isset($_GET['search'])) {
    $search_query = $db->escape($_GET['search']);
    $sql  = "SELECT part_number, name, barcode_id FROM products ";
    $sql .= "WHERE categorie_id = 7 ";
    $sql .= "AND (part_number LIKE '%{$search_query}%' OR barcode_id LIKE '%{$search_query}%')";
} else {
    $sql = "SELECT part_number, name, barcode_id FROM products WHERE categorie_id = 7";
}

$result = $db->query($sql);
$products = [];
while ($row = $db->fetch_assoc($result)) {
    $products[] = $row;
}

include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>
                    <span class="glyphicon glyphicon-barcode"></span>
                    <span>Barcode ID List (Category 7)</span>
                </strong>
            </div>
            <div class="panel-body">
                <!-- Search Form -->
                <form method="GET" class="form-inline">
                    <div class="form-group">
                        <input type="text" name="search" class="form-control" 
                               placeholder="Search by part number or barcode ID" 
                               value="<?php echo htmlspecialchars($search_query); ?>">
                    </div>
                    <button type="submit" class="btn btn-primary">Search</button>
                    <a href="view_barcode_id.php" class="btn btn-default">Reset</a>
                </form>
                <br>

                <!-- Table -->
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Part Number</th>
                            <th>Product Name</th>
                            <th>Barcode ID</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (count($products) > 0): ?>
                            <?php foreach ($products as $index => $product): ?>
                                <tr>
                                    <td><?php echo $index + 1; ?></td>
                                    <td><?php echo htmlspecialchars($product['part_number']); ?></td>
                                    <td><?php echo htmlspecialchars($product['name']); ?></td>
                                    <td><?php echo htmlspecialchars($product['barcode_id']); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="4" class="text-center">No products found</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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