<?php
// --- START PHP PROCESSING LOGIC ---
$message_sent = false;
$error_message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $to = "gpw1944@bigpond.net.au";
    $subject = "JEEPDRAW DRAWING REQUEST";

    // Collect and sanitize input
    $name = strip_tags(trim($_POST['name']));
    $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
    $email_confirm = filter_var(trim($_POST['email_confirm']), FILTER_SANITIZE_EMAIL);
    $country = $_POST['country'] ?? 'Not Specified';
    $make = $_POST['make'] ?? 'Not Specified';
    $year = $_POST['year'] ?? 'Not Specified';
    $other = strip_tags(trim($_POST['other_make']));
    $drawings = strip_tags(trim($_POST['requested_drawings']));
    $comments = strip_tags(trim($_POST['comments']));
    $ip = $_SERVER['REMOTE_ADDR'];

    // Validation
    if ($email !== $email_confirm) {
        $error_message = "Error: Email addresses do not match.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error_message = "Error: Invalid email format.";
    } else {
        // Construct the email body
        $body = "New Drawing Request from JeepDraw\n";
        $body .= "----------------------------------\n";
        $body .= "Name: $name\n";
        $body .= "Email: $email\n";
        $body .= "Country: $country\n";
        $body .= "Jeep Details: $year $make ($other)\n\n";
        $body .= "Requested Drawings:\n$drawings\n\n";
        $body .= "Comments:\n$comments\n\n";
        $body .= "Sender IP: $ip";

        $headers = "From: $email" . "\r\n" . "Reply-To: $email";

        if (mail($to, $subject, $body, $headers)) {
            $message_sent = true;
        } else {
            $error_message = "Server Error: Could not send email. Please try again later.";
        }
    }
}
// --- END PHP PROCESSING LOGIC ---
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    <title>JeepDraw Request Page</title>
</head>
<body text="#000000" bgcolor="#ffffff" background="http://jeepdraw.com/images/jeepdraw/NewBackground2.gif">

<center>
<table style="BORDER-COLLAPSE: collapse; TEXT-ALIGN: center; WIDTH: 895px" cellspacing="6" cellpadding="6" width="895" border="3" bordercolor="#808000">
<tbody>
<tr valign="top">
<td style="TEXT-ALIGN: right">

    <?php if ($message_sent): ?>
        <!-- This shows ONLY after a successful send -->
        <div style="text-align: center; padding: 50px;">
            <h1 style="color: #008000;">Success!</h1>
            <p>Thank you, <?php echo htmlspecialchars($name); ?>. Your request has been sent.</p>
            <p><a href="form-drawings-request.php">Click here to send another request.</a></p>
        </div>
    <?php else: ?>
        
        <center>
            <p align="center"><b><font color="#008000" size="6">JeepDraw Drawing Request Form</font></b></p>
            
            <?php if ($error_message): ?>
                <p align="center" style="color: red; font-weight: bold;"><?php echo $error_message; ?></p>
            <?php endif; ?>

            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                <p align="center"><strong><font color="#008000">Privacy Policy.</font></strong></p>
                <p align="center">Name <input name="name" size="36" required value="<?php echo isset($name) ? htmlspecialchars($name) : ''; ?>"></p>
                <p align="center">Email <input name="email" size="36" required></p>
                <p align="center">Confirm Email <input name="email_confirm" size="36" required></p>

                <p align="center">
                    <b>Model</b> 
                    <select name="make">
                        <option value="MB">MB</option>
                        <option value="GPW">GPW</option>
                    </select>
                    &nbsp; <b>Year</b> 
                    <select name="year">
                        <?php for($i=1941; $i<=1945; $i++) echo "<option value='$i'>$i</option>"; ?>
                    </select>
                </p>

                <p align="center">Drawing Request List:<br>
                <textarea rows="6" cols="84" name="requested_drawings" required></textarea></p>
                
                <p align="center">Comments:<br>
                <textarea rows="4" cols="84" name="comments"></textarea></p>

                <p align="center">
                    <input type="submit" value="Submit">
                    <input type="reset" value="Reset">
                </p>
            </form>
        </center>
    <?php endif; ?>

</td>
</tr>
</tbody>
</table>
</center>
</body>
</html>