<?php

// Define Host Info || Who is sending emails?
define("HOST_NAME", "Richmond Tech Group");
define("HOST_EMAIL", "syedmahadbukhari928@gmail.com");

// Define SMTP Credentials || Gmail Informations
define("SMTP_EMAIL", "syedmahadbukhari928@gmail.com"); // Apna Gmail email daalen
define("SMTP_PASSWORD", "zucd ggun estf pldi"); // Gmail App Password daalen

// Define Recipent Info ||  Who will get this email?
define("RECIPIENT_NAME", "Richmond Tech Group");
define("RECIPIENT_EMAIL", "syedmahadbukhari928@gmail.com");

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

// Get the correct path for PHPMailer files using document root
$document_root = $_SERVER['DOCUMENT_ROOT'];
$phpmailer_path = $document_root . '/RichmondTechGroup/assets/inc/PHPMailer/src/';

require $phpmailer_path . 'Exception.php';
require $phpmailer_path . 'PHPMailer.php';
require $phpmailer_path . 'SMTP.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
	// Add debugging information
	error_log("Contact form submitted from: " . $_SERVER['REQUEST_URI']);
	error_log("POST data: " . print_r($_POST, true));
	
	//Server settings
	$mail->SMTPDebug = 0;                      //Enable verbose debug output
	$mail->isSMTP();                                            //Send using SMTP
	$mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
	$mail->SMTPAuth   = true;                                   //Enable SMTP authentication
	$mail->Username   = 'syedmahadbukhari928@gmail.com';                     //SMTP username
	$mail->Password   = SMTP_PASSWORD;                               //SMTP password
	$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;            //Enable implicit TLS encryption
	$mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

	//Recipients
	$mail->setFrom(HOST_EMAIL, HOST_NAME);
	$mail->addAddress(RECIPIENT_EMAIL, RECIPIENT_NAME);     //Add a recipient

	//Content
	$name = isset($_POST['name']) ? preg_replace("/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name']) : "";
	$senderEmail = isset($_POST['email']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email']) : "";
	$phone = isset($_POST['Phone']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['Phone']) : ""; // Phone field name fix
	$services = isset($_POST['services']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['services']) : "";
	$subject = isset($_POST['subject']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['subject']) : "";
	$address = isset($_POST['address']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['address']) : "";
	$website = isset($_POST['website']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['website']) : "";
	$message = isset($_POST['message']) ? preg_replace("/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message']) : "";

	$mail->isHTML(true);                                  //Set email format to HTML
	$mail->Subject = 'Richmond Tech Group';
	$mail->Body    = '
	<!DOCTYPE html>
	<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Contact Form Submission</title>
		<style>
			body { font-family: Arial, sans-serif; margin: 0; padding: 0; background-color: #ffffff; }
			.container { max-width: 600px; margin: 0 auto; background-color: #ffffff; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
			.header { background-color: #000000; color: white; padding: 30px 25px; text-align: center; }
			.header h1 { margin: 0; font-size: 28px; color: #ffffff; font-weight: bold; }
			.header .subtitle { margin: 8px 0 0 0; color: #cccccc; font-size: 16px; }
			.content { padding: 30px 25px; background-color: #ffffff; }
			.section-title { color: #000000; font-size: 20px; margin-bottom: 20px; border-bottom: 2px solid #000000; padding-bottom: 10px; font-weight: bold; }
			.info-grid { display: table; width: 100%; border-collapse: collapse; background-color: #ffffff; border: 1px solid #dddddd; }
			.info-row { display: table-row; }
			.info-row:nth-child(even) { background-color: #f9f9f9; }
			.info-label { display: table-cell; padding: 12px 15px; font-weight: bold; color: #000000; border-bottom: 1px solid #dddddd; width: 35%; font-size: 14px; }
			.info-value { display: table-cell; padding: 12px 15px; color: #333333; border-bottom: 1px solid #dddddd; font-size: 14px; }
			.footer { background-color: #f5f5f5; padding: 20px; text-align: center; border-top: 1px solid #dddddd; }
			.footer p { margin: 8px 0; color: #666666; font-size: 14px; }
			.company-name { color: #000000; font-weight: bold; font-size: 16px; }
			.badge { display: inline-block; background-color: #000000; color: #ffffff; padding: 6px 12px; border-radius: 4px; font-size: 12px; font-weight: bold; margin-bottom: 15px; }
			@media only screen and (max-width: 600px) {
				.info-label, .info-value { display: block; width: 100%; }
				.info-label { border-bottom: none; background-color: #f5f5f5; }
			}
		</style>
	</head>
	<body>
		<div class="container">
			<div class="header">
				<h1>Richmond Tech Group</h1>
				<div class="subtitle">New Contact Form Submission</div>
			</div>
			<div class="content">
				<div class="badge">📧 Contact Details</div>
				<h2 class="section-title">Client Information</h2>
				<div class="info-grid">
					<div class="info-row">
						<div class="info-label">👤 Name:</div>
						<div class="info-value">' . $name . '</div>
					</div>
					<div class="info-row">
						<div class="info-label">📧 Email:</div>
						<div class="info-value">' . $senderEmail . '</div>
					</div>';

	if ($phone) {
		$mail->Body .= '
					<div class="info-row">
						<div class="info-label">📞 Phone:</div>
						<div class="info-value">' . $phone . '</div>
					</div>';
	}
	if ($services) {
		$mail->Body .= '
					<div class="info-row">
						<div class="info-label">🛠️ Service:</div>
						<div class="info-value">' . $services . '</div>
					</div>';
	}
	if ($subject) {
		$mail->Body .= '
					<div class="info-row">
						<div class="info-label">📝 Subject:</div>
						<div class="info-value">' . $subject . '</div>
					</div>';
	}
	if ($address) {
		$mail->Body .= '
					<div class="info-row">
						<div class="info-label">📍 Address:</div>
						<div class="info-value">' . $address . '</div>
					</div>';
	}
	if ($website) {
		$mail->Body .= '
					<div class="info-row">
						<div class="info-label">🌐 Website:</div>
						<div class="info-value">' . $website . '</div>
					</div>';
	}

	if ($message) {
		$mail->Body .= '
					<div class="info-row">
						<div class="info-label">💬 Message:</div>
						<div class="info-value">' . nl2br($message) . '</div>
					</div>';
	}

	$mail->Body .= '
				</div>';

	$mail->Body .= '
			</div>
			<div class="footer">
				<p>This email was sent from your website contact form.</p>
				<p class="company-name">Richmond Tech Group</p>
			</div>
		</div>
	</body>
	</html>';

	$mail->send();
	$response = "<div class='inner success'><p class='success'>Thanks for contacting us. We will contact you ASAP!</p></div><!-- /.inner -->";
	echo $response;
	error_log("Email sent successfully to: " . RECIPIENT_EMAIL);
} catch (Exception $e) {
	$error_msg = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
	$response = "<div class='inner error'><p class='error'>$error_msg</p></div><!-- /.inner -->";
	echo $response;
	error_log("Email sending failed: " . $error_msg);
}

