<?php
session_start();
include 'cfg.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['voucher']) && isset($_POST['name'])) {
    $voucher = $_POST['voucher'];  // Pastikan data voucher ada
    $name = $_POST['name'];  // Menambahkan variabel nama

    // Validasi voucher di database
    try {
        $stmt = $pdo->prepare("SELECT * FROM vouchers WHERE code = ? AND is_used = 0");
        $stmt->execute([$voucher]);
        $voucherData = $stmt->fetch();

        if ($voucherData) {
            // Menyimpan nama pengguna ke session
            $_SESSION['voucher_code'] = $voucher;
            $_SESSION['name'] = $name;  // Menyimpan nama pengguna di session

            // Posisi bom acak setiap sesi
            $_SESSION['bombPosition'] = random_int(0, 8);
            $_SESSION['opened'] = [];  // Reset data lingkaran yang sudah dibuka

            // Tandai voucher sebagai sudah digunakan
            // Update status voucher menjadi digunakan dan simpan nama pengguna yang menggunakan voucher
            $updateStmt = $pdo->prepare("UPDATE vouchers SET is_used = 1, used_by = ?, used_at = NOW() WHERE code = ?");
            $updateStmt->execute([$name, $voucher]);

            // Redirect ke halaman permainan setelah berhasil login
            header("Location: index.php");
            exit();
        } else {
            // Jika voucher tidak valid atau sudah digunakan
            $error = "Voucher tidak valid atau sudah digunakan!";
        }
    } catch (PDOException $e) {
        $error = "Terjadi kesalahan saat memproses voucher: " . $e->getMessage();
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Masukkan Voucher</title>
    <style>
        body {
            margin: 0;
            min-height: 100vh;
            padding: 24px 16px;
            font-family: Arial, sans-serif;
            background:
                radial-gradient(circle at top, rgba(223, 159, 54, 0.12), transparent 22%),
                radial-gradient(circle at bottom right, rgba(45, 121, 199, 0.12), transparent 26%),
                linear-gradient(180deg, #10273d, #08111d);
            color: #fff;
        }

        .page-shell {
            max-width: 480px;
            margin: 0 auto;
        }

        .navbar {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 16px;
            margin-bottom: 18px;
            padding: 12px 16px;
            border-radius: 18px;
            background: rgba(0, 0, 0, 0.28);
            border: 1px solid rgba(255,255,255,0.1);
        }

        .navbar-brand img {
            width: clamp(108px, 24vw, 145px);
            height: auto;
            display: block;
        }

        .navbar-title {
            font-size: 13px;
            font-weight: 800;
            letter-spacing: 0.1em;
            text-transform: uppercase;
            color: rgba(255,255,255,0.82);
        }

        .card {
            background: rgba(0, 0, 0, 0.38);
            border-radius: 18px;
            padding: 24px 22px;
            box-shadow: 0 18px 30px rgba(0,0,0,0.22);
        }

        input {
            width: 100%;
            padding: 12px 14px;
            margin-top: 6px;
            border: 1px solid rgba(255,255,255,0.18);
            border-radius: 10px;
            box-sizing: border-box;
        }

        button {
            padding: 12px 18px;
            border: none;
            border-radius: 10px;
            background: #ffbf3c;
            color: #1a2430;
            font-weight: bold;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="page-shell">
    <header class="navbar">
        <div class="navbar-brand">
            <img src="logo.png" alt="Logo">
        </div>
        <div class="navbar-title">Login</div>
    </header>
    <div class="card">
    <h2>Masukkan Kode Voucher dan Nama Anda</h2>

    <?php if (isset($error)): ?>
        <p style="color: red;"><?= htmlspecialchars($error) ?></p>
    <?php endif; ?>

    <form method="post">
        <label for="name">Nama:</label>
        <input type="text" name="name" required><br><br>

        <label for="voucher">Voucher:</label>
        <input type="text" name="voucher" required><br><br>

        <button type="submit">Mainkan</button>
    </form>
    </div>
    </div>
</body>
</html>
