<?php
ob_start();
if (session_status() === PHP_SESSION_NONE) {
    @session_start();
}

if (!isset($_SESSION["ansnscript_admin"])) {
    header("location:login.php");
    exit;
}

$dbans = new SQLite3("./api/.ansdb.db");
require_once __DIR__ . '/app_connection_helper.php';
app_connection_ensure_schema($dbans);
$dbans->exec("CREATE TABLE IF NOT EXISTS ibo(id INTEGER PRIMARY KEY NOT NULL,mac_address VARCHAR(100),key VARCHAR(100),username VARCHAR(100),password VARCHAR(100),expire_date VARCHAR(100),dns VARCHAR(100),epg_url VARCHAR(100),title VARCHAR(100),url VARCHAR(100), type VARCHAR(100))");
$dbans->exec("CREATE TABLE IF NOT EXISTS playlist(id INTEGER PRIMARY KEY NOT NULL,mac_address VARCHAR(100),url VARCHAR(100),name VARCHAR(100))");
$dbans->exec("CREATE TABLE IF NOT EXISTS theme(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR(100), url VARCHAR(100))");

$mac_count_row = $dbans->query("SELECT COUNT(*) as count FROM ibo")->fetchArray(SQLITE3_ASSOC);
$mac_count = $mac_count_row['count'] ?? 0;

$dbpans = new SQLite3("./api/.anspanel.db");
$resans = $dbpans->query("SELECT * FROM USERS WHERE ID='1'");
$rowans = $resans ? $resans->fetchArray(SQLITE3_ASSOC) : [];
$nameans = $rowans['NAME'] ?? 'Painel UniTV';
$logoans = $rowans['LOGO'] ?? '';
$logoSrc = !empty($logoans) ? $logoans : 'img/logo.png';
$currentPage = basename($_SERVER['PHP_SELF']);

function navItem($href, $icon, $label, $currentPage) {
    $active = $currentPage === basename($href) ? ' active' : '';
    return '<li class="nav-item"><a class="nav-link'.$active.'" href="'.$href.'"><i class="'.$icon.'"></i><span>'.$label.'</span></a></li>';
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=1">
    <meta name="google" content="notranslate">
    <title>MAXUNITV</title>
    <link rel="shortcut icon" href="./img/logo.png" type="image/png">
    <link rel="icon" href="./img/logo.png" type="image/png">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/jquery.datetimepicker.min.css"/>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg-0: #020617;
            --bg-1: #030b2a;
            --bg-2: #071238;
            --card-bg: rgba(8, 17, 51, 0.74);
            --border: rgba(255,255,255,0.08);
            --text-main: #eef2ff;
            --text-soft: #a9b3d4;
            --blue: #2563eb;
            --purple: #7c3aed;
            --red: #ef4444;
            --sidebar-width: 260px;
            --topbar-height: 70px;
        }
        * { box-sizing: border-box; }
        html, body { min-height: 100%; width: 100%; overflow-x: hidden; }
        body {
            margin: 0;
            font-family: 'Plus Jakarta Sans', sans-serif;
            background: linear-gradient(135deg, var(--bg-0) 0%, var(--bg-1) 42%, var(--bg-2) 100%);
            background-attachment: fixed;
            color: var(--text-main);
        }
        
        /* Ajuste do Wrapper e Sidebar para Mobile First */
        #wrapper { display: flex; min-height: 100vh; position: relative; width: 100%; }
        #content-wrapper { flex-grow: 1; width: 100%; min-height: 100vh; transition: margin .3s ease; }
        
        .sidebar {
            width: var(--sidebar-width); 
            min-height: 100vh; 
            position: fixed; 
            top: 0; 
            left: 0; 
            z-index: 1050; 
            padding-top: 16px;
            background: rgba(2,8,35,0.98);
            border-right: 1px solid var(--border); 
            backdrop-filter: blur(14px); 
            overflow-y: auto; 
            overflow-x: hidden; 
            box-shadow: 16px 0 45px rgba(0,0,0,0.32);
            transition: transform .3s ease-in-out;
        }
        
        /* Sidebar Brand */
        .sidebar-brand {
            min-height: 100px; margin: 0 14px 14px; border-radius: 20px; 
            background: rgba(255,255,255,0.03);
            border: 1px solid rgba(255,255,255,0.07); 
            display: flex; flex-direction: column; gap: 8px; 
            align-items: center; justify-content: center; text-decoration: none; padding: 10px;
        }
        .sidebar-brand img { max-width: 60px; max-height: 60px; border-radius: 18px; background: rgba(255,255,255,0.94); padding: 6px; }
        .brand-copy { text-align: center; line-height: 1.2; }
        .brand-copy strong { display: block; font-size: 0.9rem; font-weight: 800; color: #fff; }
        .brand-copy span { color: var(--text-soft); font-size: 0.75rem; }
        
        /* Menu Items */
        .sidebar .nav-item { margin: 0 10px 6px; }
        .sidebar .nav-link { 
            border-radius: 14px; min-height: 46px; display: flex; align-items: center; 
            font-size: 0.85rem; font-weight: 700; color: var(--text-main); 
            padding: 8px 12px; background: rgba(255,255,255,0.02); 
            border: 1px solid rgba(255,255,255,0.05); transition: all .2s ease; 
        }
        .sidebar .nav-link i { width: 20px; margin-right: 10px; color: #ff8a00; text-align: center; }
        .sidebar .nav-link:hover, .sidebar .nav-link.active { 
            transform: translateX(4px); 
            background: linear-gradient(90deg, rgba(37,99,235,0.2), rgba(124,58,237,0.2)); 
            border-color: rgba(96,165,250,0.40); color: #fff; 
        }
        .sidebar-divider { border-top: 1px solid rgba(255,255,255,0.08); margin: 10px 14px; }

        /* Topbar */
        .topbar { 
            min-height: var(--topbar-height); 
            background: rgba(5, 12, 38, 0.85) !important; 
            backdrop-filter: blur(14px); 
            border-bottom: 1px solid rgba(255,255,255,0.08); 
            padding: 0 15px; 
            display: flex;
            align-items: center;
        }
        .topbar .title-box { display: flex; align-items: center; gap: 10px; }
        .mini-logo { width: 40px; height: 40px; border-radius: 12px; object-fit: cover; background: rgba(255,255,255,0.94); padding: 4px; }
        .topbar h5 { color: #fff !important; font-size: 0.9rem; font-weight: 800; margin: 0; line-height: 1.2; }
        .topbar small { color: var(--text-soft) !important; font-size: 0.75rem;}
        
        /* Cards & Forms */
        .card {
            background: var(--card-bg) !important; color: var(--text-main) !important; 
            border: 1px solid rgba(255,255,255,0.08) !important; 
            border-radius: 18px !important; box-shadow: 0 10px 30px rgba(0,0,0,0.15); 
            backdrop-filter: blur(12px); margin-bottom: 1.5rem;
            width: 100%;
        }
        .form-control, .custom-select { 
            background: rgba(255,255,255,0.05) !important; color: #fff !important; 
            border: 1px solid rgba(255,255,255,0.10) !important; border-radius: 12px !important; 
        }
        .btn { border-radius: 12px; font-weight: 600; }
        
        /* Tabelas Responsivas */
        .table-responsive { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 12px;}
        .table { width: 100%; color: var(--text-main); margin-bottom: 0; min-width: 600px; /* Garante que não esprema demais no PC */ }
        .table th, .table td { padding: 0.75rem; vertical-align: middle; font-size: 0.85rem; border-top: 1px solid rgba(255,255,255,0.06); }
        .table thead th { border-bottom: 2px solid rgba(255,255,255,0.08); color: #cad3f6 !important; text-transform: uppercase; font-size: 0.75rem;}

        /* Mobile Overlay Background (Fundo escuro ao abrir menu no mobile) */
        #sidebar-overlay {
            display: none; position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
            background: rgba(0, 0, 0, 0.6); z-index: 1040; backdrop-filter: blur(3px);
        }

        /* Desktop Layout */
        @media (min-width: 992px) {
            #content-wrapper { margin-left: var(--sidebar-width); }
            .sidebar { transform: translateX(0) !important; }
            #sidebarToggleTop { display: none; }
            main[role="main"] { padding: 24px; }
        }

        /* Mobile / Tablet Layout */
        @media (max-width: 991.98px) {
            .sidebar { transform: translateX(-100%); }
            .sidebar.mobile-open { transform: translateX(0); }
            #content-wrapper { margin-left: 0; width: 100%; }
            main[role="main"] { padding: 15px; }
            
            /* Ajustes finos para Mobile */
            .topbar .title-box small { display: none; }
            .card-header h4, .card-header h6 { font-size: 1.1rem; }
            .btn { font-size: 0.8rem; padding: 0.4rem 0.8rem; }
            
            /* Tabela em Mobile - Força rolagem horizontal limpa */
            .table-responsive { margin-bottom: 1rem; border: 1px solid var(--border); }
        }

    </style>
</head>
<body id="page-top">

<div id="sidebar-overlay"></div>

<div id="wrapper">
   <ul class="navbar-nav sidebar sidebar-dark accordion" id="accordionSidebar">
    <a class="sidebar-brand d-flex align-items-center justify-content-center" style="text-decoration: none;" href="users.php">
        <div class="sidebar-brand-icon text-center">
            <img class="img-profile" src="<?php echo htmlspecialchars($logoSrc); ?>" alt="Logo">
            <div class="brand-copy mt-2">
                <strong>MAXUNITV</strong>
                </div>
            </div>
        </a>

        <hr class="sidebar-divider my-0">

        <?php echo navItem('users.php', 'fas fa-user-cog', 'Gerenciar Acessos ('.$mac_count.')', $currentPage); ?>
        <?php echo navItem('mac_search.php', 'fas fa-rss', 'Gerenciamento MAC', $currentPage); ?>
        <?php echo navItem('app_connection.php', 'fas fa-lock', 'Conexão Aplicativo', $currentPage); ?>
        <?php echo navItem('note.php', 'fas fa-bullhorn', 'Notificações & QRcode', $currentPage); ?>
        <?php echo navItem('theme.php', 'fas fa-paint-brush', 'Logotipo & Background', $currentPage); ?>
        <?php echo navItem('splash_slider.php', 'fas fa-images', 'Tela de Carregamento', $currentPage); ?>
        <?php echo navItem('update.php', 'fas fa-sync-alt', 'Atualização APK', $currentPage); ?>
        <?php echo navItem('profile.php', 'fas fa-user', 'Perfil', $currentPage); ?>

        <li class="nav-item mt-2 mb-4">
            <a class="nav-link" href="logout.php">
                <i class="fas fa-sign-out-alt"></i>
                <span>Sair</span>
            </a>
        </li>
    </ul>

    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <nav class="navbar navbar-expand topbar mb-4 static-top">
                <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3 text-light">
                    <i class="fa fa-bars"></i>
                </button>

                <div class="title-box">
                    <img class="mini-logo" src="<?php echo htmlspecialchars($logoSrc); ?>" alt="Logo pequeno">
                    <div>
                        <h5 class="m-0">Painel</small></h5>
                    </div>
                </div>

                <ul class="navbar-nav ml-auto align-items-center">
                    <li class="nav-item dropdown no-arrow mx-1">
                        <a class="nav-link dropdown-toggle btn btn-danger btn-sm" href="logout.php" style="border-radius: 10px; font-weight: bold;">
                            Sair <i class="fas fa-sign-out-alt fa-sm fa-fw ml-1 text-light"></i>
                        </a>
                    </li>
                </ul>
            </nav>