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

$db_path = __DIR__ . '/api/db/db_ads.db';
try {
    $pdo = new PDO("sqlite:" . $db_path);
    $pdo->exec("CREATE TABLE IF NOT EXISTS manutencao (id INTEGER PRIMARY KEY, canais INTEGER, filmes INTEGER, series INTEGER, mensagem TEXT)");
    
    // Verifica se já existe registro
    $check = $pdo->query("SELECT count(*) FROM manutencao")->fetchColumn();
    if($check == 0) $pdo->exec("INSERT INTO manutencao (id, canais, filmes, series, mensagem) VALUES (1, 1, 1, 1, 'Servidor em manutenção')");

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $canais = isset($_POST['canais']) ? 1 : 0;
        $filmes = isset($_POST['filmes']) ? 1 : 0;
        $series = isset($_POST['series']) ? 1 : 0;
        $msg = $_POST['mensagem'];
        $stmt = $pdo->prepare("UPDATE manutencao SET canais=?, filmes=?, series=?, mensagem=? WHERE id=1");
        $stmt->execute([$canais, $filmes, $series, $msg]);
        echo "<script>alert('Alterações salvas!'); window.location.href='manutencao.php';</script>";
    }

    $data = $pdo->query("SELECT * FROM manutencao WHERE id=1")->fetch(PDO::FETCH_ASSOC);
} catch(Exception $e) { die("Erro banco"); }
?>

<style>
    body, .main-panel { background-color: #020617 !important; color: #fff; }
    .card-maint {
        background: #0f172a; border: 1px solid #1e293b; border-radius: 20px;
        padding: 25px; max-width: 500px; margin: 20px auto;
    }
    .maint-item {
        display: flex; justify-content: space-between; align-items: center;
        padding: 15px; background: rgba(255,255,255,0.03);
        border-radius: 12px; margin-bottom: 10px;
    }
    .maint-label { display: flex; align-items: center; gap: 10px; font-weight: 600; }
    
    /* SWITCH STYLE */
    .switch { position: relative; display: inline-block; width: 60px; height: 30px; }
    .switch input { opacity: 0; width: 0; height: 0; }
    .slider {
        position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0;
        background-color: #ef4444; transition: .4s; border-radius: 34px;
    }
    .slider:before {
        position: absolute; content: "✖"; color: #fff; font-size: 10px; display: flex; 
        align-items: center; justify-content: center; height: 22px; width: 22px;
        left: 4px; bottom: 4px; background-color: white; transition: .4s; border-radius: 50%;
    }
    input:checked + .slider { background-color: #22c55e; }
    input:checked + .slider:before { transform: translateX(30px); content: "✔"; color: #22c55e; }

    textarea {
        width: 100%; background: #020617; border: 1px solid #1e293b;
        border-radius: 12px; color: #fff; padding: 15px; margin: 15px 0;
    }
    .btn-save {
        width: 100%; padding: 15px; border-radius: 12px; border: none;
        background: #3b82f6; color: #fff; font-weight: 800; cursor: pointer;
    }
</style>

<div class="container-fluid">
    <div class="card-maint">
        <h2 style="text-align:center; margin-bottom:25px;">⚙️ Gerenciamento</h2>
        <form method="POST">
            <div class="maint-item">
                <div class="maint-label">📺 Canais</div>
                <label class="switch">
                    <input type="checkbox" name="canais" <?= $data['canais'] ? 'checked' : '' ?>>
                    <span class="slider"></span>
                </label>
            </div>
            <div class="maint-item">
                <div class="maint-label">🎬 Filmes</div>
                <label class="switch">
                    <input type="checkbox" name="filmes" <?= $data['filmes'] ? 'checked' : '' ?>>
                    <span class="slider"></span>
                </label>
            </div>
            <div class="maint-item">
                <div class="maint-label">🎞️ Séries</div>
                <label class="switch">
                    <input type="checkbox" name="series" <?= $data['series'] ? 'checked' : '' ?>>
                    <span class="slider"></span>
                </label>
            </div>

            <label style="color:#64748b; font-size:0.8rem; font-weight:800;">MENSAGEM DE MANUTENÇÃO:</label>
            <textarea name="mensagem" rows="3"><?= $data['mensagem'] ?></textarea>
            
            <button type="submit" class="btn-save">💾 Salvar Alterações</button>
        </form>
    </div>
</div>

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