<?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);
}

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 = '';

    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">

        <div class="join-banner">
            <h1 class="join-banner-text">🎙️ Join the Team</h1>
            <p class="join-subtext">Apply to become part of Oldies And More Radio.</p>
        </div>

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

        <div class="content-box join-public-form-box">
            <?= $successMessage ?? '' ?>
            <?= $errorMessage ?? '' ?>

            <h3 class="section-title center-text">Join the Team</h3>

            <form method="POST" enctype="multipart/form-data" class="form join-public-form join-public-form-matched">
                <input id="join_name" type="text" name="name" class="input-field" placeholder="Your Name" required>

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

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

                <select id="join_role" name="role" class="input-field" required>
                    <option value="">Role You're Interested In</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 id="join_experience" type="text" name="experience" class="input-field" placeholder="Your Experience (Beginner, Intermediate, Experienced)">

                <textarea id="join_about" name="about" class="input-field" placeholder="Tell Us About Yourself" rows="4" required></textarea>

                <textarea id="join_music" name="music" class="input-field" placeholder="What Music Do You Play?" rows="3" required></textarea>

                <input id="join_availability" type="text" name="availability" class="input-field" placeholder="Availability (days/times)">

                <textarea id="join_why" name="why" class="input-field" placeholder="Why Do You Want To Join?" rows="4"></textarea>

                <div class="join-file-wrap">
                    <label for="join_audio" class="join-file-label">Upload Demo Audio (optional)</label>
                    <input id="join_audio" type="file" name="audio" accept=".mp3,.wav" class="input-field file-field">
                </div>

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

        <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>
