<?php
session_start();
if (!isset($_SESSION['id'])) { header("Location: login.php"); exit; }

$db_path = __DIR__ . "/api/db/studiolivecode_creditos.db";
if (!is_dir(__DIR__ . "/api/db/")) { mkdir(__DIR__ . "/api/db/", 0777, true); }

try {
    $db = new PDO("sqlite:" . $db_path);
    $db->exec("CREATE TABLE IF NOT EXISTS carteira (id INTEGER PRIMARY KEY, saldo INTEGER)");
    if ($db->query("SELECT COUNT(*) FROM carteira WHERE id=1")->fetchColumn() == 0) {
        $db->exec("INSERT INTO carteira (id, saldo) VALUES (1, 0)");
    }
} catch(PDOException $e) { die("Erro: " . $e->getMessage()); }

if (isset($_POST["submit"])) {
    $stmt = $db->prepare("UPDATE carteira SET saldo = ? WHERE id = 1");
    $stmt->execute([$_POST["saldo"]]);
    header("Location: gerenciar_creditos.php?r=sucesso");
    exit;
}
$res = $db->query("SELECT saldo FROM carteira WHERE id='1'")->fetch(PDO::FETCH_ASSOC);

include "includes/header.php";
?>

<div style="background-color: #020617; height: 100vh; width: 100%; display: flex; align-items: center; justify-content: center; overflow: hidden; position: fixed; top: 0; left: 0; z-index: 1;">
    <div style="background: #070e1b; border: 1px solid #1e293b; border-radius: 30px; padding: 40px; width: 90%; max-width: 480px; box-shadow: 0 20px 60px rgba(0,0,0,0.8); display: flex; flex-direction: column; align-items: center;">
        
        <div style="background: rgba(34, 197, 94, 0.1); width: 70px; height: 70px; border-radius: 20px; display: flex; align-items: center; justify-content: center; margin-bottom: 20px;">
            <i class="fas fa-wallet" style="color: #22c55e; font-size: 35px;"></i>
        </div>

        <h2 style="color: #ffffff; font-size: 22px; font-weight: 800; margin: 0 0 5px 0; text-transform: uppercase;">Saldo de Créditos</h2>
        <p style="color: #64748b; font-size: 14px; margin-bottom: 25px;">Adicione créditos ao seu sistema</p>

        <form method="POST" style="width: 100%; display: flex; flex-direction: column; align-items: center;">
            <label style="color: #22c55e; font-size: 11px; font-weight: 800; text-transform: uppercase; margin-bottom: 12px;">Quantidade de Créditos Atual</label>
            
            <input type="number" name="saldo" value="<?= $res['saldo']; ?>" required 
                   style="width: 100%; max-width: 380px; background: #0f172a; border: 1px solid #1e293b; color: #ffffff; border-radius: 15px; padding: 18px; font-size: 24px; text-align: center; outline: none; margin-bottom: 25px;">

            <button type="submit" name="submit" 
                    style="width: 100%; max-width: 380px; height: 60px; background: #22c55e; color: #ffffff; border-radius: 18px; font-size: 15px; font-weight: 800; text-transform: uppercase; border: none; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 15px;">
                <i class="fas fa-plus-circle"></i> Atualizar Créditos
            </button>
        </form>

        <a href="index.php" 
           style="width: 100%; max-width: 380px; height: 55px; background: rgba(30, 41, 59, 0.5); color: #94a3b8; border-radius: 18px; font-size: 14px; font-weight: 600; text-decoration: none; display: flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid #1e293b;">
            <i class="fas fa-chevron-left"></i> Voltar ao Painel
        </a>
    </div> 
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<?php if (isset($_GET["r"])): ?>
<script>
    Swal.fire({ icon: 'success', title: 'Créditos Atualizados!', background: '#070e1b', color: '#fff', confirmButtonColor: '#22c55e' });
</script>
<?php endif; ?>
<?php include "includes/footer.php"; ?>
