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

function clean($value) {
    return htmlspecialchars(trim($value ?? ''), ENT_QUOTES, 'UTF-8');
}

$name = clean($_POST['name'] ?? '');
$company = clean($_POST['company'] ?? '');
$email = clean($_POST['email'] ?? '');
$phone = clean($_POST['phone'] ?? '');
$position = clean($_POST['position'] ?? '');
$industry = clean($_POST['industry'] ?? '');
$locations = clean($_POST['locations'] ?? '');
$timeline = clean($_POST['timeline'] ?? '');
$message = clean($_POST['message'] ?? '');

if ($name === '' || $company === '' || $email === '' || $message === '') {
    header('Location: contact.html?status=missing');
    exit;
}

$to = 'vendaisolutionsph@gmail.com';
$subject = 'VendAi Website Inquiry - ' . $company;
$body = "A new inquiry was submitted on vendai.ph\n\n"
    . "Name: {$name}\n"
    . "Company: {$company}\n"
    . "Email: {$email}\n"
    . "Phone: {$phone}\n"
    . "Position: {$position}\n"
    . "Industry: {$industry}\n"
    . "Estimated Locations: {$locations}\n"
    . "Deployment Timeline: {$timeline}\n\n"
    . "Message:\n{$message}\n";

$headers = "From: VendAi Website <no-reply@vendai.ph>\r\n";
$headers .= "Reply-To: {$email}\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

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

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