<?php
declare(strict_types=1);

require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/api_helpers.php';
gpc_require_login();

$current = basename($_SERVER['PHP_SELF'] ?? '');
$panelName = gpc_setting('panel_name', 'GPC Panel');
$pageTitle = $pageTitle ?? 'Painel';
$flash = gpc_take_flash();

function nav_active(string $file, string $current): string
{
    return $file === $current ? ' active' : '';
}

function nav_item(string $file, string $current, string $href, string $icon, string $label): void
{
    $cls = nav_active($file, $current);
    echo '<li><a class="' . $cls . '" href="' . gpc_h($href) . '">';
    echo '<i data-lucide="' . gpc_h($icon) . '"></i><span>' . gpc_h($label) . '</span></a></li>';
}
?>
<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= gpc_page_title($pageTitle) ?></title>
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="backdrop" id="backdrop"></div>
<div class="shell">
    <aside class="sidebar" id="sidebar">
        <div class="sidebar-head">
            <div class="mark"><i data-lucide="tv-minimal-play"></i></div>
            <span><?= gpc_h($panelName) ?></span>
        </div>
        <ul class="nav">
            <?php
            nav_item('dashboard.php', $current, 'dashboard.php', 'layout-dashboard', 'Início');
            echo '<li class="nav-section">Clientes APP</li>';
            nav_item('clients_app.php', $current, 'clients_app.php', 'badge-check', 'Licenças MAC');
            nav_item('revendedores.php', $current, 'revendedores.php', 'users-round', 'Revendedores');
            echo '<li><a href="client.php" target="_blank"><i data-lucide="monitor-smartphone"></i><span>Painel Cliente</span></a></li>';
            echo '<li class="nav-section">Parceiros DNS</li>';
            nav_item('dns.php', $current, 'dns.php', 'server', 'DNS / Códigos');
            nav_item('partner_devices.php', $current, 'partner_devices.php', 'network', 'MACs por Código');
            echo '<li class="nav-section">Sistema</li>';
            nav_item('logs.php', $current, 'logs.php', 'history', 'Registros');
            nav_item('client_security_logs.php', $current, 'client_security_logs.php', 'shield-check', 'Segurança Cliente');
            nav_item('status.php', $current, 'status.php', 'activity', 'Status do Sistema');
            nav_item('appearance.php', $current, 'appearance.php', 'palette', 'Visual');
            nav_item('banners.php', $current, 'banners.php', 'images', 'Banners');
            nav_item('notification.php', $current, 'notification.php', 'bell', 'Aviso');
            nav_item('qrcode.php', $current, 'qrcode.php', 'qr-code', 'QR Code');
            nav_item('config.php', $current, 'config.php', 'settings', 'API');
            ?>
            <li class="nav-divider" role="presentation"></li>
            <?php nav_item('account.php', $current, 'account.php', 'user', 'Conta'); ?>
            <li><a href="logout.php"><i data-lucide="log-out"></i><span>Sair</span></a></li>
        </ul>
    </aside>
    <div class="main">
        <header class="topbar">
            <div class="topbar-left">
                <button type="button" class="icon-btn" id="menuBtn" aria-label="Menu">
                    <i data-lucide="menu"></i>
                </button>
                <h1><?= gpc_h($pageTitle) ?></h1>
            </div>
            <div class="user-chip">
                <span class="avatar"><?= strtoupper(substr(gpc_admin_user(), 0, 1)) ?></span>
                <span><?= gpc_h(gpc_admin_user()) ?></span>
            </div>
        </header>
        <main class="page">
