<?php
// Atur judul halaman
$page_title = "Events";

// Sertakan file koneksi database
require 'config/db.php';

// Sertakan file header
include 'templates/header.php';

// Inisialisasi variabel pencarian
$search_term = '';
$sql = "SELECT * FROM events ORDER BY event_date DESC";

// Cek jika ada input pencarian dari form
if (isset($_GET['search']) && !empty(trim($_GET['search']))) {
    $search_term = trim($_GET['search']);
    
    // Query diubah untuk mencari berdasarkan judul, deskripsi, atau lokasi
    $sql = "SELECT * FROM events WHERE title LIKE ? OR description LIKE ? OR location LIKE ? ORDER BY event_date DESC";
    
    $stmt = $conn->prepare($sql);
    $search_like = "%" . $search_term . "%";
    $stmt->bind_param("sss", $search_like, $search_like, $search_like);
    $stmt->execute();
    $result = $stmt->get_result();

} else {
    // Jika tidak ada pencarian, jalankan query biasa
    $result = $conn->query($sql);
}

?>

<main class="bg-white pt-16">
    <div class="bg-gradient-to-r from-blue-500 to-purple-500 text-white py-16">
        <div class="container mx-auto px-4 text-center">
            <h1 class="text-4xl font-bold">Kalender Acara</h1>
            <p class="mt-2 text-lg">Ikuti Acara-Acara Pilihan dari BrightEd untuk Mengembangkan Wawasan Bisnis Anda.</p>
        </div>
    </div>

    <div class="container mx-auto px-4 py-12">
        
        <div class="mb-8 p-6 bg-white rounded-lg shadow-md border">
            <form action="events.php" method="GET" class="flex flex-col sm:flex-row gap-4">
                <input 
                    type="text" 
                    name="search" 
                    placeholder="Cari nama acara, topik, atau lokasi..." 
                    class="w-full px-4 py-3 border border-gray-300 rounded-md focus:ring-blue-500 focus:border-blue-500"
                    value="<?php echo htmlspecialchars($search_term); ?>"
                >
                <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-6 rounded-md transition duration-300">
                    <i class="fas fa-search mr-2"></i>Cari
                </button>
            </form>
        </div>

        <?php if (!empty($search_term)): ?>
            <h2 class="text-2xl font-semibold mb-6 text-gray-800">Hasil pencarian untuk: "<?php echo htmlspecialchars($search_term); ?>"</h2>
        <?php endif; ?>

        <div class="space-y-8">
            
            <?php if ($result && $result->num_rows > 0): ?>
                <?php while($event = $result->fetch_assoc()): ?>
                    <div class="bg-white rounded-lg shadow-lg overflow-hidden md:flex transform hover:shadow-2xl transition-shadow duration-300">
                        <div class="md:w-1/3">
                            <img src="<?php echo htmlspecialchars($event['image_url']); ?>" alt="Gambar <?php echo htmlspecialchars($event['title']); ?>" class="w-full h-full object-cover">
                        </div>
                        <div class="p-6 md:w-2/3">
                            <p class="text-sm text-red-600 font-semibold mb-2">
                                <i class="fas fa-calendar-alt mr-2"></i><?php echo date('d F Y, H:i', strtotime($event['event_date'])); ?> WIB
                            </p>
                            <h3 class="text-2xl font-bold text-gray-900 mb-2"><?php echo htmlspecialchars($event['title']); ?></h3>
                            <p class="text-sm text-gray-500 mb-3"><i class="fas fa-map-marker-alt mr-2"></i><?php echo htmlspecialchars($event['location']); ?></p>
                            <p class="text-gray-600 mb-4">
                                <?php echo htmlspecialchars($event['description']); ?>
                            </p>
                            <a href="#" class="inline-block bg-gradient-to-r from-blue-600 to-purple-600 hover:from-blue-700 hover:to-purple-700 text-white font-bold py-2 px-5 rounded-md transition duration-300">
                                Daftar Sekarang <i class="fas fa-arrow-right ml-2"></i>
                            </a>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php else: ?>
                <div class="text-center text-gray-500 col-span-full bg-yellow-50 p-8 rounded-lg">
                    <i class="fas fa-exclamation-triangle fa-3x text-yellow-400 mb-4"></i>
                    <p class="text-xl">Data tidak ditemukan.</p>
                    <?php if (!empty($search_term)): ?>
                        <p>Tidak ada acara yang cocok dengan kata kunci "<?php echo htmlspecialchars($search_term); ?>".</p>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
            
        </div>
    </div>
</main>


<?php
// Sertakan file footer
include 'templates/footer.php';
if (isset($stmt)) $stmt->close(); // Tutup statement jika ada
$conn->close(); // Tutup koneksi database
?>