<?php
// File: competition.php
include 'header.php';

// --- DYNAMICALLY FETCH COMPETITIONS ---
$competitions = [];
$result = $conn->query("SELECT * FROM competitions ORDER BY start_date DESC");
if ($result) {
    while($row = $result->fetch_assoc()) {
        // Determine status
        $row['status'] = (strtotime($row['end_date']) > time()) ? 'Open for Entries' : 'Registration Closed';
        $competitions[] = $row;
    }
}
?>

<div class="bg-gray-50 py-12 md:py-20">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="text-center mb-12">
            <h1 class="text-4xl font-extrabold text-ucf-charcoal sm:text-5xl tracking-tight">Competitions & Events</h1>
            <p class="mt-4 max-w-2xl mx-auto text-xl text-gray-500">
                Challenge yourself and showcase your talent in our exciting art competitions.
            </p>
        </div>

        <?php if (!empty($competitions)): ?>
            <div class="space-y-8">
                <?php foreach ($competitions as $comp): ?>
                    <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 bg-ucf-charcoal text-white p-8 flex flex-col justify-center items-center text-center">
                            <i class="fas fa-trophy text-5xl text-yellow-400 mb-4"></i>
                            <h3 class="text-2xl font-bold"><?php echo htmlspecialchars($comp['title']); ?></h3>
                            <span class="mt-2 inline-block <?php echo $comp['status'] === 'Open for Entries' ? 'bg-green-500' : 'bg-red-500'; ?> text-white text-sm font-semibold px-3 py-1 rounded-full">
                                <?php echo htmlspecialchars($comp['status']); ?>
                            </span>
                        </div>
                        <div class="md:w-2/3 p-8">
                            <p class="text-sm font-semibold text-ucf-green uppercase">Topic: <?php echo htmlspecialchars($comp['rules']); ?></p>
                            <p class="text-gray-600 mt-4"><?php echo htmlspecialchars($comp['description']); ?></p>
                            <div class="mt-6 border-t border-gray-200 pt-6 flex flex-wrap gap-4 items-center justify-between">
                                <div class="text-sm text-gray-500">
                                    <p><strong class="text-ucf-charcoal">Age Group:</strong> All Ages</p> 
                                    
                                    <!-- ⭐️ FIX: Display Dynamic Fee from DB -->
                                    <p>
                                        <strong class="text-ucf-charcoal">Entry Fee:</strong> 
                                        <?php 
                                            // Use the entry_fee column. Default to 0 if missing.
                                            $fee = isset($comp['entry_fee']) ? (float)$comp['entry_fee'] : 0;
                                            if ($fee > 0) {
                                                echo '₹' . number_format($fee, 2); 
                                            } else {
                                                echo '<span class="text-green-600 font-bold">FREE</span>';
                                            }
                                        ?>
                                    </p>
                                    
                                    <p><strong class="text-ucf-charcoal">Deadline:</strong> <?php echo date('F d, Y', strtotime($comp['end_date'])); ?></p>
                                </div>
                                <div>
                                   <a href="competition_register.php?id=<?php echo $comp['id']; ?>" 
                                      class="inline-block bg-ucf-green text-white font-bold py-3 px-6 rounded-lg hover:bg-ucf-green-dark transition-colors 
                                      <?php echo $comp['status'] !== 'Open for Entries' ? 'opacity-50 pointer-events-none' : ''; ?>">
                                      Participate Now
                                   </a>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="text-center bg-white p-12 rounded-lg shadow-md">
                <i class="fas fa-trophy fa-4x text-gray-300 mb-4"></i>
                <h2 class="text-2xl font-bold text-ucf-charcoal">No Competitions Available</h2>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php include 'footer.php'; ?>