<?php 
// File: /admin/competition_entries.php
include 'header.php';
require_once '../db.php';

$competition_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($competition_id <= 0) {
    echo "<div class='p-6 text-center text-red-600 font-bold'>Invalid competition ID!</div>";
    include 'footer.php';
    exit();
}

// Fetch competition details
$comp_stmt = $conn->prepare("SELECT * FROM competitions WHERE id = ?");
$comp_stmt->bind_param("i", $competition_id);
$comp_stmt->execute();
$competition = $comp_stmt->get_result()->fetch_assoc();
$comp_stmt->close();

if (!$competition) {
    echo "<div class='p-6 text-center text-red-600 font-bold'>Competition not found!</div>";
    include 'footer.php';
    exit();
}

// Fetch competition entries
$stmt = $conn->prepare("SELECT * FROM competition_entries WHERE competition_id = ?");
$stmt->bind_param("i", $competition_id);
$stmt->execute();
$entries = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>

<div class="p-8 bg-white rounded-lg shadow-md">
  <div class="flex items-center justify-between mb-6">
    <h1 class="text-3xl font-bold text-ucf-charcoal">
      Entries for: <?= htmlspecialchars($competition['title']) ?>
    </h1>

    <!-- Send All Certificates Button -->
    <form action="generate_all_certificates.php" method="POST" 
          onsubmit="return confirm('Send certificates to ALL participants?');">
      <input type="hidden" name="competition_id" value="<?= $competition_id ?>">
      <button type="submit" class="bg-ucf-green hover:bg-ucf-green-dark text-white font-semibold px-4 py-2 rounded">
        <i class="fas fa-award mr-2"></i>Send All Certificates
      </button>
    </form>
  </div>

  <?php if (empty($entries)): ?>
      <p class="text-gray-600 text-center py-6">No entries found for this competition.</p>
  <?php else: ?>
  <div class="overflow-x-auto">
      <table class="min-w-full border border-gray-200 text-sm">
        <thead class="bg-gray-100 text-gray-700">
          <tr>
            <th class="px-4 py-2 border">#</th>
            <th class="px-4 py-2 border">Name</th>
            <!-- ⭐️ NEW: Added Photo Columns -->
            <th class="px-4 py-2 border">Photo</th>
            <th class="px-4 py-2 border">Artwork</th>
            <!-- End New Columns -->
            <th class="px-4 py-2 border">Email</th>
            <th class="px-4 py-2 border">Phone</th>
            <th class="px-4 py-2 border">Position</th>
            <th class="px-4 py-2 border">Status</th>
            <th class="px-4 py-2 border">Certificate</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($entries as $i => $entry): ?>
            <tr class="hover:bg-gray-50">
              <td class="px-4 py-2 border text-center"><?= $i + 1 ?></td>
              
              <td class="px-4 py-2 border font-medium">
                  <?= htmlspecialchars($entry['name']) ?>
                  <br>
                  <span class="text-xs text-gray-500">ID: <?= $entry['id'] ?></span>
              </td>
              
              <!-- ⭐️ NEW: User Photo Column -->
              <td class="px-4 py-2 border text-center">
                <?php if (!empty($entry['user_photo_path'])): ?>
                    <a href="../<?= htmlspecialchars($entry['user_photo_path']) ?>" target="_blank">
                        <img src="../<?= htmlspecialchars($entry['user_photo_path']) ?>" alt="User" class="h-12 w-12 object-cover rounded-full border border-gray-300 hover:scale-110 transition-transform">
                    </a>
                <?php else: ?>
                    <span class="text-xs text-gray-400 italic">No Photo</span>
                <?php endif; ?>
              </td>

              <!-- ⭐️ NEW: Artwork Photo Column -->
              <td class="px-4 py-2 border text-center">
                <?php if (!empty($entry['artwork_photo_path'])): ?>
                    <a href="../<?= htmlspecialchars($entry['artwork_photo_path']) ?>" target="_blank">
                        <img src="../<?= htmlspecialchars($entry['artwork_photo_path']) ?>" alt="Artwork" class="h-16 w-24 object-cover rounded border border-gray-300 hover:scale-110 transition-transform">
                    </a>
                <?php else: ?>
                    <span class="text-xs text-gray-400 italic">No Artwork</span>
                <?php endif; ?>
              </td>

              <td class="px-4 py-2 border"><?= htmlspecialchars($entry['email']) ?></td>
              <td class="px-4 py-2 border"><?= htmlspecialchars($entry['phone']) ?></td>
              
              <td class="px-4 py-2 border">
                <form action="generate_certificate.php" method="POST" class="flex flex-col space-y-2">
                  <input type="hidden" name="entry_id" value="<?= $entry['id'] ?>">
                  <input type="hidden" name="competition_id" value="<?= $competition_id ?>">
                  <select name="position" class="border rounded px-2 py-1 text-xs w-full">
                    <option value="">None</option>
                    <option value="1" <?= ($entry['position'] ?? '') == '1' ? 'selected' : '' ?>>1st Place</option>
                    <option value="2" <?= ($entry['position'] ?? '') == '2' ? 'selected' : '' ?>>2nd Place</option>
                    <option value="3" <?= ($entry['position'] ?? '') == '3' ? 'selected' : '' ?>>3rd Place</option>
                    <option value="Consolation" <?= ($entry['position'] ?? '') == 'Consolation' ? 'selected' : '' ?>>Consolation</option>
                  </select>
                  <button type="submit" class="bg-blue-600 text-white px-3 py-1 rounded text-xs hover:bg-blue-700 w-full">
                    Save/Gen Cert
                  </button>
                </form>
              </td>
              
              <td class="px-4 py-2 border text-center">
                  <?php if ($entry['payment_status'] === 'Success' || $entry['payment_status'] === 'Paid' || $entry['payment_status'] === 'Free'): ?>
                      <span class="px-2 py-1 text-xs font-semibold leading-tight text-green-700 bg-green-100 rounded-full">
                          <?= htmlspecialchars($entry['payment_status']) ?>
                      </span>
                  <?php else: ?>
                      <span class="px-2 py-1 text-xs font-semibold leading-tight text-red-700 bg-red-100 rounded-full">
                          <?= htmlspecialchars($entry['payment_status']) ?>
                      </span>
                  <?php endif; ?>
              </td>
              
              <td class="px-4 py-2 border text-center">
                <?php if (!empty($entry['certificate_path'])): ?>
                   <a href="../<?= htmlspecialchars($entry['certificate_path']) ?>" class="text-blue-600 hover:underline text-xs font-semibold" target="_blank">
                       <i class="fas fa-file-pdf mr-1"></i> View
                   </a>
                <?php else: ?>
                  <span class="text-gray-400 text-xs">-</span>
                <?php endif; ?>
              </td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
  </div>
  <?php endif; ?>
</div>

<?php include 'footer.php'; ?>