<?php

require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/helpers.php';

function render_header(string $title, string $active = '', string $subtitle = '', array $breadcrumbs = []): void
{
    $flash = get_flash();
    $stats = get_dashboard_stats();
    $user = $_SESSION['panel_user'] ?? 'admin';
    ?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($title) ?> — LauncherIO2026</title>
    <link rel="stylesheet" href="assets/css/admin.css">
</head>
<body>
<div class="app-shell">
    <div class="sidebar-overlay" id="sidebarOverlay" aria-hidden="true"></div>

    <aside class="sidebar" id="sidebar">
        <div class="sidebar-brand">
            <div class="logo-icon">📺</div>
            <div>
                <h2>LauncherIO2026</h2>
                <small>Painel Multi-ID</small>
            </div>
        </div>

        <nav class="sidebar-nav">
            <div class="nav-section">
                <span class="nav-section-label">Principal</span>
                <a href="dashboard.php" class="<?= $active === 'dashboard' ? 'active' : '' ?>">
                    <span class="nav-icon">📊</span>
                    <span class="nav-text">Dashboard</span>
                </a>
                <a href="devices.php" class="<?= $active === 'devices' ? 'active' : '' ?>">
                    <span class="nav-icon">📺</span>
                    <span class="nav-text">Dispositivos</span>
                    <?php if ($stats['total_devices'] > 0): ?>
                        <span class="nav-badge"><?= (int) $stats['total_devices'] ?></span>
                    <?php endif; ?>
                </a>
            </div>

            <div class="nav-section">
                <span class="nav-section-label">Infraestrutura</span>
                <a href="dns.php" class="<?= $active === 'dns' ? 'active' : '' ?>">
                    <span class="nav-icon">🌐</span>
                    <span class="nav-text">DNS Xtream</span>
                    <span class="nav-badge muted"><?= (int) $stats['dns_active'] ?></span>
                </a>
            </div>

            <div class="nav-section">
                <span class="nav-section-label">Suporte</span>
                <a href="contact.php" class="<?= $active === 'contact' ? 'active' : '' ?>">
                    <span class="nav-icon">💬</span>
                    <span class="nav-text">Contato / Renovação</span>
                </a>
            </div>

            <div class="nav-section">
                <span class="nav-section-label">Sistema</span>
                <a href="<?= htmlspecialchars(PANEL_BASE_URL) ?>/api/v1/config.php" target="_blank" rel="noopener">
                    <span class="nav-icon">🔗</span>
                    <span class="nav-text">API JSON</span>
                    <span class="nav-external">↗</span>
                </a>
                <a href="credentials.php" class="<?= $active === 'credentials' ? 'active' : '' ?>">
                    <span class="nav-icon">🔑</span>
                    <span class="nav-text">Alterar credenciais</span>
                </a>
                <a href="logout.php" class="danger-link">
                    <span class="nav-icon">🚪</span>
                    <span class="nav-text">Sair</span>
                </a>
            </div>
        </nav>

        <div class="sidebar-footer">
            <div class="sidebar-status">
                <span class="status-dot <?= $stats['db_ok'] ? 'ok' : 'err' ?>"></span>
                <span>Banco de dados</span>
            </div>
            <div class="sidebar-status">
                <span class="status-dot <?= $stats['uploads_writable'] ? 'ok' : 'err' ?>"></span>
                <span>Uploads</span>
            </div>
            <div class="panel-url"><?= htmlspecialchars(PANEL_BASE_URL) ?></div>
        </div>
    </aside>

    <div class="main-area">
        <header class="topbar">
            <div class="topbar-left">
                <button type="button" class="menu-toggle" id="menuToggle" aria-label="Abrir menu">☰</button>
                <div class="topbar-titles">
                    <?php if (!empty($breadcrumbs)): ?>
                        <nav class="breadcrumbs" aria-label="Breadcrumb">
                            <?php foreach ($breadcrumbs as $i => $crumb): ?>
                                <?php if ($i > 0): ?><span class="bc-sep">/</span><?php endif; ?>
                                <?php if (!empty($crumb['url'])): ?>
                                    <a href="<?= htmlspecialchars($crumb['url']) ?>"><?= htmlspecialchars($crumb['label']) ?></a>
                                <?php else: ?>
                                    <span class="bc-current"><?= htmlspecialchars($crumb['label']) ?></span>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </nav>
                    <?php endif; ?>
                    <h1 class="page-title"><?= htmlspecialchars($title) ?></h1>
                    <?php if ($subtitle): ?>
                        <p class="page-subtitle"><?= htmlspecialchars($subtitle) ?></p>
                    <?php endif; ?>
                </div>
            </div>
            <div class="topbar-right">
                <div class="system-pills">
                    <span class="sys-pill <?= $stats['online_now'] > 0 ? 'ok' : 'warn' ?>">
                        <?= (int) $stats['online_now'] ?> online
                    </span>
                    <span class="sys-pill <?= $stats['dns_active'] > 0 ? 'ok' : 'err' ?>">
                        DNS <?= (int) $stats['dns_active'] ?>/<?= (int) $stats['dns_total'] ?>
                    </span>
                    <span class="sys-pill neutral"><?= htmlspecialchars($stats['server_time']) ?></span>
                </div>
                <div class="user-chip"><?= htmlspecialchars($user) ?></div>
            </div>
        </header>

        <main class="main">
            <?php if ($flash): ?>
                <div class="flash <?= htmlspecialchars($flash['type']) ?>">
                    <?= htmlspecialchars($flash['message']) ?>
                </div>
            <?php endif; ?>
    <?php
}

function render_footer(): void
{
    ?>
        </main>
    </div>
</div>
<script src="assets/js/panel.js"></script>
</body>
</html>
    <?php
}
