<?php
/**
 * Admin layout top wrapper.
 * Requires: $adminPage, $adminPageTitle, $adminUserName
 */

$adminPage = $adminPage ?? 'dashboard';
$adminPageTitle = $adminPageTitle ?? 'Admin';
$adminUserName = $adminUserName ?? 'Admin';

if (!function_exists('adminIcon')) {
    function adminIcon(string $name): string {
        $icons = [
            'dashboard' => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="9"/><rect x="14" y="3" width="7" height="5"/><rect x="14" y="12" width="7" height="9"/><rect x="3" y="16" width="7" height="5"/></svg>',
            'orders'    => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z"/><line x1="3" y1="6" x2="21" y2="6"/><path d="M16 10a4 4 0 0 1-8 0"/></svg>',
            'products'  => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/></svg>',
            'customers' => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>',
            'messages'  => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',
            'gallery'   => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg>',
            'settings'  => '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 1 1-4 0v-.09a1.65 1.65 0 0 0-1-1.51 1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 1 1 0-4h.09a1.65 1.65 0 0 0 1.51-1 1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33h.01a1.65 1.65 0 0 0 1-1.51V3a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51h.01a1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82v.01a1.65 1.65 0 0 0 1.51 1H21a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',
            'logout'    => '<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/></svg>',
        ];
        return $icons[$name] ?? '';
    }
}

$initials = strtoupper(substr($adminUserName, 0, 1));
$navItems = [
    ['id' => 'dashboard', 'label' => 'Dashboard', 'href' => '/stile-lpg/admin/index.php'],
    ['id' => 'orders', 'label' => 'Orders', 'href' => '/stile-lpg/admin/orders.php'],
    ['id' => 'products', 'label' => 'Products', 'href' => '/stile-lpg/admin/products.php'],
    ['id' => 'customers', 'label' => 'Customers', 'href' => '/stile-lpg/admin/customers.php'],
    ['id' => 'messages', 'label' => 'Messages', 'href' => '/stile-lpg/admin/messages.php'],
    ['id' => 'gallery', 'label' => 'Gallery', 'href' => '/stile-lpg/admin/gallery.php'],
    ['id' => 'settings', 'label' => 'Settings', 'href' => '/stile-lpg/admin/site-settings.php'],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?= htmlspecialchars($adminPageTitle) ?> - Admin</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
<style>
*{box-sizing:border-box}body{margin:0;font-family:'Inter',sans-serif;background:#f8fafc;color:#0f172a}
a{text-decoration:none;color:inherit}
.admin-shell{display:flex;min-height:100vh}
.admin-sidebar{width:260px;background:linear-gradient(180deg,#0f172a,#1e293b);color:#cbd5e1;padding:1.2rem;position:sticky;top:0;height:100vh}
.brand{display:flex;align-items:center;gap:.6rem;color:#fff;font-weight:800;font-size:1.05rem;margin-bottom:1.2rem}
.nav-link{display:flex;align-items:center;gap:.65rem;padding:.75rem .85rem;border-radius:12px;color:#cbd5e1;font-weight:600;font-size:.9rem;margin-bottom:.35rem}
.nav-link.active{background:linear-gradient(135deg,#f97316,#ea580c);color:#fff}
.nav-link:hover{background:rgba(255,255,255,.08)}
.admin-main{flex:1;min-width:0}
.admin-top{display:flex;justify-content:space-between;align-items:center;padding:1rem 1.4rem;background:#fff;border-bottom:1px solid #e2e8f0;position:sticky;top:0;z-index:10}
.admin-content{padding:1.4rem}
.admin-user{display:flex;align-items:center;gap:.6rem}
.admin-avatar{width:34px;height:34px;border-radius:50%;display:flex;align-items:center;justify-content:center;background:#f97316;color:#fff;font-weight:800;font-size:.82rem}
.logout-link{display:inline-flex;align-items:center;gap:.4rem;font-size:.82rem;font-weight:700;color:#64748b}
@media(max-width:900px){.admin-sidebar{display:none}.admin-content{padding:1rem}.admin-top{padding:.9rem 1rem}}
</style>
</head>
<body>
<div class="admin-shell">
  <aside class="admin-sidebar">
    <a class="brand" href="/stile-lpg/admin/index.php" style="display:flex;align-items:center;justify-content:center;margin-bottom:2rem">
      <img src="/stile-lpg/backend/uploads/footer-image.png" alt="Stiles Energy Logo" style="width:120px;height:auto;object-fit:contain">
    </a>

    <nav>
      <?php foreach ($navItems as $item): ?>
        <a class="nav-link <?= $adminPage === $item['id'] ? 'active' : '' ?>" href="<?= $item['href'] ?>">
          <?= adminIcon($item['id']) ?>
          <span><?= htmlspecialchars($item['label']) ?></span>
        </a>
      <?php endforeach; ?>
    </nav>

    <div style="margin-top:1.1rem;padding-top:1.1rem;border-top:1px solid rgba(148,163,184,.25)">
      <a class="logout-link" href="/stile-lpg/logout.php"><?= adminIcon('logout') ?> Logout</a>
    </div>
  </aside>

  <main class="admin-main">
    <header class="admin-top">
      <h1 style="margin:0;font-size:1.05rem;font-weight:800"><?= htmlspecialchars($adminPageTitle) ?></h1>
      <div class="admin-user">
        <div class="admin-avatar"><?= htmlspecialchars($initials) ?></div>
        <div style="font-size:.85rem;font-weight:700;color:#334155"><?= htmlspecialchars($adminUserName) ?></div>
      </div>
    </header>

    <div class="admin-content">
