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

// Ambil semua market
$markets = $db->query("SELECT * FROM markets ORDER BY id")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Kelola Pasaran - SUNDAPOOLS 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">
    <style>
        .market-card {
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            background: #f9f9f9;
        }
        .market-card:hover {
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .market-name {
            font-size: 1.3rem;
            font-weight: bold;
            color: #333;
        }
        .market-info {
            margin: 10px 0;
        }
        .market-info span {
            display: inline-block;
            margin-right: 20px;
            font-size: 0.9rem;
        }
        .market-info .label {
            color: #666;
            font-weight: 600;
        }
        .market-actions {
            margin-top: 15px;
        }
        .market-actions a {
            margin-right: 10px;
        }
        .badge-status {
            font-size: 0.8rem;
            padding: 5px 10px;
        }
        .nav-links {
            margin-bottom: 20px;
        }
        .empty-state {
            text-align: center;
            padding: 50px 20px;
            color: #999;
        }
        .empty-state i {
            font-size: 3rem;
            margin-bottom: 15px;
            color: #ddd;
        }
    </style>
</head>
<body>
<?php include 'navbar.php'; ?>
<div class="container mt-4">
    <div class="row">
        <div class="col-lg-10 offset-lg-1">
            <!-- Navigasi -->
            <div class="nav-links">
                <a href="add_result.php" class="btn btn-primary btn-sm">
                    <i class="fas fa-plus-circle"></i> Tambah Result
                </a>
                <a href="manage_markets.php" class="btn btn-success btn-sm">
                    <i class="fas fa-cog"></i> Kelola Pasaran
                </a>
            </div>

            <div class="card">
                <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
                    <h4><i class="fas fa-cog"></i> Kelola Pasaran</h4>
                    <a href="add_market.php" class="btn btn-light btn-sm">
                        <i class="fas fa-plus"></i> Tambah Pasaran Baru
                    </a>
                </div>
                <div class="card-body">
                    <?php if (isset($_SESSION['success_message'])): ?>
                        <div class="alert alert-success"><?= $_SESSION['success_message'] ?></div>
                        <?php unset($_SESSION['success_message']); ?>
                    <?php endif; ?>
                    <?php if (isset($_SESSION['error_message'])): ?>
                        <div class="alert alert-danger"><?= $_SESSION['error_message'] ?></div>
                        <?php unset($_SESSION['error_message']); ?>
                    <?php endif; ?>

                    <?php if (count($markets) > 0): ?>
                        <?php foreach ($markets as $market): 
                            // Ambil draw number terakhir
                            $stmt = $db->prepare("SELECT draw_number FROM lottery_results WHERE market_id = ? ORDER BY draw_date DESC, draw_number DESC LIMIT 1");
                            $stmt->execute([$market['id']]);
                            $last_draw = $stmt->fetch(PDO::FETCH_ASSOC);
                            $last_draw_number = $last_draw ? $last_draw['draw_number'] : '0000';
                            
                            // Ambil jumlah result
                            $stmt = $db->prepare("SELECT COUNT(*) FROM lottery_results WHERE market_id = ?");
                            $stmt->execute([$market['id']]);
                            $total_results = $stmt->fetchColumn();
                        ?>
                        <div class="market-card">
                            <div class="row">
                                <div class="col-md-7">
                                    <div class="market-name">
                                        <?= htmlspecialchars($market['market_name']) ?>
                                        <span class="badge badge-secondary">ID: <?= $market['id'] ?></span>
                                    </div>
                                    <div class="market-info">
                                        <span><span class="label">Display:</span> <?= htmlspecialchars($market['display_name'] ?? '-') ?></span>
                                        <span><span class="label">Result Time:</span> <?= substr($market['result_time'] ?? '--:--', 0, 5) ?></span>
                                        <span><span class="label">Last Draw:</span> <?= $last_draw_number ?></span>
                                        <span><span class="label">Total Results:</span> <?= $total_results ?></span>
                                    </div>
                                    <div class="market-info">
                                        <span><span class="label">Close:</span> <?= substr($market['result_time'] ?? '--:--', 0, 5) ?></span>
                                        <span><span class="label">Open:</span> <?= substr($market['result_end_time'] ?? '--:--', 0, 5) ?></span>
                                        <span><span class="label">Auto Generate:</span> <?= substr($market['auto_generate_start'] ?? '--:--', 0, 5) ?> - <?= substr($market['auto_generate_end'] ?? '--:--', 0, 5) ?></span>
                                    </div>
                                </div>
                                <div class="col-md-5 text-right">
                                    <div class="market-actions">
                                        <a href="add_result.php?pasaran=<?= urlencode($market['market_name']) ?>" class="btn btn-sm btn-primary">
                                            <i class="fas fa-plus"></i> Tambah Result
                                        </a>
                                        <a href="edit_market_display.php?id=<?= $market['id'] ?>" class="btn btn-sm btn-warning">
                                            <i class="fas fa-edit"></i> Edit
                                        </a>
                                        <a href="delete_market.php?id=<?= $market['id'] ?>" 
                                           onclick="return confirm('Yakin ingin menghapus market <?= htmlspecialchars($market['market_name']) ?>? Semua data result akan ikut terhapus!')" 
                                           class="btn btn-sm btn-danger">
                                            <i class="fas fa-trash"></i> Hapus
                                        </a>
                                    </div>
                                    <div class="mt-2">
                                        <span class="badge badge-<?= ($market['countdown_active'] ?? 1) ? 'success' : 'danger' ?> badge-status">
                                            <i class="fas fa-<?= ($market['countdown_active'] ?? 1) ? 'check-circle' : 'times-circle' ?>"></i>
                                            <?= ($market['countdown_active'] ?? 1) ? 'Countdown Aktif' : 'Countdown Nonaktif' ?>
                                        </span>
                                        <span class="badge badge-<?= ($market['status'] ?? 1) ? 'success' : 'secondary' ?> badge-status">
                                            <i class="fas fa-<?= ($market['status'] ?? 1) ? 'check-circle' : 'times-circle' ?>"></i>
                                            <?= ($market['status'] ?? 1) ? 'Aktif' : 'Nonaktif' ?>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="empty-state">
                            <i class="fas fa-store"></i>
                            <h5>Belum ada pasaran</h5>
                            <p>Klik tombol "Tambah Pasaran Baru" untuk membuat pasaran pertama.</p>
                            <a href="add_market.php" class="btn btn-success">
                                <i class="fas fa-plus"></i> Tambah Pasaran Baru
                            </a>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>