<?php
//<----------------------------------------------------------->
//<-------------------COPIA AI BRUXÃO------------------------->
//<-------------CONTATO: https://t.me/eonbry------------------>
//<-------------DEVELOPER: EMANUEL NASCIMENTO----------------->
//<----------------------------------------------------------->

include ('includes/header.php');

// Configurações
$table_name = "ads";
$base_file = basename($_SERVER["SCRIPT_NAME"]);

// Criar tabela se não existir
$adb->exec("CREATE TABLE IF NOT EXISTS {$table_name}(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(100), url TEXT, owner TEXT)");

// Variáveis para filtragem
$filter_type = isset($_GET['filter']) ? $_GET['filter'] : 'all'; // all, images, videos
$filter_active = $filter_type != 'all';

// Buscar dados com base no filtro
if ($filter_type == 'images') {
    $res = $adb->query("SELECT * FROM {$table_name} WHERE url LIKE '%.jpg%' OR url LIKE '%.jpeg%' OR url LIKE '%.png%' OR url LIKE '%.gif%' OR url LIKE '%.svg%'");
    $page_title = "Imagens";
    $page_icon = "fa-image";
} elseif ($filter_type == 'videos') {
    $res = $adb->query("SELECT * FROM {$table_name} WHERE url LIKE '%.mp4%' OR url LIKE '%.webm%' OR url LIKE '%.ogg%'");
    $page_title = "Vídeos";
    $page_icon = "fa-video";
} else {
    $res = $adb->query("SELECT * FROM {$table_name}");
    $page_title = "Todos os Anúncios";
    $page_icon = "fa-list-alt";
}

// Contar totais
$countResult = $adb->query("SELECT COUNT(*) as total FROM {$table_name}");
$countRow = $countResult->fetchArray();
$totalAds = $countRow[0];

$videoResult = $adb->query("SELECT COUNT(*) as total FROM {$table_name} WHERE url LIKE '%.mp4%' OR url LIKE '%.webm%' OR url LIKE '%.ogg%'");
$videoRow = $videoResult->fetchArray();
$totalVideos = $videoRow[0];

$imageResult = $adb->query("SELECT COUNT(*) as total FROM {$table_name} WHERE url LIKE '%.jpg%' OR url LIKE '%.jpeg%' OR url LIKE '%.png%' OR url LIKE '%.gif%' OR url LIKE '%.svg%'");
$imageRow = $imageResult->fetchArray();
$totalImages = $imageRow[0];

?>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">

<style>
:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --secondary: #10b981;
    --accent: #f59e0b;
    --danger: #ef4444;
    --dark-bg: #0f172a;
    --dark-surface: #1e293b;
    --dark-surface-light: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border: rgba(255, 255, 255, 0.1);
    --glow: 0 0 20px rgba(99, 102, 241, 0.3);
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.05);
}

.neon-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

.neon-header {
    text-align: center;
    margin-bottom: 40px;
    position: relative;
    padding-bottom: 20px;
}

.neon-header::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 25%;
    right: 25%;
    height: 3px;
    background: linear-gradient(90deg, transparent, var(--primary), var(--secondary), transparent);
    border-radius: 3px;
}

.neon-title {
    font-size: 2.8rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 10px;
}

.neon-card {
    background: linear-gradient(145deg, rgba(30, 41, 59, 0.8), rgba(15, 23, 42, 0.9));
    backdrop-filter: blur(20px);
    border-radius: 24px;
    border: 1px solid var(--glass-border);
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    padding: 30px;
    margin-bottom: 30px;
    position: relative;
}

.neon-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--primary), var(--secondary));
    border-radius: 24px 24px 0 0;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 20px;
    margin-bottom: 40px;
}

.stat-card {
    background: linear-gradient(145deg, rgba(30, 41, 59, 0.7), rgba(15, 23, 42, 0.8));
    backdrop-filter: blur(15px);
    border-radius: 20px;
    padding: 25px;
    border: 1px solid var(--glass-border);
    text-align: center;
    transition: all 0.3s ease;
    cursor: pointer;
    position: relative;
    text-decoration: none;
    display: block;
    color: inherit;
}

.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    background: linear-gradient(145deg, #ffffff, #f0f0f0);
    text-decoration: none;
    color: inherit;
}

.stat-card:hover .stat-number {
    -webkit-text-fill-color: #000000 !important;
    color: #000000 !important;
    background: none !important;
}

.stat-card:hover .stat-label {
    color: #333333 !important;
}

.stat-card.active {
    border-color: var(--primary);
    box-shadow: 0 0 20px rgba(99, 102, 241, 0.3);
}

.stat-card.active::before {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    border-radius: 22px;
    z-index: -1;
}

.stat-number {
    font-size: 2.5rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    line-height: 1;
    margin-bottom: 5px;
    transition: all 0.3s ease;
}

.stat-label {
    color: var(--text-secondary);
    font-size: 0.9rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: all 0.3s ease;
}

.neon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 14px 28px;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    border: none;
    border-radius: 14px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    text-decoration: none;
    box-shadow: var(--glow);
}

.neon-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 10px 25px rgba(99, 102, 241, 0.4);
    color: white;
    text-decoration: none;
}

.neon-btn.secondary {
    background: linear-gradient(135deg, var(--dark-surface-light), var(--dark-surface));
    border: 1px solid var(--border);
}

.neon-btn.secondary:hover {
    background: linear-gradient(135deg, var(--dark-surface), var(--dark-surface-light));
}

.neon-input {
    width: 100%;
    padding: 16px 20px;
    background: rgba(15, 23, 42, 0.6);
    border: 2px solid rgba(99, 102, 241, 0.2);
    border-radius: 14px;
    color: var(--text-primary);
    font-size: 15px;
    font-weight: 500;
    transition: all 0.3s ease;
    outline: none;
}

.neon-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
    background: rgba(15, 23, 42, 0.8);
}

.neon-table-container {
    background: linear-gradient(145deg, rgba(30, 41, 59, 0.8), rgba(15, 23, 42, 0.9));
    backdrop-filter: blur(20px);
    border-radius: 20px;
    border: 1px solid var(--glass-border);
    overflow: hidden;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

.neon-table {
    width: 100%;
    border-collapse: collapse;
    background: transparent;
}

.neon-table thead {
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.1), rgba(16, 185, 129, 0.1));
    border-bottom: 1px solid var(--border);
}

.neon-table th {
    padding: 18px 20px;
    text-align: left;
    color: var(--text-primary);
    font-weight: 700;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 1px;
    border: none;
}

.neon-table td {
    padding: 18px 20px;
    color: var(--text-secondary);
    border-top: 1px solid var(--border);
    vertical-align: middle;
}

.neon-table tbody tr:hover {
    background: rgba(99, 102, 241, 0.05);
}

.preview-box-neon {
    width: 90px;
    height: 60px;
    border-radius: 12px;
    overflow: hidden;
    position: relative;
    border: 2px solid rgba(99, 102, 241, 0.2);
    background: rgba(15, 23, 42, 0.6);
    cursor: pointer;
}

.preview-media-neon {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.actions-cell {
    display: flex;
    gap: 10px;
    align-items: center;
}

.action-btn {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 16px;
    text-decoration: none;
}

.action-btn:hover {
    transform: translateY(-2px);
    color: var(--primary);
    border-color: var(--primary);
    box-shadow: 0 0 10px rgba(99, 102, 241, 0.2);
}

.action-btn.action-delete {
    background: rgba(239, 68, 68, 0.1);
    color: var(--danger);
    border-color: rgba(239, 68, 68, 0.3);
}

.action-btn.action-delete:hover {
    color: white;
    background: var(--danger);
    border-color: var(--danger);
    box-shadow: 0 0 15px rgba(239, 68, 68, 0.4);
}

.action-btn.action-edit {
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary);
    border-color: rgba(99, 102, 241, 0.3);
}

.action-btn.action-edit:hover {
    color: white;
    background: var(--primary);
    border-color: var(--primary);
    box-shadow: 0 0 15px rgba(99, 102, 241, 0.4);
}

.d-flex {
    display: flex;
}

.justify-content-between {
    justify-content: space-between;
}

.align-items-center {
    align-items: center;
}

.mb-4 {
    margin-bottom: 1.5rem;
}

.mt-2 {
    margin-top: 0.5rem;
}

.mt-4 {
    margin-top: 1.5rem;
}

.me-2 {
    margin-right: 0.5rem;
}

.text-center {
    text-align: center;
}

.active-filter-indicator {
    display: flex;
    align-items: center;
    gap: 5px;
}

.active-filter-indicator strong {
    font-weight: 700;
}

.no-results {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

.no-results i {
    font-size: 60px;
    margin-bottom: 20px;
    color: var(--text-muted);
}

.no-results h3 {
    color: var(--text-primary);
    margin-bottom: 10px;
}

/*RESPONSIVIDADE PRA CELULAR*/

/* Para tablets e dispositivos médios */
@media (max-width: 1024px) {
    .neon-container {
        padding: 15px;
    }
    
    .neon-title {
        font-size: 2.2rem;
    }
    
    .neon-card {
        padding: 25px;
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 15px;
    }
    
    .neon-table-container {
        overflow-x: auto;
    }
    
    .neon-table {
        min-width: 800px;
    }
    
    .neon-table th,
    .neon-table td {
        padding: 14px 16px;
        font-size: 12px;
    }
    
    .preview-box-neon {
        width: 70px;
        height: 50px;
    }
    
    .action-btn {
        width: 35px;
        height: 35px;
        font-size: 14px;
    }
}

/* Para celulares */
@media (max-width: 768px) {
    .neon-title {
        font-size: 1.8rem;
    }
    
    .neon-header {
        margin-bottom: 30px;
        padding-bottom: 15px;
    }
    
    .neon-card {
        padding: 20px;
        border-radius: 20px;
    }
    
    .stats-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    .stat-card {
        padding: 20px;
    }
    
    .stat-number {
        font-size: 2rem;
    }
    
    .d-flex {
        flex-direction: column;
        gap: 15px;
        align-items: stretch !important;
    }
    
    .d-flex.justify-content-between {
        flex-direction: column;
        gap: 15px;
    }
    
    .neon-btn {
        width: 100%;
        justify-content: center;
        padding: 12px 20px;
    }
    
    .neon-input {
        padding: 14px 16px;
    }
    
    /* Ajustes para tabela em mobile */
    .neon-table th:nth-child(3),
    .neon-table td:nth-child(3),
    .neon-table th:nth-child(4),
    .neon-table td:nth-child(4) {
        display: none;
    }
    
    .neon-table th,
    .neon-table td {
        padding: 10px 12px;
        font-size: 11px;
    }
    
    .actions-cell {
        flex-direction: column;
        gap: 8px;
    }
    
    .action-btn {
        width: 32px;
        height: 32px;
    }
    
    /* Ajuste para preview em mobile */
    .preview-box-neon {
        width: 60px;
        height: 40px;
    }
    
    /* Formulário de criação em mobile */
    .form-group label {
        font-size: 14px;
    }
    
    /* Mensagens de filtro ativo */
    .active-filter-indicator {
        flex-direction: column;
        align-items: flex-start;
        gap: 5px;
    }
}

/* Para celulares pequenos */
@media (max-width: 480px) {
    .neon-title {
        font-size: 1.5rem;
    }
    
    .neon-container {
        padding: 10px;
    }
    
    .neon-card {
        padding: 15px;
        border-radius: 16px;
    }
    
    .stat-card {
        padding: 15px;
    }
    
    .stat-number {
        font-size: 1.8rem;
    }
    
    .stat-label {
        font-size: 0.8rem;
    }
    
    /* Ocultar mais colunas em telas muito pequenas */
    .neon-table th:nth-child(5),
    .neon-table td:nth-child(5) {
        display: none;
    }
    
    /* Ajustes para botões */
    .neon-btn {
        font-size: 14px;
        padding: 10px 16px;
    }
    
    /* Modal SweetAlert em mobile */
    .swal2-popup.neon-card {
        width: 90% !important;
        margin: 0 5% !important;
        padding: 15px !important;
    }
    
    .swal2-title.neon-title {
        font-size: 1.3rem !important;
    }
    
    /* Ajuste para formulários */
    .text-center.mt-4 {
        margin-top: 20px;
    }
}

/* Para orientação paisagem em celulares */
@media (max-height: 500px) and (orientation: landscape) {
    .neon-header {
        margin-bottom: 20px;
    }
    
    .stats-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 10px;
        margin-bottom: 20px;
    }
    
    .stat-card {
        padding: 15px;
    }
    
    .neon-table th,
    .neon-table td {
        padding: 8px 10px;
    }
}

/* Melhorias para inputs em mobile */
@media (max-width: 768px) {
    input, textarea, select {
        font-size: 16px !important; /* Evita zoom em iOS */
    }
    
    .neon-input {
        font-size: 16px;
    }
}

/* Scroll suave para tabelas */
.neon-table-container {
    -webkit-overflow-scrolling: touch; /* Scroll suave em iOS */
}

/* Melhorias para toques em mobile */
.action-btn, 
.neon-btn, 
.preview-box-neon {
    min-height: 44px; /* Tamanho mínimo para toque */
    min-width: 44px;
}

/* Ajustes para o modal de preview em mobile */
@media (max-width: 768px) {
    .swal2-popup {
        width: 95% !important;
        max-width: 95% !important;
    }
    
    .swal2-html-container {
        max-height: 50vh !important;
        overflow-y: auto !important;
    }
}

/* Ajuste para os cards de estatísticas em mobile */
@media (max-width: 768px) {
    .stats-grid a {
        text-decoration: none;
    }
    
    .stat-card:hover {
        transform: translateY(-2px);
    }
}
</style>

<div class="neon-container">
    
    <div class="neon-header">
        <h1 class="neon-title">Gerenciamento de Anúncios</h1>
    </div>
    
    <!-- Stats Grid -->
    <div class="stats-grid">
        <a href="<?php echo $base_file ?>?filter=all" class="stat-card <?= $filter_type == 'all' ? 'active' : '' ?>">
            <div class="stat-number"><?= $totalAds ?></div>
            <div class="stat-label">Total</div>
        </a>
        <a href="<?php echo $base_file ?>?filter=images" class="stat-card <?= $filter_type == 'images' ? 'active' : '' ?>">
            <div class="stat-number"><?= $totalImages ?></div>
            <div class="stat-label">Imagens</div>
        </a>
        <a href="<?php echo $base_file ?>?filter=videos" class="stat-card <?= $filter_type == 'videos' ? 'active' : '' ?>">
            <div class="stat-number"><?= $totalVideos ?></div>
            <div class="stat-label">Vídeos</div>
        </a>
        <a href="create_ads.php?filter=<?= $filter_type ?>" class="stat-card">
            <div class="stat-number"><i class="fas fa-plus"></i></div>
            <div class="stat-label">Novo</div>
        </a>
    </div>
    
    <!-- Main Table -->
    <div class="neon-card">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 style="color: var(--text-primary); margin: 0;">
                    <i class="fas <?= $page_icon ?> me-2" style="color: var(--primary);"></i>
                    <?= $page_title ?>
                </h2>
                <?php if ($filter_active): ?>
                <div class="active-filter-indicator mt-2">
                    <i class="fas fa-filter" style="color: var(--primary);"></i>
                    <span style="color: var(--text-secondary); font-size: 14px;">
                        Filtro ativo: 
                        <strong style="color: var(--primary);">
                            <?= $filter_type == 'images' ? 'Imagens' : ($filter_type == 'videos' ? 'Vídeos' : 'Todos') ?>
                        </strong>
                    </span>
                </div>
                <?php endif; ?>
            </div>
            <div style="display: flex; gap: 10px;">
                <?php if ($filter_active): ?>
                <a href="<?php echo $base_file ?>?filter=all" class="neon-btn secondary">
                    <i class="fas fa-times"></i> Limpar Filtro
                </a>
                <?php endif; ?>
                <a href="create_ads.php?filter=<?= $filter_type ?>" class="neon-btn">
                    <i class="fas fa-plus"></i> Novo Anúncio
                </a>
            </div>
        </div>
        
        <?php
        // Verificar se há resultados
        $hasResults = false;
        $tempRes = $adb->query($filter_type == 'images' ? 
            "SELECT COUNT(*) FROM {$table_name} WHERE url LIKE '%.jpg%' OR url LIKE '%.jpeg%' OR url LIKE '%.png%' OR url LIKE '%.gif%' OR url LIKE '%.svg%'" : 
            ($filter_type == 'videos' ? 
                "SELECT COUNT(*) FROM {$table_name} WHERE url LIKE '%.mp4%' OR url LIKE '%.webm%' OR url LIKE '%.ogg%'" : 
                "SELECT COUNT(*) FROM {$table_name}"));
        $tempRow = $tempRes->fetchArray();
        $hasResults = $tempRow[0] > 0;
        ?>
        
        <?php if ($hasResults): ?>
        <div class="neon-table-container">
            <table class="neon-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>TÍTULO</th>
                        <th>URL</th>
                        <th>REGISTRADO POR</th>
                        <th>PRÉ-VISUALIZAÇÃO</th>
                        <th style="text-align: center;">AÇÕES</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    // Resetar o ponteiro do resultado para garantir que a tabela mostre os dados corretos
                    if ($filter_type == 'images') {
                        $res = $adb->query("SELECT * FROM {$table_name} WHERE url LIKE '%.jpg%' OR url LIKE '%.jpeg%' OR url LIKE '%.png%' OR url LIKE '%.gif%' OR url LIKE '%.svg%'");
                    } elseif ($filter_type == 'videos') {
                        $res = $adb->query("SELECT * FROM {$table_name} WHERE url LIKE '%.mp4%' OR url LIKE '%.webm%' OR url LIKE '%.ogg%'");
                    } else {
                        $res = $adb->query("SELECT * FROM {$table_name}");
                    }
                    
                    while ($row = $res->fetchArray(SQLITE3_ASSOC)): 
                    ?>
                    <tr>
                        <td style="color: var(--primary); font-weight: 700; font-size: 14px;">
                            #<?= $row['id'] ?>
                        </td>
                        <td style="color: var(--text-primary); font-weight: 500;">
                            <i class="fas fa-ad me-2" style="color: var(--primary);"></i>
                            <?= htmlspecialchars($row['title']) ?>
                        </td>
                        <td>
                            <div style="max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-secondary);">
                                <?= htmlspecialchars($row['url']) ?>
                            </div>
                        </td>
                        <td style="color: var(--text-secondary);">
                            <i class="fas fa-user me-2" style="color: var(--text-muted);"></i>
                            <?= htmlspecialchars($row['owner']) ?>
                        </td>
                        <td>
                            <?php 
                                $url = $row['url'];
                                $isVideo = strpos($url, '.mp4') !== false || 
                                          strpos($url, '.webm') !== false || 
                                          strpos($url, '.ogg') !== false;
                                $isImage = strpos($url, '.jpg') !== false || 
                                          strpos($url, '.jpeg') !== false || 
                                          strpos($url, '.png') !== false ||
                                          strpos($url, '.gif') !== false ||
                                          strpos($url, '.svg') !== false;
                            ?>
                            <div class="preview-box-neon" onclick="showImageModal('<?= htmlspecialchars($url) ?>', '<?= $isImage ? '1' : '0' ?>', '<?= $isVideo ? '1' : '0' ?>')">
                                <?php if ($isVideo): ?>
                                    <video class="preview-media-neon" muted>
                                        <source src="<?= htmlspecialchars($url) ?>" type="video/mp4">
                                    </video>
                                <?php elseif ($isImage): ?>
                                    <img src="<?= htmlspecialchars($url) ?>" 
                                         class="preview-media-neon"
                                         alt="<?= htmlspecialchars($row['title']) ?>"
                                         onerror="this.onerror=null; this.src='data:image/svg+xml,<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"90\" height=\"60\"><rect width=\"90\" height=\"60\" fill=\"%231e293b\"/><text x=\"45\" y=\"35\" font-family=\"Arial\" font-size=\"12\" fill=\"%2394a3b8\" text-anchor=\"middle\">Erro</text></svg>'">
                                <?php else: ?>
                                    <div style="width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; background: var(--dark-surface);">
                                        <i class="fas fa-link" style="color: var(--text-muted); font-size: 20px;"></i>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </td>
                        <td class="actions-cell">
                            <a href="edit_ads.php?id=<?= $row['id'] ?>&filter=<?= $filter_type ?>" 
                               class="action-btn action-edit" 
                               title="Editar">
                                <i class="fas fa-edit"></i>
                            </a>
                            <button type="button" 
   class="action-btn action-delete" 
   title="Excluir"
   onclick="confirmDelete(<?= $row['id'] ?>, '<?= addslashes($row['title']) ?>')">
    <i class="fas fa-trash"></i>
</button>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
        <?php else: ?>
        <div class="no-results">
            <i class="fas fa-inbox"></i>
            <h3>Nenhum anúncio encontrado</h3>
            <p style="margin-bottom: 20px;">
                <?php if ($filter_active): ?>
                Não há <?= $filter_type == 'images' ? 'imagens' : 'vídeos' ?> cadastrados.
                <?php else: ?>
                Você ainda não possui nenhum anúncio cadastrado.
                <?php endif; ?>
            </p>
            <a href="create_ads.php?filter=<?= $filter_type ?>" class="neon-btn">
                <i class="fas fa-plus"></i> Criar Primeiro Anúncio
            </a>
        </div>
        <?php endif; ?>
    </div>
    
</div>

<script>
function showImageModal(url, isImage, isVideo) {
    let contentHtml = '';
    let title = 'Pré-visualização';

    if (isImage === '1') {
        contentHtml = `<img src="${url}" style="max-width: 100%; max-height: 70vh; display: block; margin: 0 auto; border-radius: 10px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);">`;
        title = 'Pré-visualização de Imagem';
    } else if (isVideo === '1') {
        contentHtml = `<video controls autoplay loop style="max-width: 100%; max-height: 70vh; display: block; margin: 0 auto; border-radius: 10px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);"><source src="${url}" type="video/mp4"></video>`;
        title = 'Pré-visualização de Vídeo';
    } else {
        contentHtml = `<p>Conteúdo não é imagem nem vídeo. URL: <a href="${url}" target="_blank">${url}</a></p>`;
        title = 'Pré-visualização de Link';
    }

    Swal.fire({
        title: title,
        html: contentHtml,
        showConfirmButton: false,
        showCloseButton: true,
        background: 'var(--glass-bg)',
        backdrop: 'rgba(0,0,0,0.8)',
        customClass: {
            popup: 'neon-card',
            title: 'neon-title',
            closeButton: 'action-btn'
        },
        willClose: () => {
            const videoElement = document.querySelector('.swal2-html-container video');
            if (videoElement) {
                videoElement.pause();
            }
        }
    });
}

function confirmDelete(id, title) {
    Swal.fire({
        title: 'Tem certeza?',
        html: `Você está prestes a excluir permanentemente o anúncio: <strong>"${title}"</strong><br><br><span style="color: var(--danger); font-weight: bold;">⚠️ Esta ação não pode ser desfeita!</span>`,
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#ef4444',
        cancelButtonColor: '#64748b',
        confirmButtonText: 'Sim, excluir permanentemente!',
        cancelButtonText: 'Cancelar',
        reverseButtons: true,
        focusCancel: true
    }).then((result) => {
        if (result.isConfirmed) {
            // VERIFIQUE O CAMINHO CORRETO DO delete_ads.php
            window.location.href = 'delete_ads.php?delete=' + id + '&filter=<?= $filter_type ?>';
        }
    });
    
    return false;
}
</script>

<?php include ('includes/footer.php'); ?>