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

<h1 class="h2 mb-4">Banners</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 Banners</h5>
        <a href="banner_add.php" class="btn btn-primary btn-sm"><i class="bi bi-plus-circle"></i> Add Banner</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>Image</th>
                        <th>Video URL</th>
                        <th>Link Type</th>
                        <th>Link Value</th>
                        <th>Sort</th>
                        <th>Active</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $banners = $pdo->query("SELECT * FROM banners ORDER BY sort_order ASC")->fetchAll();
                if (empty($banners)): ?>
                    <tr><td colspan="8" class="text-center py-3">No banners found.</td></tr>
                <?php else: foreach ($banners as $b): ?>
                    <tr>
                        <td><?= $b['id'] ?></td>
                        <td>
                            <?php if ($b['image_url']): ?>
                                <img src="<?= htmlspecialchars($b['image_url']) ?>" width="60" style="object-fit:cover;" class="rounded">
                            <?php else: ?>
                                <span class="text-muted">—</span>
                            <?php endif; ?>
                        </td>
                        <td><?= htmlspecialchars($b['video_url'] ?? '') ?></td>
                        <td><?= htmlspecialchars($b['link_type']) ?></td>
                        <td><?= htmlspecialchars($b['link_value'] ?? '') ?></td>
                        <td><?= $b['sort_order'] ?></td>
                        <td><?= $b['is_active'] ? '<span class="badge bg-success">Yes</span>' : '<span class="badge bg-secondary">No</span>' ?></td>
                        <td class="text-end">
                            <a href="banner_edit.php?id=<?= $b['id'] ?>" class="btn btn-sm btn-warning"><i class="bi bi-pencil"></i></a>
                            <a href="banner_delete.php?id=<?= $b['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this banner?')"><i class="bi bi-trash"></i></a>
                        </td>
                    </tr>
                <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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