<?php
// Basic form handling
$name       = $_POST['name'] ?? '';
$email      = $_POST['email'] ?? '';
$role       = $_POST['role'] ?? '';
$experience = $_POST['experience'] ?? '';
$why        = $_POST['why'] ?? '';

// Handle file upload
$demoFile = '';
if (!empty($_FILES['demo']['name'])) {
    $uploadDir = __DIR__ . '/uploads/demos/';
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    $filename = time() . '_' . basename($_FILES['demo']['name']);
    $targetPath = $uploadDir . $filename;

    if (move_uploaded_file($_FILES['demo']['tmp_name'], $targetPath)) {
        $demoFile = $filename;
    }
}

// Save application to file
$saveFile = __DIR__ . '/data/applications.txt';
$entry = "Name: $name\nEmail: $email\nRole: $role\nExperience: $experience\nWhy: $why\nDemo: $demoFile\n---\n";
file_put_contents($saveFile, $entry, FILE_APPEND);

// Redirect back with success message
header("Location: join.php?sent=1");
exit;
