<?php
/**
 * Super Admin Sidebar Component
 * Menu lateral reorganizado com submenus
 * Uso: include __DIR__ . '/includes/sidebar.php';
 * Variável necessária: $currentPage (ex: 'dashboard', 'stores', 'plans', etc)
 */
$currentPage = $currentPage ?? 'dashboard';

// Carregar configurações de logo (otimizado: 1 query ao invés de 4)
require_once __DIR__ . '/../../../includes/landing-functions.php';
$pdoLogo = getDB();
$_logoKeys = ['header_logo', 'header_logo_type', 'header_logo_text', 'header_logo_dark'];
$_logoDefaults = ['header_logo' => '', 'header_logo_type' => 'text', 'header_logo_text' => 'MontaLoja', 'header_logo_dark' => ''];
try {
    $_logoStmt = $pdoLogo->prepare("SELECT section_key, section_value FROM landing_page_config WHERE section_key IN (?,?,?,?) AND is_active = 1");
    $_logoStmt->execute($_logoKeys);
    $_logoResults = $_logoStmt->fetchAll(PDO::FETCH_KEY_PAIR);
    $_logoConfig = array_merge($_logoDefaults, $_logoResults);
} catch (PDOException $e) {
    $_logoConfig = $_logoDefaults;
}
$sidebarLogo = $_logoConfig['header_logo'];
$sidebarLogoType = $_logoConfig['header_logo_type'];
$sidebarLogoText = $_logoConfig['header_logo_text'];
$sidebarLogoDark = $_logoConfig['header_logo_dark'];

// Detectar se está em staging (dev.duolead.com.br) ou produção (duolead.com.br)
$isStaging = strpos($_SERVER['HTTP_HOST'] ?? '', 'dev.') === 0;

// Definir grupos de menu para destacar submenu ativo
$businessPages = ['stores', 'subscriptions', 'plans'];
$contentPages = ['users', 'templates', 'landing'];
$systemPages = ['whatsapp', 'notifications', 'reports', 'deploy', 'settings', 'gateways', 'backup'];
?>
<aside class="sidebar">
    <div class="sidebar-header">
        <div class="logo sidebar-logo">
            <?php if ($sidebarLogoType === 'image' && !empty($sidebarLogo)): ?>
                <img src="<?php echo htmlspecialchars($sidebarLogo); ?>" alt="<?php echo htmlspecialchars($sidebarLogoText); ?>" class="logo-light" style="max-height: 32px; width: auto; object-fit: contain;">
                <?php if (!empty($sidebarLogoDark)): ?>
                    <img src="<?php echo htmlspecialchars($sidebarLogoDark); ?>" alt="<?php echo htmlspecialchars($sidebarLogoText); ?>" class="logo-dark" style="max-height: 32px; width: auto; object-fit: contain;">
                <?php endif; ?>
            <?php else: ?>
                <iconify-icon icon="solar:crown-bold" width="32" style="color: var(--gold);"></iconify-icon>
                <span><?php echo htmlspecialchars($sidebarLogoText); ?></span>
            <?php endif; ?>
            <span class="superadmin-badge">SuperAdmin</span>
        </div>

    </div>

    <nav class="sidebar-nav">
        <!-- Dashboard -->
        <a href="/pages/superadmin/dashboard.php" class="nav-item <?= $currentPage === 'dashboard' ? 'active' : '' ?>">
            <iconify-icon icon="solar:chart-bold"></iconify-icon>
            <span>Dashboard</span>
        </a>

        <!-- Seção: Negócios -->
        <div class="nav-section">
            <span class="nav-section-title">Negócios</span>
        </div>

        <a href="/pages/superadmin/stores/index.php" class="nav-item <?= $currentPage === 'stores' ? 'active' : '' ?>">
            <iconify-icon icon="solar:shop-bold"></iconify-icon>
            <span>Lojas</span>
        </a>

        <a href="/pages/superadmin/system/subscriptions.php" class="nav-item <?= $currentPage === 'subscriptions' ? 'active' : '' ?>">
            <iconify-icon icon="solar:wallet-bold"></iconify-icon>
            <span>Assinaturas</span>
        </a>

        <a href="/pages/superadmin/system/plans.php" class="nav-item <?= $currentPage === 'plans' ? 'active' : '' ?>">
            <iconify-icon icon="solar:tag-price-bold"></iconify-icon>
            <span>Planos</span>
        </a>

        <!-- Seção: Conteúdo -->
        <div class="nav-section">
            <span class="nav-section-title">Conteúdo</span>
        </div>

        <a href="/pages/superadmin/users/index.php" class="nav-item <?= $currentPage === 'users' ? 'active' : '' ?>">
            <iconify-icon icon="solar:users-group-rounded-bold"></iconify-icon>
            <span>Usuários</span>
        </a>

        <a href="/pages/superadmin/templates/index.php" class="nav-item <?= $currentPage === 'templates' ? 'active' : '' ?>">
            <iconify-icon icon="solar:palette-bold"></iconify-icon>
            <span>Templates</span>
        </a>
        <a href="/pages/superadmin/landing/index.php" class="nav-item <?= $currentPage === 'landing' ? 'active' : '' ?>">
            <iconify-icon icon="solar:monitor-bold"></iconify-icon>
            <span>Landing Page</span>
        </a>


        <!-- Seção: Sistema -->
        <div class="nav-section">
            <span class="nav-section-title">Sistema</span>
        </div>

        <a href="/pages/superadmin/system/whatsapp.php" class="nav-item <?= $currentPage === 'whatsapp' ? 'active' : '' ?>">
            <iconify-icon icon="logos:whatsapp-icon"></iconify-icon>
            <span>WhatsApp</span>
        </a>

        <a href="/pages/superadmin/system/notifications.php" class="nav-item <?= $currentPage === 'notifications' ? 'active' : '' ?>">
            <iconify-icon icon="solar:bell-bold"></iconify-icon>
            <span>Notificações</span>
        </a>

        <a href="/pages/superadmin/system/reports.php" class="nav-item <?= $currentPage === 'reports' ? 'active' : '' ?>">
            <iconify-icon icon="solar:graph-up-bold"></iconify-icon>
            <span>Relatórios</span>
        </a>

        <?php if ($isStaging): ?>
        <a href="/pages/superadmin/system/deploy.php" class="nav-item <?= $currentPage === 'deploy' ? 'active' : '' ?>">
            <iconify-icon icon="solar:rocket-bold"></iconify-icon>
            <span>Deploy</span>
        </a>
        <?php endif; ?>

        <a href="/pages/superadmin/system/backup.php" class="nav-item <?= $currentPage === 'backup' ? 'active' : '' ?>">
            <iconify-icon icon="solar:cloud-download-bold"></iconify-icon>
            <span>Backup</span>
        </a>


        <a href="/pages/superadmin/settings/gateways.php" class="nav-item <?= $currentPage === 'gateways' ? 'active' : '' ?>">
            <iconify-icon icon="solar:card-transfer-bold"></iconify-icon>
            <span>Gateways de Pagamento</span>
        </a>

        <a href="/pages/superadmin/settings/index.php" class="nav-item <?= $currentPage === 'settings' ? 'active' : '' ?>">
            <iconify-icon icon="solar:settings-bold"></iconify-icon>
            <span>Configurações</span>
        </a>

    </nav>

    <div class="sidebar-footer">
        <a href="/pages/superadmin/auth/logout.php" class="nav-item danger">
            <iconify-icon icon="solar:logout-2-bold"></iconify-icon>
            <span>Sair</span>
        </a>
    </div>
</aside>



<style>
/* Badge para identificar área */
.superadmin-badge {
    display: inline-block;
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    background: #b8941f15;
    color: var(--gold);
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    margin-top: 0.5rem;
    font-weight: 600;
}
.logo{
    display: flex;
    align-items: flex-start;
    gap: 0.01rem!important;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 0.01rem!important;
    flex-direction: column;
}

/* Estilos para seções do menu */
.nav-section {
    padding: 0.5rem 1rem 0.25rem 1rem;
    margin-top: 0.25rem;
    margin-bottom: 0.25rem;
}

.nav-section-title {
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    opacity: 0.7;
}

/* Ajuste para primeiro item após seção */
.nav-section + .nav-item {
    margin-top: 0;
}

/*badge Super Admin*/

/* Sidebar responsiva - esconder títulos em mobile */
/* Sidebar footer fixo no final */
.sidebar-footer {
    margin-top: auto;
    border-top: 1px solid var(--border-light);
    padding-top: 0.5rem;
}


</style>
