<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_admin();
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id']) && csrf_verify($_POST['csrf'] ?? null)) {
    db()->prepare('DELETE FROM banners WHERE id = ?')->execute([(int)$_POST['delete_id']]);
    admin_flash_set('success', 'Banner slide removed.');
    header('Location: banners.php');
    exit;
}
$rows = db()->query('SELECT * FROM banners ORDER BY sort_order ASC, id ASC')->fetchAll();
$page = 'banners';
$pageTitle = 'Hero banners';
$pageSubtitle = 'Slides in the homepage hero carousel (title, subtitle, image or video).';
$breadcrumbs = [['label' => 'Dashboard', 'href' => 'index.php'], ['label' => 'Hero banners', 'href' => null]];
$headerActions = '<a href="banner-edit.php" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Add slide</a>';
require __DIR__ . '/includes/layout-header.php';
?>
<div class="admin-card rounded">
  <div class="card-header py-3 d-flex justify-content-between align-items-center">
    <span>All slides (<?= count($rows) ?>)</span>
  </div>
  <div class="table-responsive">
    <table class="table table-hover admin-table mb-0">
      <thead>
        <tr>
          <th>ID</th>
          <th>Preview</th>
          <th>Title</th>
          <th>Type</th>
          <th>Active</th>
          <th>Order</th>
          <th>Created</th>
          <th class="text-end">Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($rows as $r): ?>
          <tr>
            <td class="text-muted"><?= (int)$r['id'] ?></td>
            <td>
              <?php if (!empty($r['is_video']) && !empty($r['video_path'])): ?>
                <span class="badge bg-info">Video</span>
              <?php elseif (!empty($r['image_path'])): ?>
                <img src="../<?= h((string)$r['image_path']) ?>" class="admin-thumb" alt="">
              <?php else: ?>—<?php endif; ?>
            </td>
            <td class="fw-semibold"><?= h((string)$r['title']) ?></td>
            <td><?= !empty($r['is_video']) ? 'MP4' : 'Image' ?></td>
            <td><?= !empty($r['is_active']) ? '<span class="badge bg-success">Yes</span>' : '<span class="badge bg-secondary">No</span>' ?></td>
            <td><?= (int)$r['sort_order'] ?></td>
            <td class="small text-muted"><?= h((string)$r['created_at']) ?></td>
            <td class="text-end text-nowrap">
              <a href="banner-edit.php?id=<?= (int)$r['id'] ?>" class="btn btn-sm btn-outline-primary">Edit</a>
              <form method="post" class="d-inline" onsubmit="return confirm('Delete this slide?');">
                <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
                <input type="hidden" name="delete_id" value="<?= (int)$r['id'] ?>">
                <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
              </form>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <?php if (!$rows): ?><div class="card-body text-center text-muted py-5">No banners. <a href="banner-edit.php">Create the first slide</a>.</div><?php endif; ?>
</div>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
