<?php
// OneLauncher - includes/topbar.php
global $conn, $active_theme_id;

$themesRes = $conn->query("SELECT id, name FROM themes ORDER BY id ASC");
$allThemes = [];
$activeThemeName = "Tema Padrão";
if ($themesRes) {
    while($t = $themesRes->fetch_assoc()) {
        $allThemes[] = $t;
        if ($t['id'] == $active_theme_id) $activeThemeName = $t['name'];
    }
}
?>
<div class="mobile-topbar">
    <div class="mobile-topbar__left" style="display: flex; align-items: center; gap: 15px;">
        <button class="sidebar-toggle" id="sidebarToggle">
            <i class="bi bi-list"></i>
        </button>
        <h1 class="topbar-page-title m-0 d-none d-sm-block"><?php echo htmlspecialchars($pageTitle); ?></h1>
        
        <div class="dropdown">
            <button class="btn btn-sm btn-outline-primary border-opacity-25 dropdown-toggle" type="button" data-bs-toggle="dropdown" style="background: rgba(13, 110, 253, 0.1);">
                <img width="20" height="20" src="https://img.icons8.com/papercut/60/color-palette.png" alt="color-palette"> Editando:</span> <strong><?php echo htmlspecialchars($activeThemeName); ?></strong>
            </button>
            <ul class="dropdown-menu dropdown-menu-dark shadow">
                <?php foreach($allThemes as $theme): ?>
                    <li><a class="dropdown-item <?php echo $theme['id'] == $active_theme_id ? 'active' : ''; ?>" href="?set_theme=<?php echo $theme['id']; ?>">
                        <?php echo htmlspecialchars($theme['name']); ?>
                    </a></li>
                <?php endforeach; ?>
                <li><hr class="dropdown-divider border-white border-opacity-10"></li>
                <li><a class="dropdown-item text-warning" href="themes.php"><i class="bi bi-plus-circle me-1"></i> Gerenciar Temas</a></li>
            </ul>
        </div>
    </div>
    <div class="header">
        <div class="profile-menu">
            <button id="profileToggle">
                <div class="user-avatar" style="padding: 0; overflow: hidden; background: transparent;">
                    <img src="assets/upload/perfil.png" alt="Perfil"
                         style="width: 100%; height: 100%; object-fit: cover; border-radius: 50%;"
                         onerror="this.style.display='none'; this.parentElement.innerHTML='AD'; this.parentElement.style.background=''; this.parentElement.style.padding='';">
                </div>
            </button>
            <div class="profile-dropdown" id="profileDropdown">
                <a href="logout.php"><i class="bi bi-box-arrow-left me-2"></i> Sair</a>
            </div>
        </div>
    </div>
</div>
