<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$appFile = __DIR__ . '/data/applications.txt';
$uploadDir = __DIR__ . '/uploads/applications/';

if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

/* Handle form submission */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $location = trim($_POST['location']);
    $about = trim($_POST['about']);
    $music = trim($_POST['music']);
    $role = trim($_POST['role']);
    $experience = trim($_POST['experience']);
    $availability = trim($_POST['availability']);
    $why = trim($_POST['why']);

    $audioFile = '';

    /* Handle audio upload */
    if (!empty($_FILES['audio']['name'])) {
        $ext = strtolower(pathinfo($_FILES['audio']['name'], PATHINFO_EXTENSION));

        if (in_array($ext, ['mp3', 'wav'])) {
            $audioFile = uniqid('demo_') . '.' . $ext;
            move_uploaded_file($_FILES['audio']['tmp_name'], $uploadDir . $audioFile);
        }
    }

    if ($name && $email && $about && $music && $role) {
        $entry = implode('|', [
            uniqid(),
            $name,
            $email,
            $location,
            $about,
            $music,
            $role,
            $experience,
            $availability,
            $why,
            $audioFile,
            date('Y-m-d H:i:s')
        ]) . "\n";

        file_put_contents($appFile, $entry, FILE_APPEND);

        $successMessage = "<div class='msg success'>Your application has been submitted! Our team will review it soon.</div>";
    } else {
        $errorMessage = "<div class='msg error'>Please fill in all required fields.</div>";
    }
}
?>

<div class="main-wrap">
    <div class="content-panel">

        <!-- PAGE HEADER -->
        <div class="join-banner">
            <h1 class="join-banner-text">🎙️ Join the Team</h1>
            <p class="join-subtext">Become part of Oldies & More! Tell us who you are and what you bring to the airwaves.</p>
        </div>

        <div class="vinyl-divider center-divider"></div>

        <!-- APPLICATION FORM -->
        <div class="content-box">
            <?= $successMessage ?? '' ?>
            <?= $errorMessage ?? '' ?>

            <h3 class="section-title center-text">Apply Now</h3>

            <form method="POST" enctype="multipart/form-data" class="form">

                <input type="text" name="name" class="input-field" placeholder="Your Name" required>

                <input type="email" name="email" class="input-field" placeholder="Your Email" required>

                <input type="text" name="location" class="input-field" placeholder="Your Location (City, Country)" required>

                <textarea name="about" class="input-field" placeholder="Tell us about yourself" rows="4" required></textarea>

                <textarea name="music" class="input-field" placeholder="What music do you play?" rows="3" required></textarea>

                <select name="role" class="input-field" required>
                    <option value="">What part of the team do you want to join?</option>
                    <option>On-Air DJ</option>
                    <option>Show Host</option>
                    <option>Producer</option>
                    <option>Music Curator</option>
                    <option>Social Media Team</option>
                    <option>Promotions / Events</option>
                    <option>Technical / Engineering</option>
                </select>

                <input type="text" name="experience" class="input-field" placeholder="Experience Level (Beginner, Intermediate, Pro)">

                <input type="text" name="availability" class="input-field" placeholder="Availability (Days/Times)">

                <textarea name="why" class="input-field" placeholder="Why do you want to join Oldies & More?" rows="3"></textarea>

                <label class="about-text">Upload a 2-minute voice/music demo (MP3 or WAV):</label>
                <input type="file" name="audio" accept=".mp3,.wav" class="input-field">

                <button type="submit" class="button radio-static">Submit Application</button>
            </form>
        </div>

        <div class="vinyl-divider center-divider"></div>

        <!-- BACK BUTTON -->
        <div class="content-box center-text">
            <a href="index.php" class="button radio-static">⬅ Back to Home</a>
        </div>
<?php include __DIR__ . '/includes/footer.php'; ?>
    </div>
</div>


