<?php
// File: /admin/competitions.php
include 'header.php'; // Includes security check and db connection

// Fetch all existing competitions to display in a table
$competitions = [];
$sql = "SELECT id, title, topic, start_date, end_date, competition_fee FROM competitions ORDER BY start_date DESC";
$result = $conn->query($sql);
if ($result) {
    $competitions = $result->fetch_all(MYSQLI_ASSOC);
}
?>

<div class="p-6 md:p-8 bg-gray-50 min-h-screen">
    <h1 class="text-3xl font-bold text-ucf-charcoal mb-8">Manage Competitions</h1>

    <!-- Section to Add New Competition -->
    <div class="bg-white p-8 rounded-lg shadow-md mb-8">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-6">Add New Competition</h2>
        <form action="competition_handler.php" method="POST">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <input type="text" name="title" placeholder="Competition Title" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-ucf-green">
                <input type="text" name="topic" placeholder="Topic" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-ucf-green">
                <textarea name="description" placeholder="Description" required rows="4" class="md:col-span-2 w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-ucf-green"></textarea>
                <div>
                    <label class="block text-gray-700">Start Date</label>
                    <input type="date" name="start_date" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-ucf-green">
                </div>
                <div>
                    <label class="block text-gray-700">End Date</label>
                    <input type="date" name="end_date" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-ucf-green">
                </div>
                <input type="number" name="competition_fee" placeholder="Entry Fee (e.g., 500)" required class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-ucf-green">
            </div>
            <button type="submit" name="add_competition" class="mt-6 bg-ucf-green text-white font-bold py-3 px-6 rounded-lg hover:bg-ucf-green-dark transition-colors duration-300">
                Create Competition
            </button>
        </form>
    </div>

    <!-- Section to View Existing Competitions -->
    <div class="bg-white p-8 rounded-lg shadow-md">
        <h2 class="text-2xl font-bold text-ucf-charcoal mb-6">Existing Competitions</h2>
        <div class="overflow-x-auto">
            <table class="w-full text-left">
                <thead>
                    <tr class="border-b">
                        <th class="py-3 px-4">Title</th>
                        <th class="py-3 px-4">Deadline</th>
                        <th class="py-3 px-4">Fee</th>
                        <th class="py-3 px-4">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($competitions as $comp): ?>
                    <tr class="border-b hover:bg-gray-50">
                        <td class="py-3 px-4 font-semibold"><?php echo htmlspecialchars($comp['title']); ?></td>
                        <td class="py-3 px-4"><?php echo date("M d, Y", strtotime($comp['end_date'])); ?></td>
                        <td class="py-3 px-4">₹<?php echo number_format($comp['competition_fee']); ?></td>
                        <td class="py-3 px-4">
                            <a href="competition_entries.php?id=<?php echo $comp['id']; ?>" class="text-ucf-green hover:underline">View Entries</a>
                            <!-- Add Edit/Delete functionality here later if needed -->
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
