<?php
// Menyertakan koneksi database
include 'cfg.php';

// Fungsi untuk menghasilkan kode voucher acak
function generateVoucher($length = 10) {
    return strtoupper(substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), 0, $length));
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Membuat voucher baru
    $voucher = generateVoucher();

    // Menentukan apakah voucher adalah voucher pemenang
    $is_winner = isset($_POST['is_winner']) ? 1 : 0;

    // Memasukkan voucher baru ke dalam tabel 'vouchers'
    $stmt = $pdo->prepare("INSERT INTO vouchers (code, is_winner, used) VALUES (?, ?, 0)");
    $stmt->execute([$voucher, $is_winner]);

    // Menampilkan voucher yang baru dibuat beserta status kemenangan
    echo "Voucher baru: " . $voucher . " (Menang: " . ($is_winner ? 'Ya' : 'Tidak') . ")";
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Generate 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: 520px;
            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.34);
            border-radius: 18px;
            padding: 24px 22px;
            box-shadow: 0 18px 30px rgba(0,0,0,0.2);
        }

        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">Voucher Generator</div>
    </header>
    <div class="card">
    <h2>Generate Voucher Baru</h2>
    
    <form method="post">
        <label>
            <input type="checkbox" name="is_winner" value="1"> Buat Voucher Pemenang
        </label>
        <br><br>
        <button type="submit">Generate Voucher</button>
    </form>
    </div>
    </div>
</body>
</html>
