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

// Handle actions
if(isset($_GET['action']) && isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    
    if($_GET['action'] == 'delete') {
        // Check if market has results
        $stmt = $db->prepare("SELECT COUNT(*) FROM lottery_results WHERE market_id = ?");
        $stmt->execute([$id]);
        
        if($stmt->fetchColumn() > 0) {
            $_SESSION['error'] = 'Cannot delete market with existing results!';
        } else {
            $stmt = $db->prepare("DELETE FROM markets WHERE id = ?");
            if($stmt->execute([$id])) {
                $_SESSION['success'] = 'Market deleted successfully!';
            } else {
                $_SESSION['error'] = 'Error deleting market!';
            }
        }
        redirect('market_list.php');
    }
}

$markets = $db->query("SELECT * FROM markets ORDER BY market_name")->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Daftar Pasaran - SOLOPOOLS Admin</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap4.min.css">
</head>
<body>
    <?php include 'navbar.php'; ?>
    
    <div class="container mt-4">
        <div class="card">
            <div class="card-header">
                <h4>Market List</h4>
                <a href="add_market.php" class="btn btn-primary">Add New Market</a>
            </div>
            <div class="card-body">
                <?php if(isset($_SESSION['error'])): ?>
                    <div class="alert alert-danger"><?= $_SESSION['error']; unset($_SESSION['error']); ?></div>
                <?php endif; ?>
                
                <?php if(isset($_SESSION['success'])): ?>
                    <div class="alert alert-success"><?= $_SESSION['success']; unset($_SESSION['success']); ?></div>
                <?php endif; ?>
                
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Market Name</th>
                            <th>Result Time</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($markets as $market): ?>
                        <tr>
                            <td><?= $market['id'] ?></td>
                            <td><?= htmlspecialchars($market['market_name']) ?></td>
                            <td><?= date('H:i', strtotime($market['result_time'])) ?></td>
                            <td><?= $market['is_active'] ? 'Active' : 'Inactive' ?></td>
                            <td>
                                <a href="add_market.php?id=<?= $market['id'] ?>" class="btn btn-sm btn-warning">Edit</a>
                                <a href="market_list.php?action=delete&id=<?= $market['id'] ?>" 
                                   class="btn btn-sm btn-danger" 
                                   onclick="return confirm('Are you sure?')">Delete</a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>