<?php
// File: upload_entry.php
include 'header.php';
require_once 'db.php';

$reg_code = $_GET['code'] ?? '';
$entry = null;
$message = '';

// Check if code is submitted
if ($reg_code) {
    $stmt = $conn->prepare("SELECT * FROM competition_entries WHERE merchant_transaction_id = ? AND payment_status = 'Success'");
    $stmt->bind_param("s", $reg_code);
    $stmt->execute();
    $result = $stmt->get_result();
    $entry = $result->fetch_assoc();
    $stmt->close();

    if (!$entry) {
        $message = "Invalid or unpaid registration code. Please check and try again.";
    }
}
?>

<div class="bg-gray-50 py-12 md:py-20">
    <div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
        <div class="bg-white p-8 rounded-xl shadow-lg">
            <h1 class="text-3xl font-extrabold text-center text-ucf-charcoal mb-8">Upload Competition Entry</h1>

            <?php if (!$entry): ?>
                <!-- Step 1: Enter Code Form -->
                <form method="GET" class="space-y-6">
                    <?php if ($message): ?>
                        <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 rounded"><?php echo $message; ?></div>
                    <?php endif; ?>
                    
                    <div>
                        <label for="code" class="block text-sm font-medium text-gray-700">Enter Registration Code</label>
                        <input type="text" name="code" id="code" required value="<?php echo htmlspecialchars($reg_code); ?>" class="mt-1 block w-full rounded-md border-2 border-gray-400 shadow-sm focus:border-ucf-green focus:ring focus:ring-ucf-green focus:ring-opacity-50" placeholder="UCF-COMP-...">
                        <p class="text-sm text-gray-500 mt-1">You received this code in your confirmation email.</p>
                    </div>
                    <button type="submit" class="w-full bg-ucf-green text-white font-bold py-3 px-6 rounded-lg hover:bg-ucf-green-dark">Verify Code</button>
                </form>

            <?php else: ?>
                <!-- Step 2: Upload Form (Only shown if code is valid) -->
                <div class="bg-green-50 p-4 rounded-md mb-6">
                    <p class="text-green-800"><strong>Welcome, <?php echo htmlspecialchars($entry['name']); ?>!</strong></p>
                    <p class="text-sm text-green-600">Please upload your files below.</p>
                </div>

                <form action="upload_entry_handler.php" method="POST" enctype="multipart/form-data" class="space-y-6">
                    <input type="hidden" name="reg_code" value="<?php echo htmlspecialchars($reg_code); ?>">
                    
                    <div>
                        <label for="user_photo" class="block text-sm font-semibold text-gray-700">Your Photo (Passport Size)</label>
                        <input type="file" name="user_photo" id="user_photo" required accept="image/*" class="mt-1 block w-full text-sm text-gray-700 border border-gray-300 rounded-md shadow-sm file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-ucf-green-light file:text-ucf-green-dark hover:file:bg-ucf-green">
                        <p class="text-xs text-gray-500 mt-1">Max size 2MB. JPG/PNG only.</p>
                    </div>

                    <div>
                        <label for="artwork_photo" class="block text-sm font-semibold text-gray-700">Competition Artwork</label>
                        <input type="file" name="artwork_photo" id="artwork_photo" required accept="image/*" class="mt-1 block w-full text-sm text-gray-700 border border-gray-300 rounded-md shadow-sm file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-ucf-green-light file:text-ucf-green-dark hover:file:bg-ucf-green">
                        <p class="text-xs text-gray-500 mt-1">High quality image of your art. Max size 5MB.</p>
                    </div>

                    <button type="submit" class="w-full bg-ucf-green text-white font-bold py-3 px-6 rounded-lg hover:bg-ucf-green-dark">Upload & Submit</button>
                </form>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>