<?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 services WHERE id = ?');
    $st->execute([$id]);
    $row = $st->fetch();
}
if (!$row) {
    admin_flash_set('danger', 'Service not found.');
    header('Location: services.php');
    exit;
}

$page = 'services';
$pageTitle = 'Service details';
$pageSubtitle = 'Read-only summary. Use Edit to change content.';
$breadcrumbs = [
    ['label' => 'Dashboard', 'href' => 'index.php'],
    ['label' => 'Services', 'href' => 'services.php'],
    ['label' => (string)$row['title'], 'href' => null],
];
$publicUrl = '../service-detail.php?slug=' . rawurlencode((string)$row['slug']);
$headerActions = '<a href="' . h($publicUrl) . '" 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="service-edit.php?id=' . (int)$row['id'] . '" class="btn btn-primary btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a>'
    . '<a href="services.php" class="btn btn-outline-secondary btn-sm">Back to list</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">Content</div>
      <div class="card-body">
        <dl class="row admin-dl mb-0">
          <dt class="col-sm-3">Title</dt>
          <dd class="col-sm-9"><?= h((string)$row['title']) ?></dd>
          <dt class="col-sm-3">URL slug</dt>
          <dd class="col-sm-9"><code><?= h((string)$row['slug']) ?></code></dd>
          <dt class="col-sm-3">Icon class</dt>
          <dd class="col-sm-9"><code><?= h((string)$row['icon_class']) ?></code></dd>
          <dt class="col-sm-3">Sort order</dt>
          <dd class="col-sm-9"><?= (int)$row['sort_order'] ?></dd>
          <dt class="col-sm-3">Created</dt>
          <dd class="col-sm-9 text-muted small"><?= h((string)$row['created_at']) ?></dd>
          <dt class="col-sm-3">Short description</dt>
          <dd class="col-sm-9"><?= nl2br(h((string)$row['short_description'])) ?></dd>
          <dt class="col-sm-3">Full description</dt>
          <dd class="col-sm-9"><?= nl2br(h((string)$row['full_description'])) ?></dd>
        </dl>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="admin-card rounded mb-4">
      <div class="card-header">Featured 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="">
          <p class="small text-muted mt-2 mb-0"><?= h((string)$row['image_path']) ?></p>
        <?php else: ?>
          <p class="text-muted mb-0">No image uploaded.</p>
        <?php endif; ?>
      </div>
    </div>
    <div class="admin-card rounded">
      <div class="card-header">Danger zone</div>
      <div class="card-body">
        <p class="small text-muted">Deleting removes this service from the database and the public site.</p>
        <form method="post" action="services.php" onsubmit="return confirm('Delete this service permanently?');">
          <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 service</button>
        </form>
      </div>
    </div>
  </div>
</div>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
