<?php

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

require '../vendor/autoload.php';
include_once("../admin/opendb.php");

$mail = new PHPMailer(true);

$name    = $_POST['name'];
$email   = $_POST['email'];
$mobile  = $_POST['mobile'];
$course  = $_POST['course'];
$message = $_POST['message'];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!empty($_POST['website'])) {
        die("Bot detected");
    }
    $name    = trim($_POST['name']);
    $email   = trim($_POST['email']);
    $mobile  = trim($_POST['subject']);
    $course  = trim($_POST['course']);
    $message = trim($_POST['message']);
    
    if (empty($name) || empty($email) || empty($mobile) || empty($course) || empty($message)) {
        die("<div class='alert alert-danger'>All fields are required.</div>");
    }

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        die("<div class='alert alert-danger'>Invalid email format.</div>");
    }

    $stmt = $conn->prepare("INSERT INTO course_enquiry (name, email, mobile, course, message) VALUES (?, ?, ?, ?, ?)");
    $stmt->bind_param("sssss", $name, $email, $mobile, $course, $message);

    if ($stmt->execute()) {
        echo "<div class='alert alert-success'>Enquiry sent successfully!</div>";
    } else {
        echo "<div class='alert alert-danger'>Error: " . $stmt->error . "</div>";
    }
    $stmt->close();
}



// try {
//     // Server settings
//     $mail->isSMTP();
//     $mail->Host       = 'smtp.gmail.com';
//     $mail->SMTPAuth   = true;
//     $mail->Username   = 'yourgmail@gmail.com'; // your Gmail
//     $mail->Password   = 'your-app-password';   // the 16-digit app password generated from google account->security->app passwords->create a new one(Choose "mail"->"Windows Computer")
//     $mail->SMTPSecure = 'tls';
//     $mail->Port       = 587;

//     // Recipients
//     $mail->setFrom($email, $name);
//     $mail->addAddress('receiver@example.com');

//     // Content
//     $mail->isHTML(true);
//     $mail->Subject = 'Test Contact Form';
//     $mail->Body = '<div style="font-family: Arial, sans-serif; background-color:#f9f9f9; padding:20px; border-radius:8px;">
//                     <div style="background:#347d90; color:#fff; padding:15px; border-radius:8px 8px 0 0;">
//                       <h2 style="margin:0;">📩 New Contact Form Submission</h2>
//                     </div>
//                     <div style="background:#fff; padding:20px; border:1px solid #ddd; border-top:none; border-radius:0 0 8px 8px;">
//                       <p style="font-size:16px; margin:10px 0;"><strong>Name:</strong> ' . htmlspecialchars($name) . '</p>
//                       <p style="font-size:16px; margin:10px 0;"><strong>Phone:</strong> ' . htmlspecialchars($mobile) . '</p>
//                       <p style="font-size:16px; margin:10px 0;"><strong>Course:</strong><br>' . nl2br(htmlspecialchars($course)) . '</p>
//                       <p style="font-size:16px; margin:10px 0;"><strong>Message:</strong><br>' . nl2br(htmlspecialchars($message)) . '</p>
//                     </div>
//                     <p style="text-align:center; font-size:12px; color:#777; margin-top:20px;">
//                       This email was sent via <strong>PSN Mentor Contact Form</strong>.
//                     </p>
//                   </div>';

//     $mail->send();
//     echo 'Message has been sent!';
// } catch (Exception $e) {
//     echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// }
?>
