<?php require_once __DIR__ . '/includes/bootstrap.php';
$pageTitle = 'Join the Team | Underground FX Radio';
$currentPage = basename(__FILE__);
$success = false;
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $items = ugfx_read_json($ugfx['storage']['applications'], []);
    $attachment = null;
    if (!empty($_FILES['attachment']['name'] ?? '')) {
        $allowed = ['pdf','doc','docx','txt','rtf','jpg','jpeg','png','webp'];
        $original = $_FILES['attachment']['name'] ?? '';
        $tmp = $_FILES['attachment']['tmp_name'] ?? '';
        $size = (int)($_FILES['attachment']['size'] ?? 0);
        $ext = strtolower(pathinfo($original, PATHINFO_EXTENSION));
        if (!in_array($ext, $allowed, true)) {
            $error = 'Please upload a PDF, DOC, DOCX, TXT, RTF, JPG, PNG, or WEBP file.';
        } elseif ($size > 10 * 1024 * 1024) {
            $error = 'Please keep uploads under 10MB.';
        } elseif (is_uploaded_file($tmp)) {
            $uploadDir = __DIR__ . '/storage/uploads/applications';
            if (!is_dir($uploadDir)) {
                @mkdir($uploadDir, 0777, true);
            }
            $safeBase = preg_replace('/[^a-zA-Z0-9_-]+/', '-', pathinfo($original, PATHINFO_FILENAME));
            $safeBase = trim($safeBase, '-');
            if ($safeBase === '') { $safeBase = 'application-file'; }
            $filename = date('YmdHis') . '-' . strtolower($safeBase) . '.' . $ext;
            $target = $uploadDir . '/' . $filename;
            if (move_uploaded_file($tmp, $target)) {
                $attachment = [
                    'name' => $original,
                    'path' => 'storage/uploads/applications/' . $filename,
                    'size' => $size,
                ];
            } else {
                $error = 'The file could not be uploaded. Please try again.';
            }
        }
    }
    if ($error === '') {
        $items[] = [
            'name' => trim($_POST['name'] ?? ''),
            'email' => trim($_POST['email'] ?? ''),
            'role' => trim($_POST['role'] ?? ''),
            'availability' => trim($_POST['availability'] ?? ''),
            'experience' => trim($_POST['experience'] ?? ''),
            'links' => trim($_POST['links'] ?? ''),
            'attachment' => $attachment,
            'created_at' => date('Y-m-d H:i:s'),
            'status' => 'new'
        ];
        ugfx_write_json($ugfx['storage']['applications'], $items);
        $success = true;
    }
}
include __DIR__ . '/includes/header.php';
?>
<section class="ugfx-page-hero">
  <div class="container-xxl">
    <div class="ugfx-kicker"><i class="bi bi-stars"></i> Build the station with us</div>
    <h1 class="page-title">Join the Team</h1>
    <p class="page-copy">A premium recruitment page for presenters, volunteers, support crew, promo help, event staff and community contributors.</p>
  </div>
</section>
<section class="section-pad pt-3">
  <div class="container-xxl">
    <div class="row g-4">
      <div class="col-lg-7">
        <div class="ugfx-card p-4">
          <?php if ($success): ?><div class="alert alert-success">Your application has been sent to the Underground FX team.</div><?php endif; ?>
          <?php if ($error !== ''): ?><div class="alert alert-danger"><?= htmlspecialchars($error) ?></div><?php endif; ?>
          <form method="post" class="row g-3" enctype="multipart/form-data">
            <div class="col-md-6"><label class="form-label">Name</label><input class="form-control" name="name" required></div>
            <div class="col-md-6"><label class="form-label">Email</label><input class="form-control" type="email" name="email" required></div>
            <div class="col-md-6"><label class="form-label">Role Interest</label><select class="form-select" name="role" required><option value="">Select role</option><option>Presenter / DJ</option><option>Admin / Moderation</option><option>Events / Promo</option><option>Graphics / Media</option><option>General Support</option></select></div>
            <div class="col-md-6"><label class="form-label">Availability</label><input class="form-control" name="availability" placeholder="Weeknights, weekends, afternoons..."></div>
            <div class="col-12"><label class="form-label">Experience / Pitch</label><textarea class="form-control" rows="6" name="experience" required></textarea></div>
            <div class="col-12"><label class="form-label">Links</label><textarea class="form-control" rows="3" name="links" placeholder="Demo, socials, portfolio, stream links"></textarea></div>
            <div class="col-12"><label class="form-label">Upload file</label><input class="form-control" type="file" name="attachment" accept=".pdf,.doc,.docx,.txt,.rtf,.jpg,.jpeg,.png,.webp"><div class="form-text">Optional. Upload a demo sheet, resume, bio, flyer, or image up to 10MB.</div></div>
            <div class="col-12"><button class="btn ugfx-btn-primary">Submit application</button></div>
          </form>
        </div>
      </div>
      <div class="col-lg-5">
        <div class="ugfx-card p-4 h-100">
          <div class="section-heading mb-4"><div class="title">What we can recruit for</div><div class="copy">This replaces the missing Hometime-style join flow with a stronger premium front-end and cleaner storage.</div></div>
          <div class="ugfx-stack-list compact">
            <div class="ugfx-stack-link static"><span><strong>Presenters and DJs</strong><small>Hosted slots, guest hours and specials</small></span></div>
            <div class="ugfx-stack-link static"><span><strong>Promo and events</strong><small>Support upcoming event drops and social pushes</small></span></div>
            <div class="ugfx-stack-link static"><span><strong>Moderation and admin support</strong><small>Help chat, content and station workflows</small></span></div>
            <div class="ugfx-stack-link static"><span><strong>Creative contributors</strong><small>Graphics, media, edits and community ideas</small></span></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>
<?php include __DIR__ . '/includes/footer.php'; ?>
