<?php
session_start();
require_once 'config.php';

// Cek login
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    header('Location: login.php');
    exit;
}

// Ambil semua hasil dari database
$stmt = $db->prepare("SELECT * FROM lottery_results ORDER BY draw_date DESC");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>History Result - Admin Panel</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
    <h2 class="mb-4">History Result</h2>
    <table class="table table-bordered table-striped">
        <thead class="thead-dark">
            <tr>
                <th>ID</th>
                <th>Pasaran</th>
                <th>Tanggal</th>
                <th>1st Prize</th>
                <th>2nd Prize</th>
                <th>3rd Prize</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($results as $result): ?>
            <tr>
                <td><?= $result['id'] ?></td>
                <td><?= htmlspecialchars($result['market']) ?></td>
                <td><?= date('d-m-Y', strtotime($result['draw_date'])) ?></td>
                <td><?= htmlspecialchars($result['first_prize']) ?></td>
                <td><?= htmlspecialchars($result['second_prize']) ?></td>
                <td><?= htmlspecialchars($result['third_prize']) ?></td>
                <td>
                    <a href="edit_result.php?id=<?= $result['id'] ?>" class="btn btn-warning btn-sm">Edit</a>
                    <a href="delete_result.php?id=<?= $result['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus data ini?');">Hapus</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
</body>
</html>
