<?php


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

if(isset($_POST['custom_token']) && $_POST['custom_token'] != ''){ 

    $response = array(
        'status' => 0,
        'message' => 'Form submission failed, please try again.'
    );

    $fname = isset($_POST['fname']) ? $_POST['fname'] : '';
    $lname = isset($_POST['lname']) ? $_POST['lname'] : '';
    $phone = isset($_POST['phone']) ? $_POST['phone'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $service = isset($_POST['service']) ? $_POST['service'] : ''; 
    $postcode = isset($_POST['postcode']) ? $_POST['postcode'] : '';
    $message = isset($_POST['message']) ? $_POST['message'] : '';

 $content = "
<strong>First Name:</strong> $fname <br>
<strong>Last Name:</strong> $lname <br>
<strong>Phone:</strong> $phone <br>
<strong>Email:</strong> $email <br>
<strong>Property Type:</strong> $service <br>
<strong>Post Code:</strong> $postcode <br>
<strong>Message:</strong> $message
";


    $subject = "MBJ Group: Quote Form Details";
   $fromname = "MBJ Group";
    $fromemail = 'info@mbjgroup.co.uk';
    $mailto = 'info@mbjgroup.co.uk';



    // Instantiate PHPMailer
    $mail = new PHPMailer(true);
  


    try {
        // Set up SMTP
        $mail->isSMTP();
        $mail->Host = 'smtp.hostinger.com';
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->Username = 'info@mbjgroup.co.uk';
        $mail->Password = 'MyLetter@2025';
        $mail->SMTPSecure = 'STARTTLS';

        // Set up email content and headers
        $mail->setFrom($fromemail, $fromname);
        $mail->addAddress($mailto);
        $mail->addCC($email);
        $mail->Subject = $subject;
        $mail->isHTML(true);
        $mail->Body = $content;

        // Send the email
        $mail->send();

        $response['status'] = 1;
        $response['message'] = 'Application submitted successfully!';
    } catch (Exception $e) {
        $response['message'] = 'Message could not be sent. Error: ' . $mail->ErrorInfo;
    }

    echo json_encode($response);
}else{
    echo 'You dont have directly access to this page. You will be redirect to home page in few seconds.'; ?>
    <meta http-equiv="refresh" content="5;url=/" />
<?php } ?>
