<?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();
    }
}

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

$table_name = 'intro';
$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>
:root {
    --primary-dark: #0f172a;
    --secondary-dark: #1e293b;
    --accent-blue: #3b82f6;
    --light-blue: #60a5fa;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
}

.glass-container {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 15px;
    border: 1px solid var(--glass-border);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3),
                inset 0 1px 0 rgba(255, 255, 255, 0.1);
    padding: 2rem;
    margin: 2rem auto;
    max-width: 800px;
}

.glass-card {
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(8px);
    border-radius: 12px;
    border: 1px solid rgba(96, 165, 250, 0.2);
    overflow: hidden;
}

.card-header-glass {
    background: rgba(30, 41, 59, 0.8);
    border-bottom: 1px solid rgba(96, 165, 250, 0.2);
    padding: 1.5rem;
}

.card-body-glass {
    padding: 2rem;
    color: #e2e8f0;
}

.glass-input {
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid rgba(96, 165, 250, 0.2);
    border-radius: 10px;
    color: #e2e8f0;
    padding: 0.75rem 1rem;
    transition: all 0.3s ease;
    width: 100%;
    font-size: 1rem;
}

.glass-input:focus {
    background: rgba(15, 23, 42, 0.8);
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
    color: #ffffff;
    outline: none;
}

.glass-input::placeholder {
    color: #94a3b8;
}

.form-label-glass {
    color: #94a3b8;
    font-size: 0.875rem;
    margin-bottom: 0.5rem;
    display: block;
    font-weight: 500;
}

.btn-primary-glass {
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
    border: none;
    border-radius: 10px;
    color: white;
    font-weight: 600;
    padding: 0.75rem 2rem;
    font-size: 1rem;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    z-index: 1;
}

.btn-primary-glass:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(59, 130, 246, 0.3);
    color: white;
}

.btn-primary-glass::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.7s ease;
    z-index: -1;
}

.btn-primary-glass:hover::before {
    left: 100%;
}

.form-group-glass {
    margin-bottom: 1.5rem;
}

.video-preview {
    background: rgba(15, 23, 42, 0.3);
    border-radius: 10px;
    padding: 1rem;
    margin-top: 1.5rem;
    border: 1px solid rgba(96, 165, 250, 0.1);
}

.video-preview h4 {
    color: #94a3b8;
    font-size: 1rem;
    margin-bottom: 0.5rem;
}

.current-url {
    color: #60a5fa;
    font-weight: 500;
    word-break: break-all;
    padding: 0.5rem;
    background: rgba(15, 23, 42, 0.5);
    border-radius: 5px;
    margin-top: 0.5rem;
}

.page-title {
    background: linear-gradient(90deg, #60a5fa, #3b82f6);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    font-weight: 700;
    font-size: 2.2rem;
    margin-bottom: 0.5rem;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.page-subtitle {
    color: #cbd5e1;
    font-size: 1.1rem;
    margin-bottom: 2rem;
    opacity: 0.9;
}

.icon-gradient {
    background: linear-gradient(90deg, #60a5fa, #3b82f6);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    display: inline-block;
}

.success-message {
    background: rgba(34, 197, 94, 0.2);
    border: 1px solid rgba(34, 197, 94, 0.3);
    color: #86efac;
    padding: 1rem;
    border-radius: 10px;
    margin-bottom: 1.5rem;
    text-align: center;
}
</style>

<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-lg-8 col-md-10">
            <div class="glass-container">
                <center>
                    <h1 class="page-title">
                        <i class="fas fa-tv icon-gradient"></i> Configurar Vídeo de Introdução
                    </h1>
                    <p class="page-subtitle">Adicione ou atualize o link do vídeo de introdução do sistema</p>
                </center>

                <?php if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['intro_url'])): ?>
                <div class="success-message">
                    <i class="fas fa-check-circle"></i> Vídeo atualizado com sucesso!
                </div>
                <?php endif; ?>

                <div class="glass-card">
                    <div class="card-header-glass">
                        <center>
                            <h3 style="color: #e2e8f0; margin: 0;">
                                <i class="fas fa-link" style="color: #60a5fa;"></i> 
                                <span style="color: #e2e8f0;">Configuração do Vídeo</span>
                            </h3>
                        </center>
                    </div>
                    
                    <div class="card-body-glass">
                        <form action="" method="post" enctype="multipart/form-data">
                            <div class="form-group-glass">
                                <label class="form-label-glass">
                                    <i class="fas fa-external-link-alt" style="color: #60a5fa;"></i> 
                                    URL do Vídeo
                                </label>
                                <div class="input-group">
                                    <input type="text" class="glass-input" name="intro_url" 
                                           value="<?php echo htmlspecialchars($intro ?: ''); ?>" 
                                           placeholder="Ex: https://www.youtube.com/embed/SEU_VIDEO ou https://seu-site.com/video.mp4"
                                           required>
                                </div>
                                <small style="color: #94a3b8; font-size: 0.875rem; margin-top: 0.5rem; display: block;">
                                    <i class="fas fa-info-circle"></i> Suporta links do YouTube (embed), Vimeo, MP4, WEBM, etc.
                                </small>
                            </div>

                            <?php if (!empty($intro)): ?>
                            <div class="video-preview">
                                <h4>
                                    <i class="fas fa-play-circle" style="color: #60a5fa;"></i> 
                                    Vídeo Atual Configurado:
                                </h4>
                                <div class="current-url">
                                    <i class="fas fa-video" style="color: #94a3b8; margin-right: 5px;"></i>
                                    <?php echo htmlspecialchars($intro); ?>
                                </div>
                            </div>
                            <?php else: ?>
                            <div class="video-preview">
                                <h4>
                                    <i class="fas fa-exclamation-triangle" style="color: #f59e0b;"></i> 
                                    Nenhum vídeo configurado
                                </h4>
                                <p style="color: #94a3b8; margin-top: 0.5rem;">
                                    Adicione um link de vídeo para que ele seja exibido na introdução do sistema.
                                </p>
                            </div>
                            <?php endif; ?>

                            <div class="form-group-glass" style="margin-top: 2rem;">
                                <center>
                                    <button class="btn btn-primary-glass" name="submit" type="submit">
                                        <i class="fas fa-save" style="margin-right: 8px;"></i> 
                                        Salvar Configuração
                                    </button>
                                </center>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="glass-card" style="margin-top: 2rem;">
                    <div class="card-header-glass">
                        <center>
                            <h4 style="color: #e2e8f0; margin: 0;">
                                <i class="fas fa-lightbulb" style="color: #fbbf24;"></i> 
                                <span style="color: #e2e8f0;">Informações Úteis</span>
                            </h4>
                        </center>
                    </div>
                    <div class="card-body-glass">
                        <div style="color: #cbd5e1;">
                            <p style="margin-bottom: 0.75rem;">
                                <i class="fas fa-check-circle" style="color: #10b981; margin-right: 8px;"></i>
                                <strong>YouTube:</strong> Use o link de incorporação (ex: https://www.youtube.com/embed/CODIGO_DO_VIDEO)
                            </p>
                            <p style="margin-bottom: 0.75rem;">
                                <i class="fas fa-check-circle" style="color: #10b981; margin-right: 8px;"></i>
                                <strong>Vimeo:</strong> Use o link de incorporação fornecido pelo Vimeo
                            </p>
                            <p style="margin-bottom: 0.75rem;">
                                <i class="fas fa-check-circle" style="color: #10b981; margin-right: 8px;"></i>
                                <strong>Arquivos diretos:</strong> Suporta MP4, WEBM, OGG e outros formatos de vídeo
                            </p>
                            <p style="margin-bottom: 0;">
                                <i class="fas fa-exclamation-triangle" style="color: #f59e0b; margin-right: 8px;"></i>
                                Certifique-se de que o link esteja acessível publicamente
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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