<?php

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

//Load Composer's autoloader
require './../vendor/autoload.php';

$organizationName = 'Intellixon';

$hostname = 'localhost:3306';
$username = 'inte5974_root';
$password = 'Champ.05';
$database = 'inte5974_intellixon';

$smtpHost = 'smtp.gmail.com';
$smtpAuth = true;
$smtpUsername = 'taskforce.intellixon';
$smtpPassword = 'nxsiwxcfssdhrqat';
$smtpSecure = PHPMailer::ENCRYPTION_STARTTLS;
$smtpPort = 587;

$mailTo = "taskforce.intellixon@gmail.com";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    try {

        // Get form data
        $name = test_input($_POST["name"]);
        $email = test_input($_POST["email"]);
        $subject = test_input($_POST["subject"]);
        $message = test_input($_POST["message"]);
        $to = $mailTo; // Replace with your email address
        
        // Compose the email body
        $body = "Name: $name\n";
        $body .= "Email: $email\n";
        $body .= "Subject: $subject\n";
        $body .= "Message: $message\n";
        
        // Create a new PHPMailer instance
        $mail = new PHPMailer(true);
        
        // SMTP configuration
        
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = $smtpHost;                     //Set the SMTP server to send through
        $mail->SMTPAuth   = $smtpAuth;                                   //Enable SMTP authentication
        $mail->Username   = $smtpUsername;                     //SMTP username
        $mail->Password   = $smtpPassword;                               //SMTP password
        $mail->SMTPSecure = $smtpSecure;            //Enable implicit TLS encryption
        $mail->Port       = $smtpPort;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
        
        // Set email content
        $mail->setFrom($email);
        $mail->addAddress($to);
        $mail->Subject = "Intellixon Enquiry";
        $mail->Body = $body;

                // Save data to the database
        $conn = new mysqli($hostname, $username, $password, $database);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $stmt = $conn->prepare("INSERT INTO contact_form (name, email, subject, message) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss", $name, $email, $subject, $message);
        $stmt->execute();
    
        $stmt->close();
        $conn->close();
        
        // Send the email
        if ($mail->send()) {
            echo "Thank you for your message. We will get back to you shortly.";
        } else {
            echo "Sorry, there was an error sending your message. Please try again later.";
        }

        // Send acknowledgment email to the user
        $ackSubject = "Thank you for your inquiry";
        $ackBody = "Dear $name,\n\n";
        $ackBody .= "Thank you for contacting us. We have received your inquiry and will get back to you soon.\n\n";
        $ackBody .= "Here are the details you provided:\n";
        $ackBody .= "Name: $name\n";
        $ackBody .= "Email: $email\n";
        $ackBody .= "Subject: $subject\n";
        $ackBody .= "Message: $message\n\n";
        $ackBody .= "Best regards,\n$organizationName";

        $ackMail = new PHPMailer(true);

        try {
            
            $ackMail->SMTPDebug = SMTP::DEBUG_SERVER; 
            $ackMail->isSMTP();
            $ackMail->Host       = $smtpHost;
            $ackMail->SMTPAuth   = $smtpAuth;
            $ackMail->Username   = $smtpUsername;
            $ackMail->Password   = $smtpPassword;
            $ackMail->SMTPSecure = $smtpSecure;
            $ackMail->Port       = $smtpPort;

            $ackMail->setFrom($mailTo, $organizationName);
            $ackMail->addAddress($email, $name); // Send acknowledgment to the user's email

            $ackMail->Subject = $ackSubject;
            $ackMail->Body    = $ackBody;

            $ackMail->send();
            echo 'Acknowledgment email has been sent.';
        } catch (Exception $e) {
            echo "Acknowledgment email could not be sent. Error: {$ackMail->ErrorInfo}";
        }

    } catch (Exception $e) {
        echo "error";
        // No exception reporting - no need to know if email succeeded or failed
    }
}

// Function to sanitize form input
function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>