<?php
session_start();

error_reporting(E_ALL);
ini_set('display_errors', 1);

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    die('Invalid Request');
}

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

// Validation
if (empty($name) || empty($email) || empty($phone) || empty($service)) {

    $_SESSION['status'] = 'error';
    $_SESSION['msg'] = 'Please fill all required fields.';

    header('Location: contact.php');
    exit();
}

$email = filter_var($email, FILTER_SANITIZE_EMAIL);

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

    $_SESSION['status'] = 'error';
    $_SESSION['msg'] = 'Please enter a valid email address.';

    header('Location: contact.php');
    exit();
}

// Recipient
$to = 'info@innovarebuilders.com.au';
$subject = '🛑 Lead - Innovare Roofing';

// Email body
$body = "
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<title>New Lead - Innovare Roofing</title>
</head>

<body style='margin:0; padding:0; background:#f4f4f4; font-family:Arial, Helvetica, sans-serif;'>

<table width='100%' cellpadding='0' cellspacing='0' style='background:#f4f4f4; padding:30px 0;'>
<tr>
<td align='center'>

<table width='600' cellpadding='0' cellspacing='0' style='background:#ffffff; border-radius:8px; overflow:hidden; box-shadow:0 2px 8px rgba(0,0,0,0.1);'>

<!-- Header -->
<tr>
<td style='background:#17146e; padding:25px; text-align:center;'>

<h1 style='color:#ffffff; margin:0; font-size:26px;'>
Innovare Roofing
</h1>

<p style='color:#eba53c; margin:8px 0 0; font-size:14px;'>
New Website Enquiry Received
</p>

</td>
</tr>


<!-- Content -->
<tr>
<td style='padding:30px;'>

<h2 style='color:#17146e; font-size:20px; margin-top:0;'>
New Lead Details
</h2>


<table width='100%' cellpadding='10' cellspacing='0' style='border-collapse:collapse;'>

<tr>
<td style='background:#f8f8f8; font-weight:bold; width:35%; color:#555;'>
Full Name
</td>
<td style='border-bottom:1px solid #eeeeee;'>
".htmlspecialchars($name)."
</td>
</tr>


<tr>
<td style='background:#f8f8f8; font-weight:bold; color:#555;'>
Email Address
</td>
<td style='border-bottom:1px solid #eeeeee;'>
".htmlspecialchars($email)."
</td>
</tr>


<tr>
<td style='background:#f8f8f8; font-weight:bold; color:#555;'>
Phone Number
</td>
<td style='border-bottom:1px solid #eeeeee;'>
".htmlspecialchars($phone)."
</td>
</tr>


<tr>
<td style='background:#f8f8f8; font-weight:bold; color:#555;'>
Service Required
</td>
<td style='border-bottom:1px solid #eeeeee;'>
".htmlspecialchars($service)."
</td>
</tr>


<tr>
<td style='background:#f8f8f8; font-weight:bold; color:#555; vertical-align:top;'>
Message
</td>
<td style='border-bottom:1px solid #eeeeee;'>
".nl2br(htmlspecialchars($message))."
</td>
</tr>


</table>


<p style='margin-top:30px; color:#777; font-size:13px;'>
This enquiry was submitted through the Innovare Roofing website contact form.
</p>


</td>
</tr>


<!-- Footer -->
<tr>
<td style='background:#17146e; padding:20px; text-align:center;'>

<p style='color:#ffffff; margin:0; font-size:13px;'>
© 2026 Innovare Roofing. <a style='color: #eba53c; text-decoration: none;' href='http://innovaregroup.com.au/' target='_blank'>Part of the Innovare Group.</a>
</p>

</td>
</tr>


</table>

</td>
</tr>
</table>


</body>
</html>
";

// Headers
$headers = [];
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: text/html; charset=UTF-8";
$headers[] = "From: Innovare Roofing <info@innovarebuilders.com.au>";
$headers[] = "Reply-To: ".$email;
$headers[] = "Bcc: ardigitalsolutions.brisbane@gmail.com";
$headers[] = "X-Mailer: PHP/" . phpversion();

$mailSent = mail($to, $subject, $body, implode("\r\n", $headers));

if ($mailSent) {

    $_SESSION['status'] = 'success';
    $_SESSION['msg'] = 'Thank you! Your enquiry has been sent successfully.';

} else {

    $_SESSION['status'] = 'error';
    $_SESSION['msg'] = 'Sorry, something went wrong. Please try again.';
}

header('Location: contact.php');
exit();