<?php
// create_ads.php
include ('includes/header.php');

// Configurações
$table_name = "ads";
$base_file = "ads.php";
$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';

// Processar criação se o formulário foi enviado
if(isset($_POST['submit'])) {
    $title = $_POST['title'];
    $url = $_POST['url'];
    
    // Usar escape para evitar SQL injection
    $title = $adb->escapeString($title);
    $url = $adb->escapeString($url);
    
    // Inserir no banco de dados
    $result = $adb->exec("INSERT INTO {$table_name}(title, url, owner) VALUES('{$title}', '{$url}', '{$admin_name}')");
    
    if($result !== false) {
        // Mostrar mensagem de sucesso
        ?>
        <style>
        .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(99, 102, 241, 0.3);
            box-shadow: 0 0 40px rgba(99, 102, 241, 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(--primary);
            margin-bottom: 25px;
            text-shadow: 0 0 20px rgba(99, 102, 241, 0.5);
        }
        
        .success-title {
            font-size: 2rem;
            font-weight: 800;
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            -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(--primary);
            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(--primary), var(--secondary));
            border-radius: 2px;
            animation: loading 2s linear forwards;
        }
        
        @keyframes loading {
            from { width: 0%; }
            to { width: 100%; }
        }
        </style>
        
        <div class="success-container">
            <div class="success-card">
                <i class="fas fa-check-circle success-icon"></i>
                <h1 class="success-title">Anúncio criado!</h1>
                
                <div class="success-message">
                    O anúncio <strong>"<?= htmlspecialchars($title) ?>"</strong><br>
                    foi criado com sucesso.
                </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 ?>" class="neon-btn" style="background: linear-gradient(135deg, var(--primary), var(--primary-dark));">
                        <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 ?>';
            }
        }, 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
    } else {
        // Erro na criação
        ?>
        <div class="neon-container">
            <div class="neon-card">
                <div style="text-align: center; padding: 50px;">
                    <i class="fas fa-exclamation-triangle" style="font-size: 60px; color: var(--danger); margin-bottom: 20px;"></i>
                    <h2 style="color: var(--danger);">Erro!</h2>
                    <p style="color: var(--text-secondary);">Não foi possível criar o anúncio.</p>
                    <br>
                    <a href="<?= $base_file ?>?filter=<?= $filter ?>" class="neon-btn secondary">
                        <i class="fas fa-arrow-left"></i> Voltar
                    </a>
                </div>
            </div>
        </div>
        <?php
    }
    exit;
}

// Exibir formulário de criação COM DESIGN NEON
?>
<!-- DESIGN NEON BONITO -->
<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: 40px;
    margin-bottom: 30px;
    position: relative;
    overflow: hidden;
}

.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;
}

.preview-section {
    background: rgba(15, 23, 42, 0.6);
    border-radius: 16px;
    padding: 25px;
    margin-top: 25px;
    border: 1px solid var(--glass-border);
}

.preview-box-large {
    width: 100%;
    height: 200px;
    border-radius: 12px;
    overflow: hidden;
    position: relative;
    border: 2px solid rgba(99, 102, 241, 0.3);
    background: rgba(15, 23, 42, 0.8);
    margin-top: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.preview-media-large {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.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;
}

.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);
}

.form-group {
    margin-bottom: 25px;
}

.form-group label {
    color: var(--text-primary);
    display: block;
    margin-bottom: 10px;
    font-weight: 600;
    font-size: 15px;
}

.d-flex {
    display: flex;
}

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

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

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

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

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

.info-badge {
    background: rgba(99, 102, 241, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 6px;
    padding: 5px 10px;
    font-size: 12px;
    color: var(--primary);
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-top: 5px;
}

.url-types {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.url-type-tag {
    background: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.3);
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 11px;
    color: var(--secondary);
}
</style>

<div class="neon-container">
    <div class="neon-header">
        <h1 class="neon-title">Criar Novo Anúncio</h1>
    </div>
    
    <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; font-size: 1.8rem;">
                    <i class="fas fa-plus-circle" style="color: var(--primary); margin-right: 10px;"></i>
                    Novo Anúncio
                </h2>
                <div style="display: flex; gap: 10px; margin-top: 5px;">
                    <div class="info-badge">
                        <i class="fas fa-user"></i>
                        <?= htmlspecialchars($admin_name) ?>
                    </div>
                    <div class="info-badge">
                        <i class="fas fa-filter"></i>
                        Filtro: <?= $filter == 'images' ? 'Imagens' : ($filter == 'videos' ? 'Vídeos' : 'Todos') ?>
                    </div>
                </div>
            </div>
            <a href="<?= $base_file ?>?filter=<?= $filter ?>" class="neon-btn secondary">
                <i class="fas fa-arrow-left"></i> Voltar
            </a>
        </div>
        
        <!-- FORMULÁRIO SIMPLES QUE FUNCIONA -->
        <form method="post">
            <div class="form-group">
                <label><i class="fas fa-heading" style="margin-right: 8px;"></i> Título</label>
                <input type="text" 
                       name="title" 
                       class="neon-input" 
                       placeholder="Ex: Promoção de Verão" 
                       required>
            </div>
            
            <div class="form-group">
                <label><i class="fas fa-link" style="margin-right: 8px;"></i> URL do Conteúdo</label>
                <input type="text" 
                       name="url" 
                       class="neon-input" 
                       placeholder="https://exemplo.com/imagem.jpg" 
                       required>
                <small style="color: var(--text-muted); display: block; margin-top: 8px; font-size: 13px;">
                    <i class="fas fa-info-circle"></i> URLs suportadas
                </small>
                <div class="url-types">
                    <span class="url-type-tag">JPG</span>
                    <span class="url-type-tag">PNG</span>
                    <span class="url-type-tag">GIF</span>
                    <span class="url-type-tag">SVG</span>
                    <span class="url-type-tag">MP4</span>
                    <span class="url-type-tag">WebM</span>
                    <span class="url-type-tag">OGG</span>
                </div>
            </div>
            
            <!-- Preview em tempo real -->
            <div class="preview-section">
                <h3 style="color: var(--text-primary); margin-bottom: 15px;">
                    <i class="fas fa-eye" style="color: var(--primary); margin-right: 8px;"></i>
                    Pré-visualização
                </h3>
                <div class="preview-box-large" id="previewContainer">
                    <div style="width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; background: var(--dark-surface);">
                        <i class="fas fa-image" style="color: var(--text-muted); font-size: 40px; margin-bottom: 10px;"></i>
                        <span style="color: var(--text-secondary);">Digite uma URL para ver a pré-visualização</span>
                    </div>
                </div>
            </div>
            
            <div style="text-align: center; margin-top: 40px;">
                <button type="submit" name="submit" class="neon-btn">
                    <i class="fas fa-save"></i> Criar Anúncio
                </button>
            </div>
        </form>
    </div>
</div>

<!-- JAVASCRIPT SIMPLIFICADO -->
<script>
// Preview em tempo real SIMPLES
document.querySelector('input[name="url"]')?.addEventListener('input', function() {
    const url = this.value;
    const container = document.getElementById('previewContainer');
    
    if (!container || !url) {
        container.innerHTML = `
            <div style="width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; background: var(--dark-surface);">
                <i class="fas fa-image" style="color: var(--text-muted); font-size: 40px; margin-bottom: 10px;"></i>
                <span style="color: var(--text-secondary);">Digite uma URL para ver a pré-visualização</span>
            </div>
        `;
        return;
    }
    
    const isVideo = url.match(/\.(mp4|webm|ogg)$/i);
    const isImage = url.match(/\.(jpg|jpeg|png|gif|svg)$/i);
    
    if (isVideo) {
        container.innerHTML = `<video class="preview-media-large" controls><source src="${url}" type="video/mp4"></video>`;
    } else if (isImage) {
        container.innerHTML = `<img src="${url}" class="preview-media-large">`;
    } else {
        container.innerHTML = `
            <div style="width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; background: var(--dark-surface);">
                <i class="fas fa-link" style="color: var(--text-muted); font-size: 40px; margin-bottom: 10px;"></i>
                <span style="color: var(--text-secondary);">Link externo</span>
            </div>
        `;
    }
});
</script>

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