<?php
/**
 * Traitement du formulaire de contact — tech&amp;vape
 */

// Anti-spam honeypot
if (!empty($_POST['website'])) {
    header('Location: contact.html');
    exit;
}

$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$message = trim($_POST['message'] ?? '');

// Validation
$errors = [];
if (empty($name)) $errors[] = 'Le nom est requis.';
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Un email valide est requis.';
if (empty($message)) $errors[] = 'Le message est requis.';

if (!empty($errors)) {
    // Redirection avec erreur
    header('Location: contact.html?error=1');
    exit;
}

// Préparation de l'email
$to = '';
$subject = 'Nouveau message de contact — ' . htmlspecialchars($name);
$body = "Nom : $name\n";
$body .= "Email : $email\n";
if ($phone) $body .= "Téléphone : $phone\n";
$body .= "\nMessage :\n$message\n";

$headers = "From: noreply@" . $_SERVER['HTTP_HOST'] . "\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

// Envoi
$sent = @mail($to, $subject, $body, $headers);

if ($sent) {
    header('Location: contact.html?success=1');
} else {
    header('Location: contact.html?error=2');
}
exit;
