<?php
require_once 'admin/config.php';

try {
    $db = new PDO('mysql:host=localhost;dbname=erniinrk_bekasi', 'erniinrk_bekasi', 'Kelentit123@');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

$bulanIndo = [
    'January' => 'Januari', 'February' => 'Februari', 'March' => 'Maret',
    'April' => 'April', 'May' => 'Mei', 'June' => 'Juni',
    'July' => 'Juli', 'August' => 'Agustus', 'September' => 'September',
    'October' => 'Oktober', 'November' => 'November', 'December' => 'Desember'
];

function formatDisplayDate($date, $bulanIndo) {
    if (!$date) return ['day' => '--', 'month' => '--', 'year' => '--'];
    $timestamp = strtotime($date);
    return [
        'day' => date('d', $timestamp),
        'month' => $bulanIndo[date('F', $timestamp)] ?? date('F', $timestamp),
        'year' => date('Y', $timestamp)
    ];
}

$market_name = isset($_GET['name']) ? $_GET['name'] : '';
try {
    $stmt = $db->prepare("SELECT * FROM markets WHERE market_name = ?");
    $stmt->execute([$market_name]);
    $market = $stmt->fetch(PDO::FETCH_ASSOC);
   
    if (!$market) die("Market not found");
   
    if (empty($market['display_name'])) {
        $market['display_name'] = strtoupper($market['market_name']);
    }
    if (empty($market['color_theme'])) {
        $market['color_theme'] = 'bg-color-1';
    }

    $limit = 40;
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
    if ($page < 1) $page = 1;
    $offset = ($page - 1) * $limit;
} catch (Exception $e) {
    die("Error fetching market: " . $e->getMessage());
}

try {
    $stmt = $db->prepare("SELECT COUNT(*) FROM lottery_results WHERE market = ?");
    $stmt->execute([$market_name]);
    $total_results = $stmt->fetchColumn();
    $total_pages = ceil($total_results / $limit);
} catch (Exception $e) {
    $total_results = 0;
    $total_pages = 1;
}

try {
    $stmt = $db->prepare("SELECT * FROM lottery_results WHERE market = ? ORDER BY draw_date DESC, created_at DESC LIMIT ? OFFSET ?");
    $stmt->bindValue(1, $market_name, PDO::PARAM_STR);
    $stmt->bindValue(2, $limit, PDO::PARAM_INT);
    $stmt->bindValue(3, $offset, PDO::PARAM_INT);
    $stmt->execute();
    $all_results = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    $all_results = [];
}
?>
<!DOCTYPE html>
<html lang="en-US">
<head>
    <base href="https://pastijepe.online/" />
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">		
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <meta name="description" content="BEKASI POOLS - Lottery Results & Live Draws"/>
    <title>BEKASI POOLS • Past Results</title>
    <link href="https://pastijepe.online/" rel="canonical"/>
    <link href="https://pastijepe.online/assets/img/faviconBEKASI.png" rel="icon"/>
    <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://pastijepe.online/assets/css/lottery.css">
    <link rel='stylesheet' href="https://pastijepe.online/assets/css/style.min.css" type='text/css' media='all' />
    <link rel='stylesheet' href="https://pastijepe.online/assets/css/classic-themes.min.css" type='text/css' media='all' />

    <style>

        body {
            background: #D4AF37; 
            background: linear-gradient(145deg, #F5D76E 0%, #D4AF37 40%, #B8860B 100%);
            color: #0a0a0a;
        }
        
        .gold-BEKASI {
            color: #D4AF37;
        }
        .bg-gold-BEKASI {
            background-color: #D4AF37;
        }
        .border-gold-BEKASI {
            border-color: #D4AF37;
        }
        
        .neon-gold {
            color: #0a0a0a;
            text-shadow: 0 0 10px rgba(255, 215, 0, 0.8),
                         0 0 20px rgba(255, 215, 0, 0.5);
        }
        
        .result-card {
            background: rgba(10, 10, 10, 0.95) !important;
            border: 2px solid #D4AF37 !important;
            border-radius: 20px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.3);
            transition: all 0.4s ease;
            overflow: hidden;
        }
        .result-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.6) !important;
        }
        
        .result-card .card-header {
            background: #0a0a0a !important;
            border-bottom: 1px solid #D4AF37;
            padding: 20px;
        }
        .result-card .card-header h5 {
            color: #D4AF37;
            font-weight: 700;
            letter-spacing: 1px;
        }
        
        .result-card .card-body {
            background: #0a0a0a !important;
            color: #ffffff;
        }
        .result-card .card-body .bg-light {
            background: rgba(20, 18, 10, 0.95) !important;
            border-bottom: 1px solid #D4AF37 !important;
            color: #D4AF37;
        }
        .result-card .card-body .bg-light strong {
            color: #F5D76E;
        }
        .result-card .card-body .bg-light small {
            color: #aaa;
        }
        
        .prize-digit {
            background: #D4AF37 !important;
            color: #0a0a0a !important;
            border: 2px solid #0a0a0a;
            border-radius: 10px;
            padding: 10px 14px;
            font-size: 1.9rem;
            font-weight: bold;
            min-width: 50px;
            text-align: center;
            display: inline-block;
            margin: 3px;
            box-shadow: 0 0 15px rgba(212, 175, 55, 0.3);
        }
        .prize-digit.small {
            font-size: 1.4rem;
            padding: 6px 10px;
            min-width: 40px;
        }
        
        .btn-back {
            background-color: #0a0a0a;
            color: #D4AF37;
            border: 2px solid #D4AF37;
            border-radius: 30px;
            padding: 10px 24px;
            transition: all 0.3s;
        }
        .btn-back:hover {
            background-color: #D4AF37;
            color: #0a0a0a;
            box-shadow: 0 0 20px rgba(212, 175, 55, 0.5);
        }
        
        .page-title {
            color: #0a0a0a;
            text-shadow: 0 2px 10px rgba(255, 215, 0, 0.3);
            font-weight: 800;
        }
        .page-subtitle {
            color: #0a0a0a;
            font-weight: 500;
        }
        
        .pagination .page-link {
            background-color: #0a0a0a;
            color: #D4AF37;
            border: 1px solid #D4AF37;
        }
        .pagination .page-link:hover {
            background-color: #D4AF37;
            color: #0a0a0a;
        }
        .pagination .active .page-link {
            background-color: #D4AF37;
            color: #0a0a0a;
            border-color: #D4AF37;
        }
        
        footer {
            background: #0a0a0a;
            border-top: 2px solid #D4AF37;
            color: #D4AF37;
        }
    </style>
</head>

<body class="market-results-page">

    <div class="bg-wrapper"></div>
    

    <section class="mt-5 pt-5">
        <div class="container-fluid px-4 py-5">
            <div class="row">
                <div class="col-12">
                    <a href="index.php" class="btn btn-back mb-4">
                        <i class="fas fa-arrow-left"></i> Back to Home
                    </a>

                    <div class="text-center mb-5">
                        <h1 class="display-4 font-weight-bold page-title">
                            <?= htmlspecialchars($market['display_name']) ?>
                        </h1>
                    </div>

                    <?php if (count($all_results) > 0): ?>
                        <div class="row">
                            <?php foreach ($all_results as $result): 
                                $market_date = formatDisplayDate($result['draw_date'] ?? null, $bulanIndo);
                                $month_short = substr($market_date['month'], 0, 3);
                            ?>
                            <div class="col-12 col-lg-6 col-xl-4 mb-4">
                                <div class="card result-card">
                                    <div class="card-header text-center">
                                        <h5 class="mb-0"><?= htmlspecialchars($market['display_name']) ?></h5>
                                    </div>
                                    <div class="card-body p-0">
                                        <div class="p-3 text-center border-bottom border-gold-BEKASI/30">
                                            <strong class="text-lg"><?= $month_short ?> <?= $market_date['day'] ?> <?= $market_date['year'] ?></strong><br>
                                            <small class="text-muted gold-BEKASI mb-3 font-weight-bold">Draw #<?= $result['draw_number'] ?? '--' ?></small>
                                        </div>

                                        <div class="p-4 text-center border-bottom border-gold-BEKASI/30">
                                            <h6 class="gold-BEKASI mb-3 font-weight-bold">1ST PRIZE</h6>
                                            <div class="prize-wrapper">
                                                <?php if (!empty($result['first_prize'])): ?>
                                                    <?php foreach (str_split($result['first_prize']) as $digit): ?>
                                                        <span class="prize-digit"><?= htmlspecialchars($digit) ?></span>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </div>
                                        </div>

                                        <div class="row no-gutters">
                                            <div class="col-6 border-right border-gold-BEKASI/30 p-3 text-center">
                                                <p class="gold-BEKASI mb-2 font-weight-bold">2ND PRIZE</p>
                                                <div>
                                                    <?php if (!empty($result['second_prize'])): ?>
                                                        <?php foreach (str_split($result['second_prize']) as $digit): ?>
                                                            <span class="prize-digit small"><?= htmlspecialchars($digit) ?></span>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                            <div class="col-6 p-3 text-center">
                                                <p class="gold-BEKASI mb-2 font-weight-bold">3RD PRIZE</p>
                                                <div>
                                                    <?php if (!empty($result['third_prize'])): ?>
                                                        <?php foreach (str_split($result['third_prize']) as $digit): ?>
                                                            <span class="prize-digit small"><?= htmlspecialchars($digit) ?></span>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-warning text-center py-5" style="background: #0a0a0a; border: 2px solid #D4AF37; color: #D4AF37;">
                            <h4>No results found for <?= htmlspecialchars($market['display_name']) ?></h4>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </section>

    <?php if ($total_pages > 1): ?>
    <nav class="mt-4 mb-5">
        <ul class="pagination justify-content-center">
            <?php if ($page > 1): ?>
                <li class="page-item"><a class="page-link" href="market.php?name=<?= urlencode($market_name) ?>&page=<?= $page - 1 ?>">Prev</a></li>
            <?php endif; ?>
            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                    <a class="page-link" href="market.php?name=<?= urlencode($market_name) ?>&page=<?= $i ?>"><?= $i ?></a>
                </li>
            <?php endfor; ?>
            <?php if ($page < $total_pages): ?>
                <li class="page-item"><a class="page-link" href="market.php?name=<?= urlencode($market_name) ?>&page=<?= $page + 1 ?>">Next</a></li>
            <?php endif; ?>
        </ul>
    </nav>
    <?php endif; ?>

    <footer>
        <div class="container px-3 py-4">
            <div class="text-center">
                <p class="mb-0">Must be 18+ and play responsibly.</p>
                <small>© 2026 BEKASI POOLS</small>
            </div>
        </div>
    </footer>

    <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>