
<?php
// Koneksi ke database (pastikan variabel $db sudah dibuat sebelumnya)
// require 'koneksi.php'; // Uncomment jika diperlukan

// Handle update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action']) && $_POST['action'] === 'update' && isset($_POST['update_id'])) {
        // Validasi sederhana
        $draw_date = $_POST['draw_date'];
        if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $draw_date)) {
            echo '<div class="alert alert-danger">Format tanggal tidak valid.</div>';
        } else {
            $updateStmt = $db->prepare("
                UPDATE lottery_results SET 
                    draw_date = :draw_date, 
                    draw_number = :draw_number, 
                    first_prize = :first_prize,
                    second_prize = :second_prize,
                    third_prize = :third_prize,
                    starter_prize = :starter_prize,
                    consolation_prize = :consolation_prize
                WHERE id = :id
            ");
            $updateStmt->execute([
                ':draw_date' => $draw_date,
                ':draw_number' => $_POST['draw_number'],
                ':first_prize' => $_POST['first_prize'],
                ':second_prize' => $_POST['second_prize'],
                ':third_prize' => $_POST['third_prize'],
                ':starter_prize' => $_POST['starter_prize'],
                ':consolation_prize' => $_POST['consolation_prize'],
                ':id' => $_POST['update_id']
            ]);

            echo '<div class="alert alert-success">Data berhasil diperbarui.</div>';
        }
    }

    // Handle delete
    if (isset($_POST['action']) && $_POST['action'] === 'delete' && isset($_POST['delete_id'])) {
        $deleteStmt = $db->prepare("DELETE FROM lottery_results WHERE id = :id");
        $deleteStmt->execute([':id' => $_POST['delete_id']]);
        echo '<div class="alert alert-warning">Data berhasil dihapus.</div>';
    }
}

// Ambil data pasaran Bandung
$results = $db->prepare("SELECT * FROM lottery_results WHERE market = 'Bandung' ORDER BY draw_date DESC, id DESC LIMIT 50");
$results->execute();
$history = $results->fetchAll(PDO::FETCH_ASSOC);
?>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<div class="container mt-4">
    <h3 class="mb-3">Riwayat Hasil Pasaran Bandung</h3>

    <?php if (empty($history)): ?>
        <div class="alert alert-info">Belum ada data hasil untuk pasaran Bandung.</div>
    <?php else: ?>
        <div class="table-responsive">
            <table class="table table-bordered table-striped table-sm text-center align-middle">
                <thead class="table-dark">
                    <tr>
                        <th>Tanggal</th>
                        <th>Draw No.</th>
                        <th>1st Prize</th>
                        <th>2nd Prize</th>
                        <th>3rd Prize</th>
                        <th>Starter</th>
                        <th>Consolation</th>
                        <th colspan="2">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($history as $result): ?>
                        <tr>
    <form method="POST">
        <td><input type="date" name="draw_date" value="<?= htmlspecialchars($result['draw_date']) ?>" class="form-control" required></td>
        <td><input type="text" name="draw_number" value="<?= htmlspecialchars($result['draw_number']) ?>" class="form-control" required></td>
        <td><input type="text" name="first_prize" value="<?= htmlspecialchars($result['first_prize']) ?>" class="form-control" required></td>
        <td><input type="text" name="second_prize" value="<?= htmlspecialchars($result['second_prize']) ?>" class="form-control" required></td>
        <td><input type="text" name="third_prize" value="<?= htmlspecialchars($result['third_prize']) ?>" class="form-control" required></td>
        <td><input type="text" name="starter_prize" value="<?= htmlspecialchars($result['starter_prize']) ?>" class="form-control" required></td>
        <td><input type="text" name="consolation_prize" value="<?= htmlspecialchars($result['consolation_prize']) ?>" class="form-control" required></td>

        <td>
            <input type="hidden" name="update_id" value="<?= $result['id'] ?>">
            <button type="submit" name="action" value="update" class="btn btn-sm btn-primary">Update</button>
        </td>
        <td>
            <input type="hidden" name="delete_id" value="<?= $result['id'] ?>">
            <button type="submit" name="action" value="delete" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus data ini?')">Hapus</button>
        </td>
    </form>
</tr>

                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>
