<?php
/**
 * Sidebar partial — included by all protected admin pages.
 * $active_nav must be set by the including page.
 */
$active_nav = $active_nav ?? '';
$saas_admin_user = $saas_admin_user ?? $_SESSION['saas_admin'] ?? [];
?>
<aside class="sidebar" id="adminSidebar">
    <div class="sidebar-brand">
        <div class="sidebar-brand-icon">E</div>
        <div style="flex:1">
            <h1>EdumacFlo</h1>
            <span>SaaS Admin Panel</span>
        </div>
        <button type="button" class="mobile-nav-toggle" onclick="toggleSidebar()" style="padding:2px 8px;border:none;font-size:1.4rem;color:var(--text-muted);display:none" id="sidebarCloseBtn">&times;</button>
    </div>

    <nav class="sidebar-nav">
        <div class="sidebar-label">Main</div>

        <a href="index.php" class="<?= $active_nav === 'dashboard' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/>
                <rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/>
            </svg>
            Dashboard
        </a>

        <a href="tenants.php" class="<?= $active_nav === 'tenants' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/>
            </svg>
            Schools Directory
        </a>

        <a href="domains.php" class="<?= $active_nav === 'domains' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <circle cx="12" cy="12" r="9"/><path d="M3.6 9h16.8M3.6 15h16.8M12 3a15 15 0 014 9 15 15 0 01-4 9 15 15 0 01-4-9 15 15 0 014-9z"/>
            </svg>
            Custom Domains
        </a>

        <div class="sidebar-label">Onboarding</div>

        <a href="provision.php" class="<?= $active_nav === 'provision' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path d="M12 4v16m8-8H4"/>
            </svg>
            Provision School
        </a>

        <div class="sidebar-label">Operations</div>

        <a href="migrations.php" class="<?= $active_nav === 'migrations' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path d="M4 7v10c0 2.21 3.582 4 8 4s8-1.79 8-4V7M4 7c0 2.21 3.582 4 8 4s8-1.79 8-4M4 7c0-2.21 3.582-4 8-4s8 1.79 8 4m0 5c0 2.21-3.582 4-8 4s-8-1.79-8-4"/>
            </svg>
            Schema Migrations
        </a>

        <a href="cron.php" class="<?= $active_nav === 'cron' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 3"/>
            </svg>
            Cron & Tasks
        </a>

        <div class="sidebar-label">Finance</div>

        <a href="billing.php" class="<?= $active_nav === 'billing' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <rect x="2" y="5" width="20" height="14" rx="2"/><line x1="2" y1="10" x2="22" y2="10"/>
            </svg>
            Subscriptions & Billing
        </a>

        <a href="plans.php" class="<?= $active_nav === 'plans' ? 'active' : '' ?>">
            <svg width="17" height="17" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
            </svg>
            Plans & Pricing
        </a>
    </nav>

    <div class="sidebar-footer">
        <strong><?= htmlspecialchars($saas_admin_user['full_name'] ?? $saas_admin_user['username'] ?? 'Super Admin') ?></strong>
        <span class="badge badge-super_admin"><?= htmlspecialchars($saas_admin_user['role'] ?? 'super_admin') ?></span>
        <div style="margin-top:10px">
            <a href="logout.php">
                <svg width="14" height="14" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/>
                </svg>
                Sign Out
            </a>
        </div>
    </div>
</aside>

<script>
function toggleSidebar() {
    const sb = document.getElementById('adminSidebar');
    if (sb) sb.classList.toggle('open');
}
if (window.innerWidth <= 992) {
    const closeBtn = document.getElementById('sidebarCloseBtn');
    if (closeBtn) closeBtn.style.display = 'block';
}
</script>
