<?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 clients WHERE id = ?');
    $st->execute([$id]);
    $row = $st->fetch();
}
if (!$row) {
    admin_flash_set('danger', 'Not found.');
    header('Location: clients.php');
    exit;
}
$page = 'clients';
$pageTitle = 'Client details';
$pageSubtitle = (string)$row['name'];
$breadcrumbs = [['label' => 'Dashboard', 'href' => 'index.php'], ['label' => 'Clients', 'href' => 'clients.php'], ['label' => (string)$row['name'], 'href' => null]];
$headerActions = '<a href="client-edit.php?id=' . (int)$row['id'] . '" class="btn btn-primary btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a><a href="clients.php" class="btn btn-outline-secondary btn-sm">Back</a>';
require __DIR__ . '/includes/layout-header.php';
?>
<div class="row g-4">
  <div class="col-md-8">
    <div class="admin-card rounded">
      <div class="card-header">Details</div>
      <div class="card-body">
        <dl class="row admin-dl">
          <dt class="col-sm-3">Name</dt><dd class="col-sm-9 fw-semibold"><?= h((string)$row['name']) ?></dd>
          <dt class="col-sm-3">Website</dt><dd class="col-sm-9"><?php if (!empty($row['website_url'])): ?><a href="<?= h((string)$row['website_url']) ?>" target="_blank" rel="noopener"><?= h((string)$row['website_url']) ?></a><?php else: ?>—<?php endif; ?></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 small text-muted"><?= h((string)$row['created_at']) ?></dd>
        </dl>
      </div>
    </div>
  </div>
  <div class="col-md-4">
    <div class="admin-card rounded mb-3">
      <div class="card-header">Logo</div>
      <div class="card-body text-center"><?php if (!empty($row['logo_path'])): ?><img src="../<?= h((string)$row['logo_path']) ?>" class="img-fluid rounded" alt=""><?php else: ?><p class="text-muted mb-0">No logo</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="clients.php" onsubmit="return confirm('Delete?');">
          <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">Delete</button>
        </form>
      </div>
    </div>
  </div>
</div>
<?php require __DIR__ . '/includes/layout-footer.php'; ?>
