<?php
/**
 * Expects: $pageTitle (string). Calling script must require_admin() first.
 */
$pageTitle = $pageTitle ?? 'Admin';
$user = admin_user();
$navItems = [
    ['index.php', 'grid', 'Dashboard'],
    ['properties.php', 'building', 'Properties'],
    ['developments.php', 'house-build', 'New Developments'],
    ['districts.php', 'map-pin', 'Districts'],
    ['categories.php', 'grid', 'Categories'],
    ['agents.php', 'user', 'Agents'],
    ['blog.php', 'document', 'Blog & News'],
    ['content-blocks.php', 'layers', 'Content Blocks'],
    ['site-content.php', 'document', 'Site Content'],
    ['testimonials.php', 'quote', 'Testimonials'],
    ['faqs.php', 'check', 'FAQs'],
    ['procurement.php', 'document', 'Procurement'],
    ['leads.php', 'mail', 'Leads Inbox'],
    ['vip-alerts.php', 'star', 'VIP Alerts'],
    ['newsletter.php', 'mail', 'Newsletter'],
];
$adminOnlyNavItems = [
    ['settings.php', 'shield', 'Site Settings'],
    ['users.php', 'user', 'Admin Users'],
];
$currentScript = basename($_SERVER['SCRIPT_NAME']);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= e($pageTitle) ?> — Admin | Kigali Yacu Properties</title>
<meta name="robots" content="noindex, nofollow">
<link rel="icon" href="/assets/icons/favicon.svg" type="image/svg+xml">
<link rel="stylesheet" href="/assets/css/fonts.css">
<link rel="stylesheet" href="/assets/css/variables.css">
<link rel="stylesheet" href="/assets/css/base.css">
<link rel="stylesheet" href="/assets/css/components.css">
<link rel="stylesheet" href="/assets/css/utilities.css">
<link rel="stylesheet" href="/assets/css/animations.css">
<style>
  body { display: flex; min-height: 100vh; background: var(--color-cream); }
  .admin-sidebar { width: 260px; background: var(--color-black); color: #fff; flex-shrink: 0; padding: var(--space-6) var(--space-5); position: sticky; top: 0; height: 100vh; overflow-y: auto; }
  .admin-sidebar a { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3) var(--space-3); border-radius: var(--radius-sm); font-size: var(--fs-sm); color: var(--color-fog); }
  .admin-sidebar a:hover, .admin-sidebar a.active { background: rgba(255,255,255,0.08); color: #fff; }
  .admin-main { flex: 1; padding: var(--space-7); min-width: 0; }
  .admin-topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--space-7); }
  .admin-table { width: 100%; border-collapse: collapse; background: #fff; border-radius: var(--radius-md); overflow: hidden; }
  .admin-table th, .admin-table td { padding: var(--space-4); border-bottom: 1px solid var(--color-border); text-align: left; font-size: var(--fs-sm); }
  .admin-table th { background: var(--color-cream); text-transform: uppercase; font-size: var(--fs-xs); letter-spacing: var(--ls-wide); color: var(--color-text-muted); }
  .admin-card { background: #fff; border-radius: var(--radius-md); padding: var(--space-6); box-shadow: var(--shadow-sm); }
  @media (max-width: 900px) { body { flex-direction: column; } .admin-sidebar { width: 100%; height: auto; position: static; } }
</style>
</head>
<body>
<aside class="admin-sidebar">
  <div class="brand" style="margin-bottom:var(--space-8);">
    <span class="brand-mark">KY</span>
    <span class="brand-name" style="color:#fff;">Admin<small style="color:var(--color-accent-light);">Kigali Yacu</small></span>
  </div>
  <nav class="flex flex-col gap-1">
    <?php foreach ($navItems as $item): ?>
      <a href="/admin/<?= $item[0] ?>" class="<?= $currentScript === $item[0] ? 'active' : '' ?>"><?= icon($item[1], 16) ?> <?= e($item[2]) ?></a>
    <?php endforeach; ?>
    <?php if (($user['role'] ?? '') === 'admin'): ?>
      <hr class="rule" style="border-color:rgba(255,255,255,0.1);">
      <?php foreach ($adminOnlyNavItems as $item): ?>
        <a href="/admin/<?= $item[0] ?>" class="<?= $currentScript === $item[0] ? 'active' : '' ?>"><?= icon($item[1], 16) ?> <?= e($item[2]) ?></a>
      <?php endforeach; ?>
    <?php endif; ?>
    <hr class="rule" style="border-color:rgba(255,255,255,0.1);">
    <a href="/" target="_blank"><?= icon('arrow-right', 16) ?> View Site</a>
    <a href="/admin/logout.php"><?= icon('lock', 16) ?> Log Out</a>
  </nav>
</aside>
<main class="admin-main">
  <div class="admin-topbar">
    <h1 style="font-size:var(--fs-xl);margin:0;"><?= e($pageTitle) ?></h1>
    <div class="flex items-center gap-3">
      <?= avatar_html($user['name'] ?? 'Admin', 'sm') ?>
      <span class="text-sm fw-semibold"><?= e($user['name'] ?? '') ?></span>
      <span class="badge badge-outline"><?= e(ucfirst($user['role'] ?? '')) ?></span>
    </div>
  </div>
