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

<!DOCTYPE html>
<html>
<head>
    <title>Daftar Market</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>Daftar Market</h2>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Market Name</th>
                <th>Display Name</th>
                <th>Result Time</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
        <?php
        try {
            $stmt = $db->query("SELECT * FROM markets ORDER BY id");
            $markets = $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            foreach ($markets as $market):
        ?>
            <tr>
                <td><?= $market['id'] ?></td>
                <td><?= htmlspecialchars($market['market_name']) ?></td>
                <td><?= htmlspecialchars($market['display_name'] ?? $market['market_name']) ?></td>
                <td><?= $market['result_time'] ?? '-' ?></td>
                <td>
                    <a href="test_delete_direct.php?id=<?= $market['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Hapus market ini?')">
                        <i class="fas fa-trash"></i> Hapus
                    </a>
                </td>
            </tr>
        <?php 
            endforeach;
        } catch (Exception $e) {
            echo "<tr><td colspan='5'>Error: " . $e->getMessage() . "</td></tr>";
        }
        ?>
        </tbody>
    </table>
    <a href="add_result.php" class="btn btn-secondary">Kembali</a>
</div>
</body>
</html>