<?php require_once '../includes/header.php'; ?>

<h1 class="h2 mb-4">Brands</h1>

<?php $flash = getFlash(); if ($flash): ?>
<div class="alert alert-<?= $flash['type'] ?> alert-dismissible fade show" role="alert">
    <?= htmlspecialchars($flash['message']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="card shadow-sm">
    <div class="card-header bg-white d-flex justify-content-between align-items-center">
        <h5 class="mb-0">All Brands</h5>
        <a href="brand_add.php" class="btn btn-primary btn-sm"><i class="bi bi-plus-circle"></i> Add Brand</a>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>Logo</th>
                        <th>Name</th>
                        <th>Slug</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $brands = $pdo->query("SELECT * FROM brands ORDER BY name")->fetchAll();
                if (!$brands): ?>
                    <tr><td colspan="4" class="text-center py-3">No brands found.</td></tr>
                <?php else: foreach ($brands as $b): ?>
                    <tr>
                        <td><?= $b['id'] ?></td>
                        <td>
                            <?php if ($b['logo_url']): ?>
                                <img src="../assets/uploads/<?= htmlspecialchars($b['logo_url']) ?>" width="40" height="40" style="object-fit:cover;" class="rounded">
                            <?php else: ?>
                                <span class="text-muted">—</span>
                            <?php endif; ?>
                        </td>
                        <td><?= htmlspecialchars($b['name']) ?></td>
                        <td><?= htmlspecialchars($b['slug']) ?></td>
                        <td class="text-end">
                            <a href="brand_edit.php?id=<?= $b['id'] ?>" class="btn btn-sm btn-warning"><i class="bi bi-pencil"></i></a>
                            <a href="brand_delete.php?id=<?= $b['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete?')"><i class="bi bi-trash"></i></a>
                        </td>
                    </tr>
                <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>