<?php
require_once __DIR__ . "/auth.php";

$scriptName = str_replace('\\', '/', $_SERVER['SCRIPT_NAME'] ?? '');
$basePath = preg_replace('#/controllers/[^/]+\.php$#', '', $scriptName);
$basePath = preg_replace('#/[^/]+\.php$#', '', $basePath);
$basePath = rtrim($basePath, '/');
$BASE_URL = $basePath === '' ? '' : $basePath;

$panelIdentityPath = dirname(__DIR__) . "/api/panel_identity.json";
$panelName = "THEFULL TV";

if (is_file($panelIdentityPath)) {
    $panelIdentityRaw = @file_get_contents($panelIdentityPath);
    $panelIdentity = json_decode((string)$panelIdentityRaw, true);
    if (is_array($panelIdentity) && isset($panelIdentity["name"])) {
        $candidateName = trim((string)$panelIdentity["name"]);
        if ($candidateName !== "") {
            $panelName = $candidateName;
        }
    }
}

$panelLogoFile = dirname(__DIR__) . "/api/img/logo.png";
$panelLogoVersion = is_file($panelLogoFile) ? (string)filemtime($panelLogoFile) : (string)time();
$panelLogoUrl = $BASE_URL . "/api/img/logo.png?v=" . $panelLogoVersion;
?>

<!doctype html>
<html lang="en" class="color-sidebar">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--favicon-->
    <link rel="icon" href="<?= $BASE_URL ?>/assets/images/favicon-32x32.png" type="image/png" />
    <!--plugins-->
    <link href="<?= $BASE_URL ?>/assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
    <link href="<?= $BASE_URL ?>/assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
    <link href="<?= $BASE_URL ?>/assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
    <link href="https://cdn.datatables.net/1.13.7/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
    <!-- loader-->
    <link href="<?= $BASE_URL ?>/assets/css/pace.min.css" rel="stylesheet" />
    <script src="<?= $BASE_URL ?>/assets/js/pace.min.js"></script>
    <!-- Bootstrap CSS -->
    <link href="<?= $BASE_URL ?>/assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="<?= $BASE_URL ?>/assets/css/bootstrap-extended.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">
    <link href="<?= $BASE_URL ?>/assets/css/geral.css" rel="stylesheet">
    <link href="<?= $BASE_URL ?>/assets/css/icons.css" rel="stylesheet">
    <!-- Theme Style CSS -->
    <link rel="stylesheet" href="<?= $BASE_URL ?>/assets/css/tema-escuro.css" />
    <link rel="stylesheet" href="<?= $BASE_URL ?>/assets/css/semi-dark.css" />
    <link rel="stylesheet" href="<?= $BASE_URL ?>/assets/css/cores-do-menu.css" />
    <link rel="stylesheet" href="<?= $BASE_URL ?>/assets/css/apps_styles.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-default/default.css" />
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
    <title><?= htmlspecialchars($panelName, ENT_QUOTES, "UTF-8") ?></title>
    <meta content="<?= htmlspecialchars($panelLogoUrl, ENT_QUOTES, "UTF-8") ?>" property="og:image" />
</head>

<body>
    <!--wrapper-->
    <div class="wrapper">
        <!--sidebar wrapper -->
        <div class="sidebar-wrapper" data-simplebar="true">
            <div class="sidebar-header">
                <div>
                    <img src="<?= htmlspecialchars($panelLogoUrl, ENT_QUOTES, "UTF-8") ?>" class="logo-icon" alt="logo icon">
                </div>
                <div>
                    <h4 class="logo-text"><?= htmlspecialchars($panelName, ENT_QUOTES, "UTF-8") ?></h4>
                </div>
                <div class="toggle-icon ms-auto"><i class='bx bx-menu'></i>
                </div>
            </div>
            <!--navigation-->
            <ul class="metismenu" id="menu">
                <?php if (!is_null($creditos)) : ?>
                    <li class="px-3 py-2 text-white"><small>CREDITOS: (<?php echo $creditos; ?>)</small></li>
                <?php endif; ?>

                <li class="menu-label">Principal</li>
                <li>
                    <a href="<?= $BASE_URL ?>/dashboard.php">
                        <div class="parent-icon"><i class='bx bxs-dashboard'></i></div>
                        <div class="menu-title">Início</div>
                    </a>
                </li>

                <li>
                    <a href="javascript:;" class="has-arrow">
                        <div class="parent-icon"><i class='bx bx-user-circle'></i></div>
                        <div class="menu-title">Clientes</div>
                    </a>
                    <ul>
                        <li><a href="<?= $BASE_URL ?>/users.php"><i class='bx bx-right-arrow-alt'></i> Todos Clientes</a></li>
                        <li><a href="<?= $BASE_URL ?>/users_create.php"><i class='bx bx-right-arrow-alt'></i> Criar Cliente</a></li>
                        <?php if ($nivel_admin == 1) { ?>
                            <li><a href="<?= $BASE_URL ?>/onlines.php"><i class='bx bx-right-arrow-alt'></i> Onlines</a></li>
                        <?php } ?>
                    </ul>
                </li>

                <?php if ($nivel_admin == 1) { ?>
                    <li>
                        <a href="<?= $BASE_URL ?>/mensagem_remota.php">
                            <div class="parent-icon"><i class='bx bx-message-rounded-dots'></i></div>
                            <div class="menu-title">Mensagem Global</div>
                        </a>
                    </li>

                    <li>
                        <a href="<?= $BASE_URL ?>/vencimentos.php">
                            <div class="parent-icon"><i class='bx bx-calendar-exclamation'></i></div>
                            <div class="menu-title">Vencimentos</div>
                        </a>
                    </li>

                    <li class="menu-label">Aplicativo</li>
                    <li>
                        <a href="javascript:;" class="has-arrow">
                            <div class="parent-icon"><i class='bx bx-palette'></i></div>
                            <div class="menu-title">Ajustes</div>
                        </a>
                        <ul>
                            <li><a href="<?= $BASE_URL ?>/qrcode.php"><i class='bx bx-right-arrow-alt'></i> QR Code</a></li>
                            <li><a href="<?= $BASE_URL ?>/logo.php"><i class='bx bx-right-arrow-alt'></i> Logotipo</a></li>
                            <li><a href="<?= $BASE_URL ?>/login_fundo.php"><i class='bx bx-right-arrow-alt'></i>Fundo do login</a></li>
                        </ul>
                    </li>

                    <li class="menu-label">Configurações</li>
                    <li>
                        <a href="javascript:;" class="has-arrow">
                            <div class="parent-icon"><i class='bx bx-cog'></i></div>
                            <div class="menu-title">Sistema</div>
                        </a>
                        <ul>
                            <li><a href="<?= $BASE_URL ?>/dns.php"><i class='bx bx-right-arrow-alt'></i> Trocar DNS</a></li>
                            <li><a href="<?= $BASE_URL ?>/teste.php"><i class='bx bx-right-arrow-alt'></i> Teste auto</a></li>
                         
                            <li><a href="<?= $BASE_URL ?>/app_.php"><i class='bx bx-right-arrow-alt'></i> API</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="<?= $BASE_URL ?>/emuladores.php">
                            <div class="parent-icon"><i class='bx bx-joystick'></i></div>
                            <div class="menu-title">Emuladores</div>
                        </a>
                    </li>
                <?php } ?>

                <li class="menu-label">Conta</li>
                <li>
                    <a href="<?= $BASE_URL ?>/usuario.php">
                        <div class="parent-icon"><i class='bx bx-user'></i></div>
                        <div class="menu-title">Perfil</div>
                    </a>
                </li>

                <?php if ($nivel_admin == 1) { ?>
                    <li>
                        <a href="<?= $BASE_URL ?>/whatsapp_chatbot.php">
                            <div class="parent-icon"><i class='bx bxl-whatsapp'></i></div>
                            <div class="menu-title">Chatbot</div>
                        </a>
                    </li>
                <?php } ?>

                <li>
                    <a href="<?= $BASE_URL ?>/sair.php">
                        <div class="parent-icon"><i class='bx bx-exit'></i></div>
                        <div class="menu-title">Sair</div>
                    </a>
                </li>
            </ul>
        </div>
        <!--end sidebar wrapper -->
        <!--start header -->
        <header>
            <div class="topbar d-flex align-items-center">
                <nav class="navbar navbar-expand">
                    <div class="mobile-toggle-menu"><i class='bx bx-menu'></i>
                    </div>

                    <div class="marquee">
                        <div class="marquee-content">
                            <span id="vencimento-text"></span>
                        </div>
                    </div>
                    <div class="top-menu ms-auto">
                        <ul class="navbar-nav align-items-center">

                            <li class="nav-item dropdown dropdown-large">
                                <a class="nav-link dropdown-toggle dropdown-toggle-nocaret" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"> <i class='bx bx-category'></i>
                                </a>
                                <div class="dropdown-menu dropdown-menu-end">
                                    <div class="row row-cols-3 g-3 p-3">
                                        <!-- Conteúdo dos atalhos -->
                                     
                            </li>
                            <li class="nav-item dropdown dropdown-large">
                                <div class="dropdown-menu dropdown-menu-end">
                                    <div class="header-notifications-list">
                                        <!-- Notificações -->
                                    </div>
                                    <a href="javascript:;">
                                        <div class="text-center msg-footer">View All Notifications</div>
                                    </a>
                                </div>
                            </li>
                            <li class="nav-item dropdown dropdown-large">
                                <div class="dropdown-menu dropdown-menu-end">
                                    <div class="header-message-list">
                                        <!-- Mensagens -->
                                    </div>
                                    <a href="javascript:;">
                                        <div class="text-center msg-footer">View All Messages</div>
                                    </a>
                                </div>
                            </li>
                        </ul>
                    </div>

                </nav>
            </div>
        </header>

        <script>
            document.addEventListener("DOMContentLoaded", function() {
                var vencimentoText = document.getElementById('vencimento-text');

                <?php
                $tipoUser = (string)($_SESSION["tipo_user"] ?? "");
                $isAdminHeader = ($tipoUser === "admin" || $nivel_admin == 1);

                if ($isAdminHeader) {
                    echo 'vencimentoText.innerHTML = "";';
                } else {
                    if (!empty($vencimento)) {
                        $dataAtual = new DateTime();
                        $dataVencimento = new DateTime($vencimento);
                        $diasRestantes = (int)$dataAtual->diff($dataVencimento)->format('%r%a');

                        if ($diasRestantes <= 0) {
                            echo 'vencimentoText.innerHTML = "SEU PAINEL REVENDA ESTÁ VENCIDO!";';
                        } else {
                            echo 'vencimentoText.innerHTML = "PAINEL REVENDA VENCE EM ' . $diasRestantes . ' DIAS!";';
                        }
                    } else {
                        echo 'vencimentoText.innerHTML = "Vencimento da revenda não definido.";';
                    }
                }
                ?>
            });
        </script>
        <script>
document.addEventListener("DOMContentLoaded", function () {
    const wrapper = document.querySelector(".wrapper");
    const sidebarToggle = document.querySelector(".toggle-icon");
    const mobileToggle = document.querySelector(".mobile-toggle-menu");

    function toggleMenu() {
        if (!wrapper) return;
        wrapper.classList.toggle("toggled");
    }

    if (sidebarToggle) {
        sidebarToggle.addEventListener("click", function (e) {
            e.preventDefault();
            toggleMenu();
        });
    }

    if (mobileToggle) {
        mobileToggle.addEventListener("click", function (e) {
            e.preventDefault();
            toggleMenu();
        });
    }
});
</script>
        <script>
            $(document).ready(function() {
                $('#menu').metisMenu();
            });
        </script>
        <!--end header -->
        <div class="page-wrapper">
            <div class="page-content">
                <!-- Conteúdo da página -->
