<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/flash.php';
require_admin();
$id = (int)($_GET['id'] ?? 0);
$row = null;
if ($id > 0) {
    $st = db()->prepare('SELECT * FROM projects WHERE id = ?');
    $st->execute([$id]);
    $row = $st->fetch();
}
if (!$row) {
    admin_flash_set('danger', 'Project not found.');
    header('Location: projects.php');
    exit;
}
$page = 'projects';
$pageTitle = 'Project details';
$pageSubtitle = (string)$row['title'];
$breadcrumbs = [
    ['label' => 'Dashboard', 'href' => 'index.php'],
    ['label' => 'Projects', 'href' => 'projects.php'],
    ['label' => (string)$row['title'], 'href' => null],
];
$headerActions = '<a href="../project-detail.php?slug=' . rawurlencode((string)$row['slug']) . '" class="btn btn-outline-primary btn-sm" target="_blank" rel="noopener"><i class="bi bi-box-arrow-up-right me-1"></i>View on site</a>'
    . '<a href="project-edit.php?id=' . (int)$row['id'] . '" class="btn btn-primary btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a>'
    . '<a href="projects.php" class="btn btn-outline-secondary btn-sm">Back</a>';
require __DIR__ . '/includes/layout-header.php';
?>
<div class="row g-4">
  <div class="col-lg-8">
    <div class="admin-card rounded mb-4">
      <div class="card-header">Description</div>
      <div class="card-body"><?php $d = trim((string)$row['description']); echo $d !== '' ? nl2br(h($d)) : '<span class="text-muted">No description.</span>'; ?></div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="admin-card rounded mb-4">
      <div class="card-header">Meta</div>
      <div class="card-body">
        <dl class="admin-dl mb-0">
          <dt>Slug</dt>
          <dd><code><?= h((string)$row['slug']) ?></code></dd>
          <dt>Category</dt>
          <dd><?= h((string)($row['category'] ?? '—')) ?></dd>
          <dt>Sort order</dt>
          <dd><?= (int)$row['sort_order'] ?></dd>
          <dt>Created</dt>
          <dd class="small text-muted"><?= h((string)$row['created_at']) ?></dd>
        </dl>
      </div>
    </div>
    <div class="admin-card rounded mb-4">
      <div class="card-header">Image</div>
      <div class="card-body text-center">
        <?php if (!empty($row['image_path'])): ?>
          <img src="../<?= h((string)$row['image_path']) ?>" class="img-fluid rounded shadow-sm" alt="">
        <?php else: ?>
          <p class="text-muted mb-0">No image.</p>
        <?php endif; ?>
      </div>
    </div>
    <div class="admin-card rounded">
      <div class="card-header">Danger zone</div>
      <div class="card-body">
        <form method="post" action="projects.php" onsubmit="return confirm('Delete this project?');">
          <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
          <input type="hidden" name="delete_id" value="<?= (int)$row['id'] ?>">
          <button type="submit" class="btn btn-outline-danger btn-sm w-100"><i class="bi bi-trash me-1"></i>Delete</button>
        </form>
      </div>
    </div>
  </div>
</div>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
