<?php
include 'config.php';
checkLogin();

/* ===============================
   FILTER INPUTS
================================ */
$serial = $_GET['serial'] ?? '';
$brand  = $_GET['brand'] ?? '';
$model  = $_GET['model'] ?? '';

/* ===============================
   BUILD QUERY SAFELY
================================ */
$sql = "
SELECT 
    s.id,
    s.serial_number,
    m.brand,
    m.model
FROM laptop_serials s
JOIN laptop_models m ON s.model_id = m.id
WHERE s.printed = 1
";

$params = [];
$types  = "";

/* Serial filter */
if (!empty($serial)) {
    $sql .= " AND s.serial_number LIKE ? ";
    $params[] = "%$serial%";
    $types .= "s";
}

/* Brand filter */
if (!empty($brand)) {
    $sql .= " AND m.brand = ? ";
    $params[] = $brand;
    $types .= "s";
}

/* Model filter */
if (!empty($model)) {
    $sql .= " AND m.model = ? ";
    $params[] = $model;
    $types .= "s";
}

$sql .= " ORDER BY s.id DESC LIMIT 200";

/* ===============================
   PREPARE & EXECUTE
================================ */
$stmt = $conn->prepare($sql);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();

/* ===============================
   FILTER DATA
================================ */
$brands = $conn->query("SELECT DISTINCT brand FROM laptop_models ORDER BY brand");
$models = $conn->query("SELECT DISTINCT model FROM laptop_models ORDER BY model");

include 'layout/header.php';
include 'layout/sidebar.php';
?>

<h3 class="mb-4">Printed Labels</h3>

<!-- FILTER FORM -->
<div class="card p-4 mb-4">
<form method="get" class="row g-3">

    <div class="col-md-4">
        <label class="form-label">Serial Number</label>
        <input type="text" name="serial" class="form-control"
               value="<?= htmlspecialchars($serial) ?>"
               placeholder="Search serial">
    </div>

    <div class="col-md-4">
        <label class="form-label">Brand</label>
        <select name="brand" class="form-select">
            <option value="">All Brands</option>
            <?php while ($b = $brands->fetch_assoc()) { ?>
                <option value="<?= $b['brand'] ?>"
                    <?= ($brand == $b['brand']) ? 'selected' : '' ?>>
                    <?= strtoupper($b['brand']) ?>
                </option>
            <?php } ?>
        </select>
    </div>

    <div class="col-md-4">
        <label class="form-label">Model</label>
        <select name="model" class="form-select">
            <option value="">All Models</option>
            <?php while ($m = $models->fetch_assoc()) { ?>
                <option value="<?= $m['model'] ?>"
                    <?= ($model == $m['model']) ? 'selected' : '' ?>>
                    <?= strtoupper($m['model']) ?>
                </option>
            <?php } ?>
        </select>
    </div>

    <div class="col-md-12 text-end">
        <button class="btn btn-primary">
            🔍 Apply Filters
        </button>
        <a href="recently_printed.php" class="btn btn-secondary">
            Reset
        </a>
    </div>

</form>
</div>

<!-- RESULT TABLE -->
<div class="card p-4">
<table class="table table-bordered table-hover">
    <thead class="table-light">
        <tr>
            <th>#</th>
            <th>Brand</th>
            <th>Model</th>
            <th>Serial Number</th>
            <th width="120">Action</th>
        </tr>
    </thead>
    <tbody>

    <?php
    $i = 1;
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
    ?>
        <tr>
            <td><?= $i++ ?></td>
            <td><?= strtoupper($row['brand']) ?></td>
            <td><?= strtoupper($row['model']) ?></td>
            <td><strong><?= $row['serial_number'] ?></strong></td>
            <td>
                <a href="print_barcode.php?id=<?= $row['id'] ?>"
                   target="_blank"
                   class="btn btn-sm btn-outline-primary">
                   Re-Print
                </a>
            </td>
        </tr>
    <?php
        }
    } else {
    ?>
        <tr>
            <td colspan="5" class="text-center text-muted">
                No printed labels found
            </td>
        </tr>
    <?php } ?>

    </tbody>
</table>
</div>

<?php include 'layout/footer.php'; ?>
