<?php
/**
 * Shared layout for every super-admin (control-plane) screen. Deliberately
 * a separate layout from layouts/main.php (tenant back-office): a sidebar
 * rather than a top navbar, fixed indigo branding (no tenant to read
 * primary_color from — admin/* never resolves a tenant, see Filters.php),
 * and a completely different nav tree (hotels/plans/billing, not
 * rooms/bookings/folios).
 */
$activeNav = $activeNav ?? '';
$navItems  = [
    'dashboard'    => ['label' => 'Dashboard', 'icon' => 'bi-speedometer2', 'url' => 'admin'],
    'tenants'      => ['label' => 'Hotels', 'icon' => 'bi-buildings', 'url' => 'admin/tenants'],
    'plans'        => ['label' => 'Plans & Pricing', 'icon' => 'bi-tags', 'url' => 'admin/plans'],
    'invoices'     => ['label' => 'Billing & Invoices', 'icon' => 'bi-receipt', 'url' => 'admin/invoices'],
    'transactions' => ['label' => 'Transactions', 'icon' => 'bi-credit-card', 'url' => 'admin/transactions'],
    'reports'      => ['label' => 'Reports', 'icon' => 'bi-graph-up', 'url' => 'admin/reports'],
    'admins'       => ['label' => 'Admins & Users', 'icon' => 'bi-person-badge', 'url' => 'admin/admins'],
    'activity-log' => ['label' => 'Activity Log', 'icon' => 'bi-clock-history', 'url' => 'admin/activity-log'],
    'system'       => ['label' => 'System Health', 'icon' => 'bi-heart-pulse', 'url' => 'admin/system-health'],
    'settings'     => ['label' => 'Settings', 'icon' => 'bi-gear', 'url' => 'admin/settings'],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= esc($pageTitle ?? 'Dashboard') ?> · MaxYields Platform</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root { --brand: #4f46e5; --brand-dark: #3730a3; }
        * { font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
        body { background-color: #f4f6f8; }
        .admin-shell { min-height: 100vh; }
        .admin-sidebar {
            width: 250px; flex-shrink: 0; background: #16182b; color: #fff;
            min-height: 100vh; position: sticky; top: 0; align-self: flex-start;
        }
        .admin-sidebar .brand { padding: 1.25rem 1.25rem 1rem; display: flex; align-items: center; gap: 0.6rem; }
        .admin-sidebar .brand-mark { width: 36px; height: 36px; border-radius: 9px; background: var(--brand); display: flex; align-items: center; justify-content: center; font-size: 1.1rem; }
        .admin-sidebar .brand-text { font-weight: 800; font-size: 1.05rem; letter-spacing: -0.01em; }
        .admin-sidebar .brand-sub { font-size: 0.7rem; color: rgba(255,255,255,0.5); }
        .admin-nav { padding: 0.5rem 0.75rem; }
        .admin-nav .nav-link { color: rgba(255,255,255,0.72); border-radius: 8px; padding: 0.55rem 0.75rem; font-size: 0.88rem; font-weight: 500; display: flex; align-items: center; gap: 0.65rem; margin-bottom: 0.15rem; }
        .admin-nav .nav-link i { font-size: 1rem; width: 1.1rem; text-align: center; }
        .admin-nav .nav-link:hover { color: #fff; background: rgba(255,255,255,0.06); }
        .admin-nav .nav-link.active { color: #fff; background: var(--brand); }
        .admin-topbar { background: #fff; border-bottom: 1px solid #eef0f3; padding: 0.85rem 1.5rem; display: flex; align-items: center; justify-content: space-between; }
        .admin-main { flex: 1; min-width: 0; }
        .card { border: none; border-radius: 12px; box-shadow: 0 1px 2px rgba(16,24,40,0.04), 0 1px 3px rgba(16,24,40,0.06); }
        .card-header { background-color: #fff; border-bottom: 1px solid #eef0f3; font-weight: 600; }
        .btn { border-radius: 8px; font-weight: 500; }
        .btn-primary {
            --bs-btn-bg: var(--brand); --bs-btn-border-color: var(--brand);
            --bs-btn-hover-bg: var(--brand-dark); --bs-btn-hover-border-color: var(--brand-dark);
            --bs-btn-active-bg: var(--brand-dark); --bs-btn-active-border-color: var(--brand-dark);
        }
        .btn-outline-primary {
            --bs-btn-color: var(--brand); --bs-btn-border-color: var(--brand);
            --bs-btn-hover-bg: var(--brand); --bs-btn-hover-border-color: var(--brand);
            --bs-btn-active-bg: var(--brand); --bs-btn-active-border-color: var(--brand);
        }
        .form-control:focus, .form-select:focus { border-color: var(--brand); box-shadow: 0 0 0 0.2rem color-mix(in srgb, var(--brand) 18%, transparent); }
        .table thead th { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: #6b7280; background: #f9fafb; font-weight: 700; border-bottom-width: 1px; }
        .badge { font-weight: 600; }
        h1, h2, h3, .h1, .h2, .h3 { letter-spacing: -0.01em; }
        .stat-card { border-radius: 12px; padding: 1.1rem 1.25rem; background: #fff; }
        .stat-card .stat-icon { width: 42px; height: 42px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.15rem; }
        .stat-card .stat-value { font-size: 1.6rem; font-weight: 800; letter-spacing: -0.02em; }
        .stat-card .stat-label { font-size: 0.8rem; color: #6b7280; }
        @media (max-width: 991.98px) { .admin-sidebar { display: none; } }
        <?= $this->renderSection('styles') ?>
    </style>
</head>
<body>
<div class="admin-shell d-flex">
    <aside class="admin-sidebar d-flex flex-column">
        <div class="brand">
            <div class="brand-mark"><i class="bi bi-hexagon-fill"></i></div>
            <div>
                <div class="brand-text">MaxYields</div>
                <div class="brand-sub">Platform Console</div>
            </div>
        </div>
        <nav class="admin-nav flex-grow-1">
            <?php foreach ($navItems as $key => $item): ?>
                <a class="nav-link <?= $activeNav === $key ? 'active' : '' ?>" href="<?= site_url($item['url']) ?>">
                    <i class="bi <?= $item['icon'] ?>"></i> <?= esc($item['label']) ?>
                </a>
            <?php endforeach; ?>
        </nav>
        <div class="p-3 border-top border-white border-opacity-10">
            <div class="text-white small fw-semibold"><?= esc(session('adminUserName')) ?></div>
            <div class="text-white-50" style="font-size: 0.75rem;"><?= esc(session('adminUserEmail')) ?></div>
            <a class="btn btn-outline-light btn-sm w-100 mt-2" href="<?= site_url('admin/logout') ?>">
                <i class="bi bi-box-arrow-right"></i> Log out
            </a>
        </div>
    </aside>

    <div class="admin-main">
        <div class="admin-topbar">
            <h1 class="h5 mb-0 fw-bold"><?= esc($pageTitle ?? 'Dashboard') ?></h1>
            <span class="badge bg-light text-dark border"><i class="bi bi-shield-check me-1"></i>Super Admin</span>
        </div>

        <div class="container-fluid py-4 px-4">
            <?php if (session()->getFlashdata('success')): ?>
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <?= esc(session()->getFlashdata('success')) ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>
            <?php if (session()->getFlashdata('error')): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <?= esc(session()->getFlashdata('error')) ?>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>
            <?php if (isset($validation) && $validation instanceof \CodeIgniter\Validation\ValidationInterface && $validation->getErrors()): ?>
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <ul class="mb-0">
                        <?php foreach ($validation->getErrors() as $error): ?>
                            <li><?= esc($error) ?></li>
                        <?php endforeach; ?>
                    </ul>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            <?php endif; ?>

            <?= $this->renderSection('content') ?>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<?= $this->renderSection('scripts') ?>
</body>
</html>
