<?php
session_start();
require_once 'includes/db.php';

// Verificação de segurança
if (!isset($_SESSION['logged_in_fxtream']) || $_SESSION['logged_in_fxtream'] !== true) {
    header('Location: index.php');
    exit;
}

// === BUSCA NO BANCO DE DADOS (CORRIGIDO PELAS FOTOS) ===
try {
    // 1. Canais (Tabela: channels | Colunas: name, logo_url)
    $sql_channels = "SELECT id, name, logo_url FROM channels ORDER BY id DESC LIMIT 20";
    $recent_channels = $pdo->query($sql_channels)->fetchAll(PDO::FETCH_ASSOC);

    // 2. Filmes (Tabela: movies | Colunas: name, cover_url)
    $sql_movies = "SELECT id, name, cover_url FROM movies ORDER BY id DESC LIMIT 20";
    $recent_movies = $pdo->query($sql_movies)->fetchAll(PDO::FETCH_ASSOC);

    // 3. Séries (Tabela: series | Colunas: name, cover_url)
    $sql_series = "SELECT id, name, cover_url FROM series ORDER BY id DESC LIMIT 20"; 
    $recent_series = $pdo->query($sql_series)->fetchAll(PDO::FETCH_ASSOC);

    // 4. Episódios (Tabela: episodes | Colunas: name)
    $sql_episodes = "SELECT id, name FROM episodes ORDER BY id DESC LIMIT 20";
    $recent_episodes = $pdo->query($sql_episodes)->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    // Se der erro, define arrays vazios para não quebrar a página
    $recent_channels = []; $recent_movies = []; $recent_series = []; $recent_episodes = [];
    // Descomente a linha abaixo se quiser ver o erro na tela para debug:
    // echo "Erro SQL: " . $e->getMessage(); 
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
    <title>Últimos Conteúdos</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://vendedorp2p.com/views/_css/bootstrap/bootstrap.min.css">
    <link rel="stylesheet" href="https://vendedorp2p.com/views/_css/style.css">
    <link rel="stylesheet" href="https://vendedorp2p.com/views/_fonts/font-awesome/css/all.min.css">
    <link rel="stylesheet" href="https://vendedorp2p.com/views/_css/dark-theme.css">
    
    <style>
        body { background-color: #1a1c23; color: #fff; }
        
        /* Botões de Navegação (Abas) */
        .nav-pills .nav-link {
            background-color: #232730;
            color: #fff;
            border-radius: 5px;
            margin-right: 10px;
            border: 1px solid #3a3f4b;
            text-transform: uppercase;
            font-weight: 600;
            padding: 15px 20px;
        }
        .nav-pills .nav-link.active, .nav-pills .nav-link:hover {
            background-color: #007bff; /* Azul destaque */
            color: #fff;
            border-color: #007bff;
        }

        /* Lista de Conteúdo */
        .content-list {
            background-color: #232730;
            border-radius: 8px;
            padding: 20px;
            margin-top: 20px;
        }
        .content-item {
            display: flex;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #3a3f4b;
        }
        .content-item:last-child { border-bottom: none; }
        
        .item-icon {
            width: 40px;
            height: 40px;
            background: #333;
            border-radius: 5px; /* Quadrado arredondado para imagens */
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;
            overflow: hidden;
            font-size: 18px;
        }
        .item-icon img { width: 100%; height: 100%; object-fit: cover; }
        .item-name { font-size: 0.95rem; font-weight: 500; }
        .badge-new { font-size: 0.7rem; margin-left: auto; }
        
        /* Visibilidade do Menu Lateral */
        @media (min-width: 992px) {
            #sidebar { display: block !important; width: 240px; position: fixed; top: 0; left: 0; height: 100vh; overflow-y: auto; }
            .right-column { margin-left: 240px; width: calc(100% - 240px); }
            .btn-voltar-mobile { display: none; }
        }
        @media (max-width: 991px) {
            #sidebar { display: none !important; }
            .right-column { margin-left: 0 !important; width: 100% !important; }
            .btn-voltar-mobile { display: inline-block; margin-bottom: 15px; background: #333; color: #fff; padding: 8px 15px; border-radius: 5px; text-decoration: none; }
        }
    </style>
</head>

<body>

<div class="container-fluid p-0"> 
    <div class="row no-gutters"> 
        
        <?php include 'includes/sidebar.php'; ?>

        <div class="right-column">
            <main class="main-content p-4">
                
                <a href="dashboard.php" class="btn-voltar-mobile"><i class="fas fa-arrow-left"></i> Voltar</a>

                <div class="mb-2">
                    <h3 class="text-white">INFORMAÇÕES</h3>
                    <p class="text-muted small">Lista em ordem dos últimos conteúdos adicionados.</p>
                </div>

                <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link active" id="pills-canais-tab" data-toggle="pill" href="#pills-canais" role="tab"><i class="fas fa-tv mr-2"></i> Novos Canais</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="pills-filmes-tab" data-toggle="pill" href="#pills-filmes" role="tab"><i class="fas fa-film mr-2"></i> Novos Filmes</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="pills-series-tab" data-toggle="pill" href="#pills-series" role="tab"><i class="fas fa-video mr-2"></i> Novas Séries</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="pills-eps-tab" data-toggle="pill" href="#pills-eps" role="tab"><i class="fas fa-play-circle mr-2"></i> Novos Episódios</a>
                    </li>
                </ul>

                <div class="tab-content" id="pills-tabContent">
                    
                    <div class="tab-pane fade show active" id="pills-canais" role="tabpanel">
                        <div class="content-list">
                            <?php if(empty($recent_channels)): ?>
                                <p class="text-muted text-center">Nenhum canal encontrado.</p>
                            <?php else: ?>
                                <?php foreach($recent_channels as $item): ?>
                                    <div class="content-item">
                                        <div class="item-icon text-info">
                                            <?php if(!empty($item['logo_url'])): ?>
                                                <img src="<?php echo htmlspecialchars($item['logo_url']); ?>" onerror="this.style.display='none'">
                                            <?php else: ?>
                                                <i class="fas fa-tv"></i>
                                            <?php endif; ?>
                                        </div>
                                        <span class="item-name"><?php echo htmlspecialchars($item['name']); ?></span>
                                        <span class="badge badge-success badge-new">NOVO</span>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="pills-filmes" role="tabpanel">
                        <div class="content-list">
                            <?php if(empty($recent_movies)): ?>
                                <p class="text-muted text-center">Nenhum filme encontrado.</p>
                            <?php else: ?>
                                <?php foreach($recent_movies as $item): ?>
                                    <div class="content-item">
                                        <div class="item-icon text-warning">
                                            <?php if(!empty($item['cover_url'])): ?>
                                                <img src="<?php echo htmlspecialchars($item['cover_url']); ?>" onerror="this.style.display='none'">
                                            <?php else: ?>
                                                <i class="fas fa-film"></i>
                                            <?php endif; ?>
                                        </div>
                                        <span class="item-name"><?php echo htmlspecialchars($item['name']); ?></span>
                                        <span class="badge badge-primary badge-new">HD/FHD</span>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="pills-series" role="tabpanel">
                        <div class="content-list">
                            <?php if(empty($recent_series)): ?>
                                <p class="text-muted text-center">Nenhuma série encontrada.</p>
                            <?php else: ?>
                                <?php foreach($recent_series as $item): ?>
                                    <div class="content-item">
                                        <div class="item-icon text-danger">
                                            <?php if(!empty($item['cover_url'])): ?>
                                                <img src="<?php echo htmlspecialchars($item['cover_url']); ?>" onerror="this.style.display='none'">
                                            <?php else: ?>
                                                <i class="fas fa-video"></i>
                                            <?php endif; ?>
                                        </div>
                                        <span class="item-name"><?php echo htmlspecialchars($item['name']); ?></span>
                                        <span class="badge badge-danger badge-new">SÉRIE</span>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="pills-eps" role="tabpanel">
                        <div class="content-list">
                            <?php if(empty($recent_episodes)): ?>
                                <p class="text-muted text-center">Nenhum episódio recente.</p>
                            <?php else: ?>
                                <?php foreach($recent_episodes as $item): ?>
                                    <div class="content-item">
                                        <div class="item-icon text-success"><i class="fas fa-play"></i></div>
                                        <span class="item-name"><?php echo htmlspecialchars($item['name']); ?></span>
                                        <span class="badge badge-light badge-new text-dark">EP</span>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>

                </div> </main>
        </div>
    </div>
</div>

<script src="https://vendedorp2p.com/views/_js/jquery/jquery-3.1.1.min.js"></script>
<script src="https://vendedorp2p.com/views/_js/bootstrap/popper.min.js"></script>
<script src="https://vendedorp2p.com/views/_js/bootstrap/bootstrap.min.js"></script>

</body>
</html>