<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/PHPMailer/src/Exception.php';
require 'vendor/PHPMailer/src/PHPMailer.php';
require 'vendor/PHPMailer/src/SMTP.php';

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
  header("Location: femtechwaves-contact.php");
  exit;
}

$name = htmlspecialchars($_POST['name']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$phone = htmlspecialchars($_POST['phone']);
$service = htmlspecialchars($_POST['service']);
$message = htmlspecialchars($_POST['message']);

$mail = new PHPMailer(true);

try {
  $mail->isSMTP();
  $mail->Host       = 'smtp.gmail.com';
  $mail->SMTPAuth   = true;
  $mail->Username   = 'info@femtechwaveslimited.com.ng';
  $mail->Password   = 'Femtech2025!';
  $mail->SMTPSecure = 'tls';
  $mail->Port       = 465;

  $mail->setFrom($email, 'Website Contact');
  $mail->addAddress('olasteve93@gmail.com');

  $mail->isHTML(true);
  $mail->Subject = 'New Contact Enquiry - FEMTECH WAVES';
  $mail->Body    = "
    <h3>New Contact Message</h3>
    <p><strong>Name:</strong> {$name}</p>
    <p><strong>Email:</strong> {$email}</p>
    <p><strong>Phone:</strong> {$phone}</p>
    <p><strong>Service:</strong> {$service}</p>
    <p><strong>Message:</strong><br>{$message}</p>
  ";

  $mail->send();
  echo "<script>alert('Message sent successfully!');window.location.href='femtechwaves-contact.php';</script>";

} catch (Exception $e) {
  echo "<script>alert('Message could not be sent. Please try again later.');window.history.back();</script>";
}
