<?php
ini_set('display_errors', 0);
include(__DIR__ . '/functions.php');

// Include config
$config = include(__DIR__ . '/config.php');

// Get request count
$count = $db->getRecordCount('actreq');
$displayCount = ($count !== '' && $count > 99) ? '99+' : $count;

$badgeHtml = ($count === '') ? '' : '<span class="badge badge-danger" 
    style="background:red; color:white; border-radius:50%; padding:4px 8px; font-size:12px; margin-left:5px;">' . $displayCount . '</span>';

// Use config variables
$superusername = $config['superusername'] ?? 'superadmin';
$superpassword = $config['superpassword'] ?? 'superadmin';
$weblink = $config['weblink'] ?? 'https://t.me/RTXshowcase';
$brandname = $config['brandname'] ?? 'RTX Rebrands';
$visione = $config['visione'] ?? 'We make modern TV simple, smooth, and personal';
$footerstatu = $config['footerstatu'] ?? 'enable';
$email = $config['email'] ?? 'lankadroid@gmail.com';
$whatsapp = $config['whatsapp'] ?? 'https://wa.me/5561999831994';

// Atualizando os �cones para puxar a URL externa
$icon32x32 = 'https://a.imagem.app/B9faua.png';
$icon16x16 = 'https://a.imagem.app/B9faua.png';
$iconaplle = 'https://a.imagem.app/B9faua.png';
$iconmain = 'https://a.imagem.app/B9faua.png';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$log_check = $db->select('user', '*', 'id = :id', '', [':id' => 1]);
$loggedinuser = !empty($log_check) ? $log_check[0]['username'] : null;

if (!isset($_SESSION['name']) || $_SESSION['name'] !== $loggedinuser) {
    header("Location: index.php");
    exit();
}

if (isset($_REQUEST['logout'])) {
    session_destroy();
    setcookie("auth", "", time() - 3600);
    header("Location: index.php");
    exit;
}

$time = $_SERVER['REQUEST_TIME'];

$timeout_duration = 900;
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY']) > $timeout_duration) {
    session_unset();
    session_destroy();
    session_start();
}
$_SESSION['LAST_ACTIVITY'] = $time;

function sanitize($data) {
    $data = trim($data);
    $data = htmlspecialchars($data, ENT_QUOTES);
    $data = SQLite3::escapeString($data);
    return $data;
}

function adminisking() {
    global $superusername;
    if ($_SESSION['name'] === $superusername) {
        return $_SESSION['name'] . ' 👑';
    } else {
        return $_SESSION['name'] . ' 🔥';
    }
}
?>


<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="utf-8">
    <title><?php echo htmlspecialchars($brandname); ?></title>
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <meta content="" name="keywords">
    <meta content="" name="description">

    <!-- Favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="<?php echo htmlspecialchars($iconaplle); ?>">
    <link rel="icon" type="image/png" sizes="32x32" href="<?php echo htmlspecialchars($icon32x32); ?>">
    <link rel="icon" type="image/png" sizes="16x16" href="<?php echo htmlspecialchars($icon16x16); ?>">
    <link rel="manifest" href="<?php echo htmlspecialchars($iconmain); ?>">

    <!-- Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Font Awesome 5.10.0 (mantendo compatibilidade com ícones originais) -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.0/css/all.min.css" rel="stylesheet">
    <!-- Font Awesome 4.7.0 (fallback para ícones específicos) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css" rel="stylesheet">

    <!-- Libraries Stylesheet -->
    <link href="lib/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet">
    <link href="lib/tempusdominus/css/tempusdominus-bootstrap-4.min.css" rel="stylesheet" />

    <!-- Customized Bootstrap Stylesheet -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Template Stylesheet -->
    <link href="css/style.css" rel="stylesheet">

    <style>
        :root {
            --primary-dark: #0f172a;
            --secondary-dark: #1e293b;
            --accent-blue: #1e40af;
            --accent-blue-hover: #2563eb;
            --text-primary: #f8fafc;
            --text-secondary: #cbd5e1;
            --text-muted: #64748b;
            --border-color: #334155;
            --success-color: #10b981;
            --danger-color: #ef4444;
            --shadow-color: rgba(0, 0, 0, 0.25);
        }

        * {
            font-family: 'Inter', 'Poppins', -apple-system, BlinkMacSystemFont, sans-serif;
        }

        body {
            background: var(--primary-dark);
            color: var(--text-primary);
            font-weight: 400;
            line-height: 1.6;
        }

        .version {
            font-size: 0.75rem;
            color: #6c757d;
            vertical-align: bottom;
            position: relative;
            top: 6px;
        }

        .bytx {
            font-size: 0.75rem;
            font-weight: 500;
            color: var(--accent-blue);
            letter-spacing: 2px;
            text-transform: uppercase;
            margin-top: 0.25rem;
            opacity: 0.9;
        }

        /* Spinner Modernized */
        #spinner {
            background: var(--primary-dark);
        }

        .spinner-border {
            border-color: var(--accent-blue) transparent var(--accent-blue) transparent;
        }

        /* Main Container */
        .container-fluid {
            background: var(--primary-dark);
        }

        /* Modern Sidebar */
        .sidebar {
            background: linear-gradient(135deg, var(--secondary-dark) 0%, var(--primary-dark) 100%);
            border-right: 1px solid var(--border-color);
            box-shadow: 4px 0 20px var(--shadow-color);
            min-height: 100vh;
        }

        .navbar.bg-secondary {
            background: transparent !important;
            padding: 1.5rem 0;
        }

        /* Logo and Brand */
        .navbar-brand {
            padding: 1rem 1.5rem;
            margin-bottom: 1rem !important;
        }

        .navbar-brand h3 {
            font-family: 'Poppins', sans-serif;
            font-weight: 700;
            font-size: 1.75rem;
            color: var(--text-primary) !important;
            margin: 0;
            text-shadow: 0 2px 4px var(--shadow-color);
        }

        .navbar-brand .fa-cubes {
            color: var(--accent-blue) !important;
            margin-right: 0.75rem;
            font-size: 1.5rem;
        }

        /* Modernized Navigation */
        .navbar-nav {
            padding: 0 0.5rem;
        }

        .nav-item .nav-link {
            color: var(--text-secondary) !important;
            padding: 1rem 1.5rem;
            margin: 0.25rem 0;
            border-radius: 12px;
            font-weight: 500;
            font-size: 0.95rem;
            transition: all 0.3s ease;
            position: relative;
            border: 1px solid transparent;
        }

        .nav-item .nav-link:hover {
            background: linear-gradient(135deg, var(--accent-blue) 0%, var(--accent-blue-hover) 100%);
            color: var(--text-primary) !important;
            transform: translateX(4px);
            border-color: var(--accent-blue-hover);
            box-shadow: 0 4px 12px rgba(30, 64, 175, 0.3);
        }

        .nav-item .nav-link.active {
            background: linear-gradient(135deg, var(--accent-blue) 0%, var(--accent-blue-hover) 100%);
            color: var(--text-primary) !important;
            border-color: var(--accent-blue-hover);
            box-shadow: 0 4px 12px rgba(30, 64, 175, 0.4);
        }

        .nav-item .nav-link i {
            width: 20px;
            text-align: center;
            margin-right: 0.875rem;
            font-size: 1.1rem;
            display: inline-block;
        }

        /* Garantindo que os ícones apareçam */
        .nav-item .nav-link .fa-globe:before { content: "\f0ac"; }
        .nav-item .nav-link .fa-user:before { content: "\f007"; }
        .nav-item .nav-link .fa-users:before { content: "\f0c0"; }
        .nav-item .nav-link .fa-id-card:before { content: "\f2c2"; }
        .nav-item .nav-link .fa-paper-plane:before { content: "\f1d8"; }
        .nav-item .nav-link .fa-commenting:before { content: "\f27a"; }
        .nav-item .nav-link .fa-bell:before { content: "\f0f3"; }
        .nav-item .nav-link .fa-video-camera:before { content: "\f03d"; }
        .nav-item .nav-link .fa-video:before { content: "\f03d"; }
        .nav-item .nav-link .fa-futbol-o:before { content: "\f1e3"; }
        .nav-item .nav-link .fa-futbol:before { content: "\f1e3"; }
        .nav-item .nav-link .fa-sliders:before { content: "\f1de"; }
        .nav-item .nav-link .fa-cogs:before { content: "\f085"; }
        .nav-item .nav-link .fa-key:before { content: "\f084"; }
        .nav-item .nav-link .fa-fire:before { content: "\f06d"; }
        .nav-item .nav-link .fa-crown:before { content: "\f521"; }

        /* Notification Badge */
        .badge-danger {
            background: linear-gradient(135deg, var(--danger-color) 0%, #dc2626 100%) !important;
            color: white !important;
            border-radius: 20px !important;
            padding: 0.25rem 0.75rem !important;
            font-size: 0.75rem !important;
            font-weight: 600 !important;
            margin-left: 0.5rem !important;
            box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3) !important;
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0% { box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3); }
            50% { box-shadow: 0 2px 12px rgba(239, 68, 68, 0.6); }
            100% { box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3); }
        }

        /* Content Area */
        .content {
            background: var(--primary-dark);
            min-height: 100vh;
        }

        /* Top Navbar */
        .navbar.navbar-expand {
            background: var(--secondary-dark) !important;
            border-bottom: 1px solid var(--border-color);
            box-shadow: 0 2px 20px var(--shadow-color);
            backdrop-filter: blur(10px);
        }

        .navbar-brand.d-flex.d-lg-none h2 {
            color: var(--accent-blue) !important;
        }

        .sidebar-toggler {
            color: var(--text-secondary) !important;
            padding: 0.75rem;
            border-radius: 8px;
            transition: all 0.3s ease;
        }

        .sidebar-toggler:hover {
            background: var(--accent-blue);
            color: var(--text-primary) !important;
        }

        /* User Dropdown */
        .dropdown-toggle {
            color: var(--text-primary) !important;
            font-weight: 500;
            padding: 0.75rem 1rem;
            border-radius: 8px;
            transition: all 0.3s ease;
        }

        .dropdown-toggle:hover {
            background: rgba(255, 255, 255, 0.1);
        }

        .dropdown-toggle img {
            border: 2px solid var(--accent-blue);
            box-shadow: 0 2px 8px rgba(30, 64, 175, 0.3);
        }

        .dropdown-menu {
            background: var(--secondary-dark) !important;
            border: 1px solid var(--border-color) !important;
            box-shadow: 0 8px 32px var(--shadow-color) !important;
            border-radius: 12px !important;
        }

        .dropdown-item {
            color: var(--text-secondary) !important;
            padding: 0.75rem 1.25rem !important;
            transition: all 0.3s ease;
            border-radius: 8px;
            margin: 0.25rem 0.5rem;
        }

        .dropdown-item:hover {
            background: var(--accent-blue) !important;
            color: var(--text-primary) !important;
        }

        /* Responsiveness */
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
                transition: transform 0.3s ease;
            }
            
            .sidebar.show {
                transform: translateX(0);
            }
            
            .navbar-brand h3 {
                font-size: 1.5rem;
            }
            
            .nav-item .nav-link {
                padding: 0.875rem 1.25rem;
                font-size: 0.9rem;
            }
        }

        /* Additional Visual Enhancements */
        .nav-item .nav-link::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 3px;
            height: 0;
            background: var(--accent-blue);
            transition: height 0.3s ease;
            border-radius: 0 2px 2px 0;
        }

        .nav-item .nav-link:hover::before,
        .nav-item .nav-link.active::before {
            height: 60%;
        }

        /* Custom Scrollbar */
        .sidebar::-webkit-scrollbar {
            width: 6px;
        }

        .sidebar::-webkit-scrollbar-track {
            background: var(--primary-dark);
        }

        .sidebar::-webkit-scrollbar-thumb {
            background: var(--accent-blue);
            border-radius: 3px;
        }

        .sidebar::-webkit-scrollbar-thumb:hover {
            background: var(--accent-blue-hover);
        }
    </style>
</head>

<body>
    <div class="container-fluid position-relative d-flex p-0">
        <!-- Spinner Start -->
        <div id="spinner"
            class="show bg-dark position-fixed translate-middle w-100 vh-100 top-50 start-50 d-flex align-items-center justify-content-center">
            <div class="spinner-border text-primary" style="width: 3rem; height: 3rem;" role="status">
                <span class="sr-only">Loading...</span>
            </div>
        </div>
        <!-- Spinner End -->

        <!-- Sidebar Start -->
        <div class="sidebar pe-4 pb-3">
            <nav class="navbar bg-secondary navbar-dark">
                <a href="index.php" class="navbar-brand mx-4 mb-2">
                    <div class="d-flex flex-column">
                        <div class="d-flex align-items-center">
                            <i class="fas fa-play me-2"></i><?php echo htmlspecialchars($brandname); ?>
                        </div>
                        <span class="bytx mt-1">Configuração</span>
                    </div>
                </a>

                <div class="navbar-nav w-100">
                    <?php if ($_SESSION['name'] === $superusername || $_SESSION['passwordxyz'] === $superpassword) { ?>
                        <a href="admin.php"
                           class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'admin.php' ? 'active' : ''; ?>">
                            <i class="fas fa-crown me-2"></i>Super Administrador
                        </a>
                    <?php } ?>
                    <a href="main.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'main.php' ? 'active' : ''; ?>">
                        <i class="fas fa-globe me-2"></i>Configurações de DNS
                    </a>
                    <a href="user_manage.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'user_manage.php' ? 'active' : ''; ?>">
                        <i class="fas fa-users me-2"></i>Usuários MAC
                    </a>
                    <a href="mac_manage.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'mac_manage.php' ? 'active' : ''; ?>">
                        <i class="fas fa-id-card me-2"></i>Configurações MAC
                    </a>
                    <a href="activation_request.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'activation_request.php' ? 'active' : ''; ?>">
                        <i class="fas fa-paper-plane me-2"></i>Solicitações <?php echo $badgeHtml; ?>
                    </a>
                    <a href="noti_settings.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'noti_settings.php' ? 'active' : ''; ?>">
                        <i class="fas fa-bell me-2"></i>Notificações
                    </a>
                    <a href="intro_settings.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'intro_settings.php' ? 'active' : ''; ?>">
                        <i class="fas fa-video me-2"></i>Configurações de Intro
                    </a>
                    <a href="hot_sports.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'hot_sports.php' ? 'active' : ''; ?>">
                        <i class="fas fa-fire me-2"></i>Ligas Populares
                    </a>
                    <a href="sport_api.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'sport_api.php' ? 'active' : ''; ?>">
                        <i class="fas fa-futbol me-2"></i>Chave API Esportes
                    </a>
                    <a href="dev_option.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'dev_option.php' ? 'active' : ''; ?>">
                        <i class="fas fa-cogs me-2"></i>Opções Avançadas
                    </a>
                    <a href="user.php"
                       class="nav-item nav-link <?php echo basename($_SERVER['PHP_SELF']) === 'user.php' ? 'active' : ''; ?>">
                        <i class="fas fa-key me-2"></i>Credenciais
                    </a>
                </div>
            </nav>
        </div>
        <!-- Sidebar End -->

        <!-- Content Start -->
        <div class="content">
            <!-- Navbar Start -->
           <nav class="navbar navbar-expand-lg navbar-dark bg-dark sticky-top px-4 py-3 shadow-lg">
    
    <a href="index.php" class="navbar-brand d-lg-none me-3">
        <h2 class="text-primary mb-0"><i class="fa fa-cubes"></i></h2>
    </a>
    
    <a href="#" class="sidebar-toggler flex-shrink-0 text-white me-3 d-lg-block">
        <i class="fa fa-bars fa-lg"></i>
    </a>
    
    <div class="ms-auto">
        <div class="nav-item dropdown">
            <a href="#" class="nav-link dropdown-toggle d-flex align-items-center" data-bs-toggle="dropdown" aria-expanded="false">
                <img class="rounded-circle me-lg-2 border border-3 border-info" src="img/rtx_bg.png" alt="User Profile" style="width: 45px; height: 45px;">
                <span class="d-none d-lg-inline-flex"><?= htmlspecialchars(adminisking()); ?></span>
            </a>
            <div class="dropdown-menu dropdown-menu-end dropdown-menu-dark border-0 rounded-3 shadow-lg">
                <a href="<?= htmlspecialchars($weblink); ?>" class="dropdown-item py-2 px-3">
                    <?= htmlspecialchars($brandname); ?>
                </a>
                <a href="<?= basename($_SERVER["SCRIPT_NAME"]) . '?logout' ?>" class="dropdown-item py-2 px-3">
                    <i class="fa fa-sign-out-alt me-2 text-danger"></i><span class="text-danger">Sair</span>
                </a>
            </div>
        </div>
    </div>
    
</nav>
            <!-- Navbar End -->