<?php
session_start();

// --- BANCO DE DADOS INDIVIDUAL ---
$db_file = 'intro_config.db';

try {
    $db = new PDO("sqlite:" . $db_file);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Criar tabela se não existir
    $db->exec("CREATE TABLE IF NOT EXISTS intro (id INTEGER PRIMARY KEY, url TEXT, status INTEGER)");

    // Salvar Dados
    if (isset($_POST['btn_salvar'])) {
        $url_video = $_POST['url_video'];
        $status_video = isset($_POST['status_intro']) ? 1 : 0;

        $db->exec("DELETE FROM intro"); // Limpa anterior
        $stmt = $db->prepare("INSERT INTO intro (url, status) VALUES (?, ?)");
        $stmt->execute([$url_video, $status_video]);
        $sucesso = true;
    }

    // Carregar Dados
    $dados = $db->query("SELECT * FROM intro LIMIT 1")->fetch(PDO::FETCH_ASSOC);
    $url_atual = $dados ? $dados['url'] : '';
    $status_atual = $dados ? $dados['status'] : 0;

} catch (Exception $e) {
    $erro = $e->getMessage();
}
?>

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>CONFIGURAR INTRO</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body { background: #020617; color: white; font-family: sans-serif; display: flex; justify-content: center; padding: 20px; }
        .box { background: #070e1b; border: 1px solid #1e293b; padding: 30px; border-radius: 20px; width: 100%; max-width: 500px; box-shadow: 0 10px 30px rgba(0,0,0,0.5); }
        .btn-voltar { color: #3b82f6; text-decoration: none; font-size: 12px; font-weight: bold; text-transform: uppercase; display: block; margin-bottom: 20px; }
        h2 { margin-top: 0; font-size: 20px; }
        label { color: #3b82f6; font-size: 12px; font-weight: bold; display: block; margin-bottom: 8px; }
        input[type="text"] { width: 100%; padding: 15px; background: #0f172a; border: 1px solid #1e293b; border-radius: 10px; color: white; box-sizing: border-box; margin-bottom: 20px; }
        .row { display: flex; justify-content: space-between; align-items: center; background: #0f172a; padding: 15px; border-radius: 10px; margin-bottom: 20px; border: 1px solid #1e293b; }
        .btn-post { width: 100%; background: #3b82f6; border: none; padding: 15px; color: white; border-radius: 10px; font-weight: bold; cursor: pointer; }
        .msg { background: #22c55e; color: white; padding: 15px; border-radius: 10px; margin-bottom: 20px; text-align: center; font-size: 14px; }
        video { width: 100%; border-radius: 10px; margin-top: 20px; border: 1px solid #1e293b; }
    </style>
</head>
<body>

<div class="box">
    <a href="index.php" class="btn-voltar"><i class="fas fa-arrow-left"></i> Voltar</a>
    
    <?php if(isset($sucesso)): ?>
        <div class="msg">ATUALIZADO COM SUCESSO!</div>
    <?php endif; ?>

    <h2><i class="fas fa-video"></i> Vídeo de Introdução</h2>
    
    <form method="POST">
        <label>URL DO VÍDEO (.MP4)</label>
        <input type="text" name="url_video" value="<?php echo $url_atual; ?>" placeholder="https://site.com/video.mp4" required>

        <div class="row">
            <span>ATIVAR NO APLICATIVO</span>
            <input type="checkbox" name="status_intro" style="width: 20px; height: 20px;" <?php echo ($status_atual == 1) ? 'checked' : ''; ?>>
        </div>

        <button type="submit" name="btn_salvar" class="btn-post">SALVAR CONFIGURAÇÃO</button>
    </form>

    <?php if(!empty($url_atual)): ?>
        <video controls>
            <source src="<?php echo $url_atual; ?>" type="video/mp4">
        </video>
    <?php endif; ?>
</div>

</body>
</html>
