35286-vm/subscribe-step3.php
Flatlogic Bot 6e132e0b38 0.2
2025-10-27 21:34:11 +00:00

174 lines
9.4 KiB
PHP

<?php
session_start();
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/mail/MailService.php';
// Redirect to step 1 if session is not fully populated
if (!isset($_SESSION['personal_details']) || !isset($_SESSION['insurance_details'])) {
header('Location: subscribe.php');
exit;
}
$personal_details = $_SESSION['personal_details'];
$insurance_details = $_SESSION['insurance_details'];
function get_coverage_details_html($details) {
$type = $details['insuranceType'] ?? 'N/A';
$output = '';
switch ($type) {
case 'Car':
$output .= "<li><strong>Car Make:</strong> " . htmlspecialchars($details['carMake']) . "</li>";
$output .= "<li><strong>Car Model:</strong> " . htmlspecialchars($details['carModel']) . "</li>";
$output .= "<li><strong>Year of Manufacture:</strong> " . htmlspecialchars($details['carYear']) . "</li>";
break;
case 'Health':
$output .= "<li><strong>Number of Dependents:</strong> " . htmlspecialchars($details['healthDependents']) . "</li>";
break;
case 'Life':
$output .= "<li><strong>Coverage Amount:</strong> $" . number_format($details['lifeCoverage'], 2) . "</li>";
break;
case 'Home':
$output .= "<li><strong>Property Type:</strong> " . htmlspecialchars($details['homeType']) . "</li>";
break;
}
return $output;
}
$show_success_message = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
$pdo = db();
$stmt = $pdo->prepare(
"INSERT INTO subscriptions (fullName, email, phone, insuranceType, carMake, carModel, carYear, healthDependents, lifeCoverage, homeType) " .
"VALUES (:fullName, :email, :phone, :insuranceType, :carMake, :carModel, :carYear, :healthDependents, :lifeCoverage, :homeType)"
);
$stmt->execute([
':fullName' => $personal_details['fullName'],
':email' => $personal_details['email'],
':phone' => $personal_details['phone'],
':insuranceType' => $insurance_details['insuranceType'],
':carMake' => $insurance_details['carMake'] ?? null,
':carModel' => $insurance_details['carModel'] ?? null,
':carYear' => $insurance_details['carYear'] ?: null,
':healthDependents' => $insurance_details['healthDependents'] ?: null,
':lifeCoverage' => $insurance_details['lifeCoverage'] ?: null,
':homeType' => $insurance_details['homeType'] ?? null,
]);
$submission_id = $pdo->lastInsertId();
// --- Send Confirmation Email to User ---
$user_subject = "Your Subscription to SecureLife is Confirmed!";
$user_body_html = "<h1>Welcome to SecureLife, " . htmlspecialchars($personal_details['fullName']) . "!</h1>";
$user_body_html .= "<p>We have received your application for " . htmlspecialchars($insurance_details['insuranceType']) . " insurance. Here is a summary of your submission:</p>";
$user_body_html .= "<ul>" . get_coverage_details_html($insurance_details) . "</ul>";
$user_body_html .= "<p>We will review your application and get back to you shortly. Your submission ID is: <strong>{$submission_id}</strong></p>";
MailService::sendMail($personal_details['email'], $user_subject, $user_body_html);
// --- Send Notification Email to Admin ---
$admin_subject = "New Insurance Application Received (#{$submission_id})";
$admin_body_html = "<h1>New Submission</h1>";
$admin_body_html .= "<p>A new insurance application has been submitted by <strong>" . htmlspecialchars($personal_details['fullName']) . "</strong>.</p>";
$admin_body_html .= "<h5>Personal Details</h5><ul>";
$admin_body_html .= "<li>Name: " . htmlspecialchars($personal_details['fullName']) . "</li>";
$admin_body_html .= "<li>Email: " . htmlspecialchars($personal_details['email']) . "</li>";
$admin_body_html .= "<li>Phone: " . htmlspecialchars($personal_details['phone']) . "</li></ul>";
$admin_body_html .= "<h5>Insurance Details</h5><ul>";
$admin_body_html .= "<li>Type: " . htmlspecialchars($insurance_details['insuranceType']) . "</li>";
$admin_body_html .= get_coverage_details_html($insurance_details) . "</ul>";
$admin_body_html .= "<p>You can view this submission in the <a href=\"http://{\$\_SERVER['HTTP_HOST']}/admin.php\">Admin Dashboard</a>.</p>";
MailService::sendMail(null, $admin_subject, $admin_body_html); // $to = null uses MAIL_TO from env
// Clear the session and show a success message
session_unset();
session_destroy();
$show_success_message = true;
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
die("An error occurred. Please try again later.");
} catch (Exception $e) {
error_log("Mail sending error: " . $e->getMessage());
die("An error occurred. Please try again later.");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Confirm Subscription - SecureLife</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<header class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="index.php">SecureLife</a>
</div>
</header>
<main class="container my-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card shadow-sm border-0" style="border-radius: 0.75rem;">
<div class="card-body p-5">
<?php if ($show_success_message): ?>
<div class="text-center">
<i class="bi bi-check-circle-fill text-success" style="font-size: 4rem;"></i>
<h1 class="card-title mt-4 fw-bold">Subscription Complete!</h1>
<p class="text-muted fs-5">Thank you for choosing SecureLife. A confirmation email has been sent to you. We will be in touch shortly.</p>
<a href="index.php" class="btn btn-primary-modern btn-lg mt-4">Back to Home</a>
</div>
<?php else: ?>
<h1 class="card-title text-center mb-2 fw-bold">Confirm Your Details</h1>
<p class="text-center text-muted mb-5">Step 3: Please review your information before confirming.</p>
<div class="bg-light-blue p-4 rounded-3">
<div class="row">
<div class="col-md-6">
<h5 class="fw-bold">Personal Details</h5>
<ul class="list-unstyled">
<li><strong>Full Name:</strong> <?php echo htmlspecialchars($personal_details['fullName']); ?></li>
<li><strong>Email:</strong> <?php echo htmlspecialchars($personal_details['email']); ?></li>
<li><strong>Phone:</strong> <?php echo htmlspecialchars($personal_details['phone']); ?></li>
</ul>
</div>
<div class="col-md-6">
<h5 class="fw-bold">Insurance Details</h5>
<ul class="list-unstyled">
<li><strong>Insurance Type:</strong> <?php echo htmlspecialchars($insurance_details['insuranceType']); ?></li>
<?php echo get_coverage_details_html($insurance_details); ?>
</ul>
</div>
</div>
</div>
<form action="subscribe-step3.php" method="POST">
<div class="d-flex justify-content-between mt-5">
<a href="subscribe-step2.php" class="btn btn-secondary btn-lg">&larr; Go Back</a>
<button type="submit" class="btn btn-success btn-lg">Confirm Subscription &check;</button>
</div>
</form>
<?php endif; ?>
</div>
</div>
</div>
</div>
</main>
<footer class="text-center py-4 mt-auto bg-dark text-white">
<p class="mb-0">&copy; <?php echo date("Y"); ?> SecureLife. All Rights Reserved.</p>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>