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

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

<!-- Flash Message -->
<?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 Categories</h5>
        <a href="category_add.php" class="btn btn-primary btn-sm"><i class="bi bi-plus-circle"></i> Add Category</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>Icon</th>
                        <th>Name</th>
                        <th>Slug</th>
                        <th>Parent</th>
                        <th>Sort Order</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $stmt = $pdo->query("SELECT c.*, parent.name AS parent_name FROM categories c LEFT JOIN categories parent ON c.parent_id = parent.id ORDER BY c.sort_order ASC, c.name ASC");
                $categories = $stmt->fetchAll();
                if (!$categories): ?>
                    <tr><td colspan="7" class="text-center py-3">No categories found.</td></tr>
                <?php else: foreach ($categories as $cat): ?>
                    <tr>
                        <td><?= $cat['id'] ?></td>
                        <td>
                            <?php if ($cat['icon_url']): ?>
                                <img src="../assets/uploads/<?= htmlspecialchars($cat['icon_url']) ?>" width="40" height="40" style="object-fit:cover;" class="rounded">
                            <?php else: ?>
                                <span class="text-muted">—</span>
                            <?php endif; ?>
                        </td>
                        <td><?= htmlspecialchars($cat['name']) ?></td>
                        <td><?= htmlspecialchars($cat['slug']) ?></td>
                        <td><?= htmlspecialchars($cat['parent_name'] ?? '—') ?></td>
                        <td><?= $cat['sort_order'] ?></td>
                        <td class="text-end">
                            <a href="category_edit.php?id=<?= $cat['id'] ?>" class="btn btn-sm btn-warning"><i class="bi bi-pencil"></i></a>
                            <a href="category_delete.php?id=<?= $cat['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this category?')"><i class="bi bi-trash"></i></a>
                        </td>
                    </tr>
                <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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