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

// delete_ads.php
include ('includes/header.php');

// Configurações
$table_name = "ads";
$base_file = "ads.php";

// Verificar se foi passado um ID para excluir
if(isset($_GET['delete']) && is_numeric($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';
    
    try {
	        // Buscar o anúncio antes de excluir para remover arquivo físico se necessário
	        $result = $adb->query("SELECT title, url FROM {$table_name} WHERE id = {$id}");
	        $row = $result->fetchArray();
	        $title = $row['title'] ?? 'Anúncio';
	        $url = $row['url'] ?? '';
	        
	        // Se for um arquivo local, excluir do disco
	        if ($url != "" && strpos($url, 'rtx/ads/ADM/') !== false) {
	            if (file_exists($url)) {
	                unlink($url);
	            }
	        }
	        
	        // Excluir o registro
	        $adb->exec("DELETE FROM {$table_name} WHERE id = {$id}");
        
        ?>
        <style>
        :root {
            --primary: #6366f1;
            --primary-dark: #4f46e5;
            --secondary: #10b981;
            --danger: #ef4444;
            --dark-bg: #0f172a;
            --dark-surface: #1e293b;
            --text-primary: #f1f5f9;
            --text-secondary: #94a3b8;
        }
        
        .success-container {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(145deg, var(--dark-bg), var(--dark-surface));
        }
        
        .success-card {
            background: linear-gradient(145deg, rgba(30, 41, 59, 0.9), rgba(15, 23, 42, 0.95));
            backdrop-filter: blur(20px);
            border-radius: 24px;
            border: 1px solid rgba(16, 185, 129, 0.3);
            box-shadow: 0 0 40px rgba(16, 185, 129, 0.2);
            padding: 50px 40px;
            text-align: center;
            max-width: 500px;
            width: 90%;
            position: relative;
            overflow: hidden;
        }
        
        .success-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;
        }
        
        .success-icon {
            font-size: 80px;
            color: var(--secondary);
            margin-bottom: 25px;
            text-shadow: 0 0 20px rgba(16, 185, 129, 0.5);
        }
        
        .success-title {
            font-size: 2rem;
            font-weight: 800;
            background: linear-gradient(135deg, var(--secondary), var(--primary));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 15px;
        }
        
        .success-message {
            color: var(--text-secondary);
            font-size: 1.1rem;
            margin-bottom: 30px;
            line-height: 1.5;
        }
        
        .countdown {
            color: var(--secondary);
            font-weight: 700;
            font-size: 1.2rem;
            margin-top: 20px;
        }
        
        .loading-bar {
            width: 100%;
            height: 4px;
            background: rgba(30, 41, 59, 0.5);
            border-radius: 2px;
            margin-top: 30px;
            overflow: hidden;
        }
        
        .loading-progress {
            width: 0%;
            height: 100%;
            background: linear-gradient(90deg, var(--secondary), var(--primary));
            border-radius: 2px;
            animation: loading 2s linear forwards;
        }
        
        @keyframes loading {
            from { width: 0%; }
            to { width: 100%; }
        }
        
        .neon-btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            padding: 14px 28px;
            background: linear-gradient(135deg, var(--secondary), var(--primary));
            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: 0 0 20px rgba(99, 102, 241, 0.3);
        }
        
        .neon-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 25px rgba(99, 102, 241, 0.4);
            color: white;
            text-decoration: none;
        }
        </style>
        
        <div class="success-container">
            <div class="success-card">
                <i class="fas fa-check-circle success-icon"></i>
                <h1 class="success-title">Anúncio excluído!</h1>
                
                <div class="success-message">
                    O anúncio <strong>"<?= htmlspecialchars($title) ?>"</strong><br>
                    foi excluído permanentemente.
                </div>
                
                <div class="loading-bar">
                    <div class="loading-progress"></div>
                </div>
                
                <div class="countdown">
                    Redirecionando em <span id="countdown-number">2</span> segundos...
                </div>
                
                <div style="margin-top: 30px;">
                    <a href="<?= $base_file ?>?filter=<?= $filter ?>&t=<?= time() ?>" class="neon-btn">
                        <i class="fas fa-arrow-left"></i> Voltar agora
                    </a>
                </div>
            </div>
        </div>
        
        <script>
        // Contagem regressiva
        let countdown = 2;
        const countdownElement = document.getElementById('countdown-number');
        const interval = setInterval(function() {
            countdown--;
            countdownElement.textContent = countdown;
            
            if (countdown <= 0) {
                clearInterval(interval);
                window.location.href = '<?= $base_file ?>?filter=<?= $filter ?>&t=<?= time() ?>';
            }
        }, 1000);
        
        // Se o usuário clicar em "Voltar agora", cancela o redirecionamento automático
        document.querySelector('.neon-btn').addEventListener('click', function() {
            clearInterval(interval);
        });
        </script>
        <?php
        
    } catch (Exception $e) {
        ?>
        <div class="success-container">
            <div class="success-card" style="border-color: rgba(239, 68, 68, 0.3); box-shadow: 0 0 40px rgba(239, 68, 68, 0.2);">
                <i class="fas fa-exclamation-circle success-icon" style="color: var(--danger);"></i>
                <h1 class="success-title" style="background: linear-gradient(135deg, var(--danger), #f87171); -webkit-background-clip: text;">Erro!</h1>
                
                <div class="success-message">
                    Não foi possível excluir o anúncio.<br>
                    Tente novamente.
                </div>
                
                <div style="margin-top: 30px;">
                    <a href="<?= $base_file ?>?filter=<?= $filter ?>&t=<?= time() ?>" class="neon-btn" style="background: linear-gradient(135deg, var(--danger), #f87171);">
                        <i class="fas fa-arrow-left"></i> Voltar
                    </a>
                </div>
            </div>
        </div>
        <?php
    }
    
} else {
    // Se não houver ID, redirecionar
    header("Location: {$base_file}");
    exit;
}

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