<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start(); // Inicia a sessão apenas se não estiver iniciada
}
ini_set("display_errors", 1);
ini_set("display_startup_errors", 0);
error_reporting(32767);

if (!isset($_SESSION['id'])) {
    header("Location: login.php");
    exit();
}

$id = $_SESSION['id'];
$isAdmin = $_SESSION['admin'];

$dbans = new SQLite3("./api/.ansdb.db");
$adb = new SQLite3('./api/.adb.db');

$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), id_user INT)");
$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))");

$res = $dbans->query("SELECT * FROM theme");
$rows = $dbans->query("SELECT COUNT(*) as count FROM theme");
$row = $rows->fetchArray();
$numRows = $row["count"];

$protocol = (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off" ? "https" : "http");
$baseUrl = $protocol . "://" . $_SERVER["HTTP_HOST"] . dirname($_SERVER["PHP_SELF"]);
$HOSTa = $baseUrl . "/img/red.jpg";
$HOSTb = $baseUrl . "/img/blue.jpg";
$HOSTc = $baseUrl . "/img/green.jpg";
$HOSTa1 = $baseUrl . "/img/g1.gif";

$mac_count = $dbans->query("SELECT COUNT(*) as count FROM ibo WHERE id_user = $id")->fetchArray()["count"];
$expired_mac_count = $dbans->query("SELECT COUNT(*) as count FROM ibo WHERE id_user = $id AND (active = 0 OR expire_date < date('today'))")->fetchArray()["count"];

$dbpans = new SQLite3("./api/.anspanel.db");
$resans = $dbpans->query("SELECT * FROM USERS WHERE ID='1'");
$rowans = $resans->fetchArray();
$nameans = $rowans["NAME"];
$logoans = $rowans["LOGO"];

$jsondata111 = file_get_contents("./includes/ansibo.json");
$json111 = json_decode($jsondata111, true);
$col1 = $json111["info"];
$col2 = $col1["aa"];
$col3 = $col2;

// Caminho do JSON (uma pasta acima)
$jsonFile = __DIR__ . '/../img/logo/logo_filenames.json';

// Inicializa variável da logo
$logoans = null;

// Lê o JSON e pega a primeira logo disponível
if (file_exists($jsonFile)) {
    $jsonData = file_get_contents($jsonFile);
    $logos = json_decode($jsonData, true);
    if (!empty($logos) && isset($logos[0]['ImageName'])) {
        $logoans = str_replace('\\', '/', $logos[0]['ImageName']); // Ajusta barras
    }
}

// Fallback caso não exista logo no JSON
$logoSrc = $logoans ?? 'https://lcan.thefullnet.com.br/img_custom/logo/678ed8e771850.png';
?>

<!DOCTYPE html>
<html lang="en">
<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">
    <meta name="description" content="">
    <meta name="author" content="">
    <meta name="google" content="notranslate">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <title>PAINEL TV</title>
    <link rel="shortcut icon" href="./img2/logo.png?ver=<?= time(); ?>" type="image/png">
    <link rel="icon" href="./img2/logo.png?ver=<?= time(); ?>" type="image/png">
    <!-- Custom styles for this template-->
    <link href="css/sb-admin-<?= $col2 ?>.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/jquery.datetimepicker.min.css">
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

    
   

</head>
<body id="page-top" class="sidebar-toggled">


<!-- Page Wrapper -->
<div id="wrapper">

    <!-- Sidebar -->
   <ul class="navbar-nav bg-gradient-header-adm-rev sidebar sidebar-dark accordion toggled" id="accordionSidebar">


        <?php if ($logoans != NULL): ?>
            <!-- Sidebar - Brand -->
          <!-- Sidebar - Brand -->
<a class="sidebar-brand d-flex align-items-center justify-content-center" href="users.php">
    <div class="sidebar-brand-icon">
        <img class="img-profile rounded-circle" width="65px" src="<?= $logoSrc ?>">
    </div>
</a>
        <?php else: ?>
            <!-- Sidebar - Brand -->
           <!-- Sidebar - Brand -->
<a class="sidebar-brand d-flex align-items-center justify-content-center" href="users.php">
    <div class="sidebar-brand-icon">
        <img class="img-profile rounded-circle" width="65px" src="<?= $logoSrc ?>">
    </div>
</a>
        <?php endif; ?>

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

        <!-- HEADER ADMIN -->
        <?php if ($isAdmin) { ?>
        <span class="text-menu-header">Menu</span>
        <li class="nav-item no-margin">
              <a class="nav-link" href="home.php">
                    <i class="fas fa-tachometer-alt"></i>
                    <span>Dashboard</span></a>
           
                <a class="nav-link" href="all_users.php">
                    <i class="fas fa-fw fa-users"></i>
                    <span>Clientes</span></a>
                      <a class="nav-link" href="stores.php">
                    <i class="fas fa-fw fa-users"></i>
                    <span>Revendas</span></a>
            </li>

         
                <div id="collapsePages1" class="collapse" aria-labelledby="headingPages" data-parent="#accordionSidebar">
                    <div class="bg-white py-2 collapse-inner rounded">
                        <h6 class="collapse-header">Área do Revenda:</h6>
                        <a class="collapse-item" href="stores.php"><i class="fas fa-fw fa-infinity"></i><span> Revendas Premium</span></a>
                        <a class="collapse-item" href="stores_mac.php"><i class="fas fa-fw fa-hand-holding-usd"></i><span> Revendas MACs</span></a>
                    </div>
                </div>
            </li>

          <span class="text-menu-header">Design</span>
<li class="nav-item no-margin">
    <a class="nav-link" href="autoads.php">
        <i class="fa fa-adjust"></i>
        <span>Personalizar App</span>
    </a>
</li>


            <span class="text-menu-header">Ajustes</span>
            <li class="nav-item no-margin">
                <a class="nav-link" href="pagamentos.php">
                    <i class="fas fa-credit-card"></i>
                    <span>Pagamentos</span></a>
                <a class="nav-link" href="chatbot.php">
                    <i class="fas fa-robot"></i>
                    <span>Teste Auto</span></a>

            

              


                

                <a class="nav-link" href="qrcode.php">
                    <i class="fas fa-qrcode"></i>
                    <span>QR Code do Suporte</span></a>

                <a class="nav-link" href="profile.php">
                    <i class="fas fa-fw fa-user"></i>
                    <span>Perfil</span></a>
            <a class="nav-link" href="logout.php">
        <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-red-400" style="color: #FF0000;"></i>
        <span>Sair</span></a>
            </li>
<?php } ?>
<?php if ($isAdmin) { ?>
    <!-- Código do painel do admin -->
<?php } else { // Se não for admin, então é revenda ?>
    <li class="nav-item no-margin">
        <a class="nav-link" href="users.php">
            <i class="fas fa-home" style="color: #FFFFFF;"></i>
            <span>Inicio</span></a>
    
        <a class="nav-link" href="replace.php">
                <i class="fas fa-fw fa-globe"></i>
                <span>Atualizar Dominio</span></a>
                
    <a class="nav-link" href="logout.php">
        <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-red-400" style="color: #FF0000;"></i>
        <span>Sair</span></a>
        </li>
        
<?php } ?>


        <!-- Divider -->
        <hr class="sidebar-divider d-none d-md-block">

        <!-- Sidebar Toggler (Sidebar) -->
        <div class="text-center d-none d-md-inline">
            <button class="rounded-circle border-0" id="sidebarToggle"></button>
        </div>
        
        <footer class="sticky-footer">
            <div class="copyright text-center">
                <span></span>
            </div>
        </footer>
    </ul>
    <!-- End of Sidebar -->

    <!-- Content Wrapper -->
    <div id="content-wrapper" class="d-flex flex-column">

        <!-- Main Content -->
        <div id="content">

            <!-- Topbar -->
            <nav class="navbar navbar-expand navbar-light topbar mb-4 static-top shadow">

                <!-- Sidebar Toggle (Topbar) -->
                <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                    <i class="fa fa-bars"></i>
                </button>

                <?php
                echo "<div><h5 class=\"m-0 text-primary\">" . $nameans . " </br></h5></div>" . "\n";
                echo "\n          <!-- Topbar Navbar -->\n          <ul class=\"navbar-nav ml-auto\">\n\n\n            <!-- Nav Item - Theme -->\n            <!-- <li class=\"nav-item no-margin dropdown no-arrow mx-1\">\n            </li> -->\n            <div class=\"topbar-divider d-none d-sm-block\"></div>\n\n            <!-- Nav Item - Logout -->\n            <li class=\"nav-item no-margin3 dropdown no-arrow mx-1\">\n              <a class=\"nav-link dropdown-toggle\" href=\"logout.php\"><span class=\"badge badge-danger\">Sair</span>\n                <i class=\"fas fa-sign-out-alt fa-sm fa-fw mr-2 text-red-400\"></i>\n              </a>\n            </li>\n\n          </ul>\n\n        </nav>\n        <!-- End of Topbar -->\n\n";
                ?>