<?php
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    header("Location: volunteer.php");
    exit;
}

/* ======================
   SANITIZE INPUTS
====================== */
$name    = htmlspecialchars(trim($_POST['name'] ?? ''));
$email   = filter_var($_POST['email'] ?? '', FILTER_SANITIZE_EMAIL);
$phone   = htmlspecialchars(trim($_POST['phone'] ?? ''));
$area    = htmlspecialchars(trim($_POST['area'] ?? ''));
$message = htmlspecialchars(trim($_POST['message'] ?? ''));

/* ======================
   VALIDATION
====================== */
if (empty($name) || empty($email)) {
    die("Name and Email are required.");
}

/* ======================
   EMAIL SETUP
====================== */
$to = "info@wipetearsafrica.org";
$subject = "New Volunteer Application - Wipe Tears Africa";

$body = "
New Volunteer Application

Full Name: $name
Email: $email
Phone: $phone
Preferred Area: $area

Message:
$message
";

$headers  = "From: Wipe Tears Africa <no-reply@wipetearsafrica.org>\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8";

/* ======================
   SEND EMAIL
====================== */
if (mail($to, $subject, $body, $headers)) {
    header("Location: volunteer-thank-you.php");
    exit;
} else {
    echo "Error: Message could not be sent. Please try again later.";
}
