<div class="py-5 bg-light">
    <div class="container-xl">
        <div class="d-flex justify-content-between align-items-end mb-4">
            <div>
                <h2 class="fw-bold m-0">Agenda Terdekat</h2>
                <p class="text-muted m-0">Jangan lewatkan kesempatan belajar terbaru</p>
            </div>
            <a href="seminars/" class="text-decoration-none fw-bold">
                Lihat Semua <i class="bi bi-chevron-right ms-1"></i>
            </a>
        </div>
        
        <div class="row g-4">
            <?php
            // Query mengambil data seminar yang aktif, diurutkan dari yang terbaru
            $sql_seminar = mysql_query("SELECT * FROM seminar WHERE aktif = 'Y' ORDER BY waktu_mulai DESC LIMIT 3");
            
            while ($row = mysql_fetch_array($sql_seminar)) {
                // Logika Status Badge
                $skrg = date('Y-m-d H:i:s');
                if ($skrg < $row['waktu_mulai']) {
                    $status_badge = '<span class="badge bg-success">Open</span>';
                    $btn_class = 'btn-primary';
                    $btn_text = 'Detail & Daftar';
                    $btn_disabled = '';
                } elseif ($skrg >= $row['waktu_mulai'] && $skrg <= $row['waktu_akhir']) {
                    $status_badge = '<span class="badge bg-warning text-dark">Berlangsung</span>';
                    $btn_class = 'btn-primary';
                    $btn_text = 'Sedang Berlangsung';
                    $btn_disabled = '';
                } else {
                    $status_badge = '<span class="badge bg-secondary">Selesai</span>';
                    $btn_class = 'btn-outline-secondary';
                    $btn_text = 'Selesai';
                    $btn_disabled = 'disabled';
                }

                // Cek Gambar
                $gambar = (!empty($row['gambar'])) ? $img . $row['gambar'] : 'https://images.unsplash.com/photo-1544531586-fde5298cdd40?auto=format&fit=crop&q=80&w=800';
            ?>
            
            <div class="col-md-4">
                <div class="card h-100 border-0 shadow-sm hover-card overflow-hidden">
                    <div class="position-relative" style="height: 200px;">
                        <img src="<?php echo $gambar; ?>" alt="<?php echo $row['judul']; ?>" class="w-100 h-100 object-fit-cover">
                        <div class="position-absolute top-0 end-0 p-2">
                            <?php echo $status_badge; ?>
                        </div>
                    </div>
                    <div class="card-body d-flex flex-column p-4">
                        <div class="d-flex gap-3 text-muted small mb-2">
                            <span class="d-flex align-items-center gap-1">
                                <i class="bi bi-calendar" style="font-size: 0.875rem;"></i> 
                                <?php echo date('d M Y', strtotime($row['waktu_mulai'])); ?>
                            </span>
                            <span class="d-flex align-items-center gap-1 text-capitalize">
                                <i class="bi bi-camera-video" style="font-size: 0.875rem;"></i> 
                                <?php echo $row['metode']; ?>
                            </span>
                        </div>
                        <h5 class="card-title fw-bold mb-2"><?php echo $row['judul']; ?></h5>
                        <p class="card-text text-muted small flex-grow-1">
                            <?php echo substr(strip_tags($row['deskripsi']), 0, 100); ?>...
                        </p>
                        
                        <hr class="opacity-10 my-3">
                        
                        <div class="mb-3 d-flex justify-content-between align-items-end">
                            <div>
                                <small class="text-muted d-block" style="font-size: 0.7rem;">UMUM</small> 
                                <span class="text-muted small">
                                    <?php echo ($row['biaya_umum'] > 0) ? 'Rp '.number_format($row['biaya_umum'], 0, ',', '.') : 'GRATIS'; ?>
                                </span>
                            </div>
                            <div class="text-end">
                                <small class="text-primary fw-bold d-block" style="font-size: 0.7rem;">MEMBER</small> 
                                <span class="text-primary fw-bold">
                                    <?php echo ($row['biaya_anggota'] > 0) ? 'Rp '.number_format($row['biaya_anggota'], 0, ',', '.') : 'GRATIS'; ?>
                                </span>
                            </div>
                        </div>

                        <a href="seminar-detail.php?id=<?php echo $row['id']; ?>" class="btn w-100 fw-medium <?php echo $btn_class . ' ' . $btn_disabled; ?>">
                            <?php echo $btn_text; ?>
                        </a>
                    </div>
                </div>
            </div>
            
            <?php } // End loop ?>
        </div>
    </div>
</div>