<?php


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


function Submit($sub_type, $db, $table_name)
{
    if ($sub_type == 'sub_new' && isset($_POST['intro_url'])) {
        $intro_url = $_POST['intro_url'];
        $stmt = $db->prepare("UPDATE $table_name SET url = :url WHERE id = 1");
        $stmt->bindValue(':url', $intro_url, SQLITE3_TEXT);
        $stmt->execute();
        echo "<script>window.location.href='users.php'</script>";
    }
}

error_reporting(0); // Avoid displaying errors to users in production
include 'includes/header.php';

$table_name = 'intro2';
$db = new SQLite3('./api/intro.db'); // Replace with your database configuration

$db->exec("CREATE TABLE IF NOT EXISTS $table_name (id INTEGER PRIMARY KEY, url TEXT)");
$rows = $db->querySingle("SELECT COUNT(*) as count FROM $table_name");
$numRows = intval($rows);

if ($numRows === 0) {
    $db->exec("INSERT INTO $table_name (id, url) VALUES ('1','')");
}

$intro = $db->querySingle("SELECT url FROM $table_name WHERE id = 1");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    Submit('sub_new', $db, $table_name);
}

if (!file_exists('intro')) {
    mkdir('intro', 0755, true);
}

?>

<style>
/* === ESTILOS ESPECÍFICOS PARA CONFIGURAÇÃO DE VÍDEO FUTURISTA === */
.main-content {
    background: transparent !important;
    padding: 20px;
}

.card {
    background: var(--surface-light) !important;
    border: 1px solid var(--border) !important;
    border-radius: 20px !important;
    box-shadow: 0 8px 40px rgba(0,0,0,0.4) !important;
    color: var(--text-primary) !important;
    overflow: hidden;
    position: relative;
}

.card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--primary), var(--accent), var(--secondary));
    animation: borderGlow 3s ease-in-out infinite;
}

@keyframes borderGlow {
    0%, 100% { opacity: 0.7; }
    50% { opacity: 1; }
}

.card-header {
    background: linear-gradient(135deg, var(--surface-light), var(--surface-lighter)) !important;
    border-bottom: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
    font-weight: 700 !important;
    padding: 20px !important;
}

.card-body {
    background: transparent !important;
    color: var(--text-primary) !important;
    padding: 30px !important;
}

/* Formulários futuristas */
.form-control {
    background: var(--surface-lighter) !important;
    border: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
    border-radius: 12px !important;
    padding: 14px 18px !important;
    font-size: 1rem;
    transition: all 0.3s ease;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.form-control:focus {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 0.3rem rgba(0, 212, 255, 0.25) !important;
    background: var(--surface-lighter) !important;
    color: var(--text-primary) !important;
    transform: translateY(-2px);
}

.form-control::placeholder {
    color: var(--text-muted) !important;
}

/* Botões futuristas */
.btn-info {
    background: linear-gradient(135deg, var(--accent), #00cc7a) !important;
    border: none !important;
    border-radius: 12px !important;
    box-shadow: var(--glow-accent) !important;
    padding: 14px 30px !important;
    font-weight: 600 !important;
    transition: all 0.3s ease !important;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-size: 1rem;
}

.btn-info:hover {
    transform: translateY(-3px) !important;
    box-shadow: 0 8px 25px rgba(0, 255, 136, 0.5) !important;
}

/* Labels e textos */
.control-label {
    color: var(--text-primary) !important;
    font-weight: 600;
    margin-bottom: 10px;
    display: block;
    font-size: 1rem;
}

.text-primary2 {
    color: var(--primary) !important;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-size: 1.3rem !important;
}

/* Títulos */
.h2 {
    color: var(--text-primary) !important;
    font-weight: 700;
    margin-bottom: 20px;
}

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

/* Ícones */
.fa-television {
    color: var(--primary) !important;
    margin-right: 10px;
}

/* Efeito de partículas */
@keyframes moveParticles {
    from { transform: translateY(0); opacity: 0.7; }
    to { transform: translateY(-1000px); opacity: 0; }
}

.particle {
    position: fixed;
    bottom: 0;
    width: 2px;
    height: 2px;
    background: var(--primary);
    opacity: 0.5;
    animation: moveParticles linear infinite;
    z-index: -1;
}

.particle:nth-child(odd) { background: var(--accent); }

/* Preview do vídeo */
.video-preview {
    background: var(--surface-lighter);
    border: 2px dashed var(--border);
    border-radius: 12px;
    padding: 25px;
    text-align: center;
    margin-top: 20px;
    transition: all 0.3s ease;
}

.video-preview:hover {
    border-color: var(--primary);
    box-shadow: var(--glow);
}

.video-placeholder {
    width: 100%;
    height: 200px;
    background: var(--surface-light);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    margin-bottom: 15px;
}

/* Responsividade */
@media (max-width: 768px) {
    .main-content {
        padding: 10px !important;
    }
    
    .card-body {
        padding: 20px !important;
    }
    
    .btn-info {
        padding: 12px 25px !important;
        font-size: 0.9rem !important;
    }
    
    .form-control {
        padding: 12px 15px !important;
    }
}
</style>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        // Partículas animadas
        const numParticles = 15;
        const particlesContainer = document.createElement('div');
        particlesContainer.style.position = 'fixed';
        particlesContainer.style.top = '0';
        particlesContainer.style.left = '0';
        particlesContainer.style.width = '100%';
        particlesContainer.style.height = '100%';
        particlesContainer.style.pointerEvents = 'none';
        particlesContainer.style.zIndex = '-1';
        
        for (let i = 0; i < numParticles; i++) {
            const particle = document.createElement('div');
            particle.classList.add('particle');
            particle.style.left = Math.random() * 100 + 'vw';
            particle.style.animationDuration = (2 + Math.random() * 8) + 's';
            particle.style.animationDelay = Math.random() * 10 + 's';
            particle.style.width = particle.style.height = (1 + Math.random() * 3) + 'px';
            particlesContainer.appendChild(particle);
        }
        
        document.body.appendChild(particlesContainer);

        // Preview dinâmico do vídeo
        const videoInput = document.querySelector('input[name="intro_url"]');
        const previewContainer = document.createElement('div');
        previewContainer.className = 'video-preview';
        previewContainer.innerHTML = `
            <div style="color: var(--text-secondary); margin-bottom: 15px;">
                <i class="fas fa-play-circle"></i> Preview do Vídeo
            </div>
            <div id="video-preview-container" class="video-placeholder">
                <i class="fas fa-film fa-3x" style="color: var(--text-muted);"></i>
                <div style="margin-top: 10px;">URL do vídeo será mostrada aqui</div>
            </div>
        `;
        
        // Inserir o preview após o formulário
        const form = document.querySelector('form');
        form.appendChild(previewContainer);

        function updateVideoPreview() {
            const url = videoInput.value;
            const container = document.getElementById('video-preview-container');
            
            if (url && (url.includes('youtube.com') || url.includes('youtu.be') || url.includes('vimeo.com') || url.includes('mp4') || url.includes('webm'))) {
                if (url.includes('youtube.com') || url.includes('youtu.be')) {
                    // Extrair ID do YouTube
                    let videoId = '';
                    if (url.includes('youtube.com/watch?v=')) {
                        videoId = url.split('v=')[1].split('&')[0];
                    } else if (url.includes('youtu.be/')) {
                        videoId = url.split('youtu.be/')[1].split('?')[0];
                    }
                    
                    if (videoId) {
                        container.innerHTML = `
                            <iframe width="100%" height="200" src="https://www.youtube.com/embed/${videoId}" 
                                frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
                                allowfullscreen style="border-radius: 8px;">
                            </iframe>
                        `;
                    }
                } else {
                    container.innerHTML = `
                        <div style="color: var(--accent); text-align: center;">
                            <i class="fas fa-check-circle fa-2x"></i>
                            <div style="margin-top: 10px;">✅ URL de vídeo válida</div>
                            <small style="color: var(--text-muted);">${url}</small>
                        </div>
                    `;
                }
            } else if (url) {
                container.innerHTML = `
                    <div style="color: var(--secondary); text-align: center;">
                        <i class="fas fa-exclamation-triangle fa-2x"></i>
                        <div style="margin-top: 10px;">⚠️ Verifique a URL do vídeo</div>
                        <small style="color: var(--text-muted);">Suportado: YouTube, Vimeo, MP4, WebM</small>
                    </div>
                `;
            } else {
                container.innerHTML = `
                    <i class="fas fa-film fa-3x" style="color: var(--text-muted);"></i>
                    <div style="margin-top: 10px; color: var(--text-muted);">🔗 Insira a URL do vídeo</div>
                `;
            }
        }

        videoInput.addEventListener('input', updateVideoPreview);
        updateVideoPreview(); // Initial update

        // Efeitos nos inputs
        const inputs = document.querySelectorAll('.form-control');
        inputs.forEach(input => {
            input.addEventListener('focus', function() {
                this.style.transform = 'translateY(-2px)';
            });
            
            input.addEventListener('blur', function() {
                this.style.transform = 'translateY(0)';
            });
        });
    });
</script>

<main role="main" class="col-12 pt-4 px-4 main-content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-8 mx-auto">
                <div class="card border-left-primary shadow h-100 card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary2">
                            <i class="fas fa-tv me-2"></i>Configuração de Vídeo de Introdução
                        </h6>
                    </div>
                    <div class="card-body">
                        <form action="" method="post" enctype="multipart/form-data">
                            <div class="form-group">
                                <label class="control-label" for="intro_url">
                                    <i class="fas fa-link me-2"></i><strong>URL do Vídeo</strong>
                                </label>
                                <input type="text" class="form-control" name="intro_url" 
                                       placeholder="Ex: https://www.youtube.com/watch?v=SEU_VIDEO_ID ou https://vimeo.com/SEU_VIDEO" 
                                       value="<?php echo htmlspecialchars($intro); ?>">
                                <small class="text-muted">Suporta YouTube, Vimeo, MP4, WebM e outros formatos de vídeo.</small>
                            </div>
                            
                            <div class="form-group mt-4">
                                <center>
                                    <button class="btn btn-info btn-lg" name="submit" type="submit">
                                        <i class="fas fa-save me-2"></i>Salvar Configuração
                                    </button>
                                </center>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

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