<?php
/**
 * Admin Layout Component — Modern Executive Palette
 * Included at top of all admin pages
 */
$pageTitle = $pageTitle ?? 'Administration';
$activeNav = $activeNav ?? 'dashboard';

// Fetch counts for sidebar badges
$db = getDB();
try {
    $newContacts    = $db->query("SELECT COUNT(*) FROM contact_messages WHERE status = 'nouveau'")->fetchColumn();
    $newEstimations = $db->query("SELECT COUNT(*) FROM estimation_requests WHERE status = 'nouveau'")->fetchColumn();
    $upcomingRdv    = $db->query("SELECT COUNT(*) FROM rdv_requests WHERE date_rdv >= CURDATE() AND status = 'confirme'")->fetchColumn();
    $totalProps     = $db->query("SELECT COUNT(*) FROM properties WHERE publication_status = 'publie'")->fetchColumn();
    $totalArticles  = $db->query("SELECT COUNT(*) FROM articles WHERE status = 'publie'")->fetchColumn();
} catch(Exception $e) {
    $newContacts = $newEstimations = $upcomingRdv = $totalProps = $totalArticles = 0;
}

$navItems = [
    'dashboard'     => ['label'=>'Tableau de bord',    'icon'=>'grid',       'href'=> SITE_URL . '/admin/index.php'],
    'properties'    => ['label'=>'Propriétés',          'icon'=>'home',       'href'=> SITE_URL . '/admin/properties.php',    'badge' => $totalProps, 'badgeColor' => 'bg-emerald-500/20 text-emerald-300 border-emerald-500/30'],
    'articles'      => ['label'=>'Journal & Articles',  'icon'=>'newspaper',  'href'=> SITE_URL . '/admin/articles.php',      'badge' => $totalArticles, 'badgeColor' => 'bg-amber-500/20 text-amber-300 border-amber-500/30'],
    'page_contents' => ['label'=>'Contenu des pages',  'icon'=>'pencil',     'href'=> SITE_URL . '/admin/page_contents.php'],
    'taxonomies'    => ['label'=>'Zones & Types',       'icon'=>'tag',        'href'=> SITE_URL . '/admin/taxonomies.php'],
    'contacts'      => ['label'=>'Messages contact',    'icon'=>'mail',       'href'=> SITE_URL . '/admin/contacts.php',      'badge' => $newContacts,  'badgeColor' => 'bg-blue-500 text-white'],
    'estimations'   => ['label'=>'Estimations',         'icon'=>'calculator', 'href'=> SITE_URL . '/admin/estimations.php',   'badge' => $newEstimations, 'badgeColor' => 'bg-amber-500 text-black font-bold'],
    'rdv'           => ['label'=>'Rendez-vous',         'icon'=>'calendar',   'href'=> SITE_URL . '/admin/rdv.php',           'badge' => $upcomingRdv,  'badgeColor' => 'bg-purple-500 text-white'],
    'settings'      => ['label'=>'Paramètres',          'icon'=>'settings',   'href'=> SITE_URL . '/admin/settings.php'],
];

$icons = [
    'grid'       => 'M3.75 6A2.25 2.25 0 016 3.75h2.25A2.25 2.25 0 0110.5 6v2.25a2.25 2.25 0 01-2.25 2.25H6a2.25 2.25 0 01-2.25-2.25V6zm0 9.75A2.25 2.25 0 016 13.5h2.25a2.25 2.25 0 012.25 2.25V18a2.25 2.25 0 01-2.25 2.25H6A2.25 2.25 0 013.75 18v-2.25zm9.75-9.75A2.25 2.25 0 0115.75 3.75H18A2.25 2.25 0 0120.25 6v2.25A2.25 2.25 0 0118 10.5h-2.25a2.25 2.25 0 01-2.25-2.25V6zm0 9.75a2.25 2.25 0 012.25-2.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-2.25A2.25 2.25 0 0113.5 18v-2.25z',
    'home'       => 'M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25',
    'newspaper'  => 'M12 7.5h1.5m-1.5 3h1.5m-7.5 3h7.5m-7.5 3h7.5m3-9h3.375c.621 0 1.125.504 1.125 1.125V18a2.25 2.25 0 01-2.25 2.25M16.5 7.5V18a2.25 2.25 0 002.25 2.25M16.5 7.5V4.875c0-.621-.504-1.125-1.125-1.125H4.125C3.504 3.75 3 4.254 3 4.875V18a2.25 2.25 0 002.25 2.25h13.5M6 7.5h3v3H6v-3z',
    'pencil'     => 'M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10',
    'tag'        => 'M9.568 3H5.25A2.25 2.25 0 003 5.25v4.318c0 .597.237 1.17.659 1.591l9.581 9.581c.699.699 1.78.872 2.607.33a18.095 18.095 0 005.223-5.223c.542-.827.369-1.908-.33-2.607L11.16 3.66A2.25 2.25 0 009.568 3z M6 6h.008v.008H6V6z',
    'mail'       => 'M21.75 6.75v10.5a2.25 2.25 0 01-2.25 2.25h-15a2.25 2.25 0 01-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0019.5 4.5h-15a2.25 2.25 0 00-2.25 2.25m19.5 0v.243a2.25 2.25 0 01-1.07 1.916l-7.5 4.615a2.25 2.25 0 01-2.36 0L3.32 8.91a2.25 2.25 0 01-1.07-1.916V6.75',
    'calculator' => 'M15.75 15.75V18m-7.5-6.75h.008v.008H8.25v-.008zm0 2.25h.008v.008H8.25V13.5zm0 2.25h.008v.008H8.25v-.008zm0 2.25h.008v.008H8.25V18zm2.498-6.75h.007v.008h-.007v-.008zm0 2.25h.007v.008h-.007V13.5zm0 2.25h.007v.008h-.007v-.008zm0 2.25h.007v.008h-.007V18zm2.504-6.75h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V13.5zm0 2.25h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V18zm2.498-6.75h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V13.5zM8.25 6h7.5v2.25h-7.5V6zM12 2.25c-1.892 0-3.758.11-5.593.322C5.307 2.7 4.5 3.616 4.5 4.709v.776M12 2.25c1.892 0 3.758.11 5.593.322 1.1.128 1.907 1.044 1.907 2.137v.776M12 2.25v18.75m0-18.75l-3.75 3.75M12 2.25l3.75 3.75',
    'calendar'   => 'M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 9v7.5m-9-6h.008v.008H12V12zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM12 15h.008v.008H12V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM9.75 15h.008v.008H9.75V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM7.5 15h.008v.008H7.5V15zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM12 18h.008v.008H12V18zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM9.75 18h.008v.008H9.75V18zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0zM7.5 18h.008v.008H7.5V18zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z',
    'settings'   => 'M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.324.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 011.37.49l1.296 2.247a1.125 1.125 0 01-.26 1.431l-1.003.827c-.293.24-.438.613-.431.992a6.759 6.759 0 010 .255c-.007.378.138.75.43.99l1.005.828c.424.35.534.954.26 1.43l-1.298 2.247a1.125 1.125 0 01-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.57 6.57 0 01-.22.128c-.331.183-.581.495-.644.869l-.213 1.28c-.09.543-.56.941-1.11.941h-2.594c-.55 0-1.02-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 01-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 01-1.369-.49l-1.297-2.247a1.125 1.125 0 01.26-1.431l1.004-.827c.292-.24.437-.613.43-.992a6.932 6.932 0 010-.255c.007-.378-.138-.75-.43-.99l-1.004-.828a1.125 1.125 0 01-.26-1.43l1.297-2.247a1.125 1.125 0 011.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.087.22-.128.332-.183.582-.495.644-.869l.214-1.281z M15 12a3 3 0 11-6 0 3 3 0 016 0z',
    'logout'     => 'M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15m3 0l3-3m0 0l-3-3m3 3H9',
];
?>
<!DOCTYPE html>
<html lang="fr" class="h-full bg-slate-900">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title><?= htmlspecialchars($pageTitle) ?> | Console Administration AJOUAMAI IMMO-BUSINESS</title>
  
  <!-- Favicon Logo -->
  <link rel="icon" type="image/png" href="<?= SITE_URL ?>/assets/img/logo.png"/>
  <link rel="apple-touch-icon" href="<?= SITE_URL ?>/assets/img/logo.png"/>

  <!-- Google Fonts: Sansation (titles) + McLaren (body) -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Sansation:ital,wght@0,300;0,400;0,700;1,300;1,400&family=McLaren&display=swap" rel="stylesheet">
  <script src="https://cdn.tailwindcss.com"></script>
  <script>
    tailwind.config = {
      theme: {
        extend: {
          fontFamily: {
            sans: ['McLaren', 'cursive', 'sans-serif'],
            serif: ['Sansation', 'sans-serif'],
          },
          colors: {
            brand: {
              50: '#FBF8F1',
              100: '#F5EFE0',
              500: '#D4C7A3',
              600: '#B8A882',
              700: '#8E7E5A',
              900: '#3D3524',
            }
          }
        }
      }
    }
  </script>
  <link href="https://cdn.jsdelivr.net/npm/quill@2.0.3/dist/quill.snow.css" rel="stylesheet"/>
  
  <style>
    .admin-glass-card {
      background: rgba(255, 255, 255, 0.98);
      backdrop-filter: blur(12px);
      border: 1px solid rgba(226, 232, 240, 0.8);
      box-shadow: 0 4px 20px -2px rgba(0, 0, 0, 0.05);
    }
    .admin-nav-item {
      display: flex;
      items-center: center;
      gap: 0.875rem;
      padding: 0.75rem 1rem;
      font-size: 0.8125rem;
      font-weight: 500;
      color: #94A3B8;
      border-radius: 0.625rem;
      transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .admin-nav-item:hover {
      background: rgba(255, 255, 255, 0.08);
      color: #F8FAFC;
      transform: translateX(3px);
    }
    .admin-nav-item.active {
      background: linear-gradient(135deg, #B8A882 0%, #8E7E5A 100%);
      color: #FFFFFF;
      box-shadow: 0 4px 15px -3px rgba(184, 168, 130, 0.4);
      font-weight: 600;
    }
    .admin-table th {
      padding: 1rem 1.25rem;
      text-align: left;
      font-size: 0.6875rem;
      letter-spacing: 0.15em;
      text-transform: uppercase;
      color: #64748B;
      font-weight: 700;
      background: #F8FAFC;
    }
    .admin-table td {
      padding: 1rem 1.25rem;
      font-size: 0.8125rem;
      color: #1E293B;
    }
    .admin-table tr {
      border-bottom: 1px solid #F1F5F9;
      transition: background-color 0.15s ease;
    }
    .admin-table tr:hover {
      background-color: #F8FAFC;
    }
  </style>
</head>
<body class="h-full bg-slate-100 font-sans antialiased text-slate-800">

<!-- SIDEBAR NAVIGATION DRAWER -->
<aside class="fixed inset-y-0 left-0 w-72 bg-gradient-to-b from-slate-950 via-slate-900 to-indigo-950 text-white flex flex-col z-30 shadow-2xl border-r border-slate-800/60">
  
  <!-- Brand Header -->
  <div class="h-24 px-6 flex items-center justify-between border-b border-slate-800/80 bg-slate-950/60 backdrop-blur-md">
    <a href="<?= SITE_URL ?>/admin/index.php" class="flex items-center gap-3.5 group">
      <div class="p-2 bg-gradient-to-tr from-brand-600 to-brand-500 rounded-xl shadow-lg shadow-brand-600/30 group-hover:scale-105 transition-transform">
        <img src="<?= SITE_URL ?>/assets/img/logo.png" class="h-7 w-auto object-contain filter brightness-0 invert" alt="AJOUAMAI IMMO-BUSINESS Logo"/>
      </div>
      <div>
        <span class="text-sm font-bold tracking-wider uppercase font-sans text-white block">AJOUAMAI IMMO-BUSINESS Console</span>
        <span class="text-[10px] tracking-widest text-brand-500 uppercase font-medium block">Excellence Pro</span>
      </div>
    </a>
  </div>

  <!-- Main Navigation Links -->
  <nav class="flex-1 px-4 py-6 space-y-1.5 overflow-y-auto custom-scrollbar">
    <div class="px-3 pb-2 text-[10px] tracking-[0.2em] uppercase font-bold text-slate-500">Navigation Principale</div>
    
    <?php foreach ($navItems as $key => $item): ?>
    <a href="<?= $item['href'] ?>" class="admin-nav-item <?= $activeNav === $key ? 'active' : '' ?>">
      <svg class="w-4 h-4 flex-shrink-0 <?= $activeNav === $key ? 'text-white' : 'text-slate-400' ?>" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.75">
        <path stroke-linecap="round" stroke-linejoin="round" d="<?= $icons[$item['icon']] ?>"/>
      </svg>
      <span class="flex-1"><?= $item['label'] ?></span>
      <?php if (!empty($item['badge']) && $item['badge'] > 0): ?>
        <span class="px-2 py-0.5 text-[10px] rounded-full font-bold shadow-xs <?= $item['badgeColor'] ?? 'bg-brand-600 text-white' ?>">
          <?= $item['badge'] ?>
        </span>
      <?php endif; ?>
    </a>
    <?php endforeach; ?>
  </nav>

  <!-- User / Action Footer -->
  <div class="p-4 border-t border-slate-800/80 bg-slate-950/80 space-y-2">
    <a href="<?= SITE_URL ?>/" target="_blank" class="flex items-center justify-between p-3 rounded-xl bg-slate-900/90 text-slate-300 hover:text-white hover:bg-slate-800 transition-all border border-slate-800 text-xs font-medium group">
      <span class="flex items-center gap-2.5">
        <svg class="w-4 h-4 text-brand-500 group-hover:scale-110 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.75"><path stroke-linecap="round" stroke-linejoin="round" d="M13.5 6H5.25A2.25 2.25 0 003 8.25v10.5A2.25 2.25 0 005.25 21h10.5A2.25 2.25 0 0018 18.75V10.5m-10.5 6L21 3m0 0h-5.25M21 3v5.25"/></svg>
        Voir le site public
      </span>
      <span class="text-[10px] uppercase tracking-wider text-slate-500 font-sans">↗</span>
    </a>

    <a href="<?= SITE_URL ?>/admin/index.php?logout=1" class="flex items-center gap-2.5 p-3 rounded-xl text-rose-400 hover:bg-rose-950/40 hover:text-rose-300 transition-colors text-xs font-medium">
      <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.75"><path stroke-linecap="round" stroke-linejoin="round" d="<?= $icons['logout'] ?>"/></svg>
      <span>Déconnexion</span>
    </a>
  </div>

</aside>

<!-- MAIN WRAPPER -->
<div class="ml-72 min-h-screen flex flex-col bg-slate-100">

  <!-- TOP HEADER BAR -->
  <header class="h-20 bg-white/90 backdrop-blur-md border-b border-slate-200/80 px-8 flex items-center justify-between sticky top-0 z-20 shadow-xs">
    <div>
      <h1 class="font-serif text-2xl font-semibold text-slate-900"><?= htmlspecialchars($pageTitle) ?></h1>
      <p class="text-xs text-slate-500 font-sans mt-0.5">Plateforme de Gestion immobilière AJOUAMAI IMMO-BUSINESS</p>
    </div>

    <div class="flex items-center gap-4">
      <div class="flex items-center gap-3 bg-slate-50 border border-slate-200/80 rounded-full px-4 py-1.5 shadow-2xs">
        <div class="w-2.5 h-2.5 rounded-full bg-emerald-500 animate-pulse"></div>
        <span class="text-xs font-semibold text-slate-700 font-sans">Console Active</span>
      </div>

      <div class="h-8 w-px bg-slate-200"></div>

      <div class="flex items-center gap-3">
        <div class="w-9 h-9 rounded-full bg-gradient-to-tr from-brand-600 to-brand-500 text-white font-bold flex items-center justify-center text-xs shadow-md shadow-brand-600/30 font-sans">
          HA
        </div>
        <div class="hidden sm:block">
          <p class="text-xs font-bold text-slate-900 font-sans leading-tight">Hind Ajouamai</p>
          <p class="text-[10px] text-brand-700 font-medium font-sans">Super Admin</p>
        </div>
      </div>
    </div>
  </header>

  <!-- FLASH MESSAGES -->
  <?php if ($flash = getFlash()): ?>
  <div class="mx-8 mt-6 p-4 rounded-xl shadow-xs flex items-center gap-3 border <?= $flash['type'] === 'success' ? 'border-emerald-200 bg-emerald-50 text-emerald-800' : 'border-rose-200 bg-rose-50 text-rose-800' ?>">
    <span class="text-lg"><?= $flash['type'] === 'success' ? '✓' : '⚠️' ?></span>
    <p class="text-xs font-semibold font-sans"><?= htmlspecialchars($flash['message']) ?></p>
  </div>
  <?php endif; ?>

  <!-- PAGE CONTENT CONTAINER -->
  <main class="p-8 flex-1">
