<?php
/**
 * Contact Form Handler for Charity Boat Race
 */

// Basic security check
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    // Sanitize inputs
    $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
    $first_name = filter_input(INPUT_POST, 'first_name', FILTER_SANITIZE_SPECIAL_CHARS) ?: $name;
    $last_name = filter_input(INPUT_POST, 'last_name', FILTER_SANITIZE_SPECIAL_CHARS);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $subject = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_SPECIAL_CHARS);
    $message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_SPECIAL_CHARS);
    
    // Validation
    if ((!$first_name && !$name) || !$email || !$message) {
        die("Please fill in all required fields.");
    }
    
    // Email details (Simulated)
    $to = "info@charityboatrace.org";
    $full_subject = "New Contact Form Submission: " . $subject;
    $body = "Name: $first_name $last_name\n";
    $body .= "Email: $email\n\n";
    $body .= "Message:\n$message";
    
    // In a real scenario, we would use mail() or a library like PHPMailer
    // mail($to, $full_subject, $body);
    
    // Redirect back with success message
    // For now, let's just show a simple success page or redirect
    echo "<!DOCTYPE html>
    <html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <title>Message Sent | Charity Boat Race</title>
        <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'>
        <link rel='stylesheet' href='../css/style.css'>
    </head>
    <body class='bg-background d-flex align-items-center justify-content-center' style='min-height: 100vh;'>
        <div class='card p-5 shadow-lg text-center' style='max-width: 500px;'>
            <div class='bg-primary-subtle p-4 rounded-circle mx-auto mb-4' style='width: 80px; height: 80px;'>
                <svg xmlns='http://www.w3.org/2000/svg' width='32' height='32' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' class='text-primary'><path d='m22 2-7 20-4-9-9-4Z'/><path d='M22 2 11 13'/></svg>
            </div>
            <h2 class='fw-bold mb-3'>Thank You!</h2>
            <p class='text-muted mb-4'>Your message has been sent successfully. We'll get back to you as soon as possible.</p>
            <a href='../index.php' class='btn btn-primary px-4'>Back to Home</a>
        </div>
    </body>
    </html>";
    
} else {
    // Redirect if accessed directly
    header("Location: ../contact.php");
    exit();
}
?>
