34850-vm/leads.php
2025-10-10 11:23:53 +00:00

159 lines
5.8 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$success_message = '';
if (isset($_SESSION['success_message'])) {
$success_message = $_SESSION['success_message'];
unset($_SESSION['success_message']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$company = trim($_POST['company'] ?? '');
$message = trim($_POST['message'] ?? '');
$errors = [];
if (empty($name)) {
$errors[] = 'Name is required.';
}
if (empty($email)) {
$errors[] = 'Email is required.';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email format.';
}
if (empty($errors)) {
try {
$pdo = db();
$sql = "INSERT INTO leads (name, email, company, message) VALUES (:name, :email, :company, :message)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':name' => $name,
':email' => $email,
':company' => $company,
':message' => $message
]);
$_SESSION['success_message'] = 'Lead added successfully!';
header("Location: leads.php");
exit;
} catch (PDOException $e) {
$errors[] = "Database error: " . $e->getMessage();
}
}
// If there are errors, the script will continue and display them below
}
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, name, company, email, status, created_at FROM leads ORDER BY created_at DESC");
$leads = $stmt->fetchAll();
} catch (PDOException $e) {
die("Could not fetch leads: " . $e->getMessage());
}
require_once 'includes/header.php';
?>
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($success_message); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<strong>Error!</strong> Please correct the following issues:
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo htmlspecialchars($error); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h1 class="h4 mb-0">Leads</h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#newLeadModal">
<i class="bi bi-plus-lg"></i> New Lead
</button>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead class="table-light">
<tr>
<th>Name</th>
<th>Company</th>
<th>Email</th>
<th>Status</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php if (empty($leads)): ?>
<tr>
<td colspan="5" class="text-center text-muted">No leads found. Add one to get started!</td>
</tr>
<?php else: ?>
<?php foreach ($leads as $lead): ?>
<tr>
<td><?php echo htmlspecialchars($lead['name']); ?></td>
<td><?php echo htmlspecialchars($lead['company']); ?></td>
<td><?php echo htmlspecialchars($lead['email']); ?></td>
<td><span class="badge bg-secondary"><?php echo htmlspecialchars($lead['status']); ?></span></td>
<td><?php echo date("M d, Y", strtotime($lead['created_at'])); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- New Lead Modal -->
<div class="modal fade" id="newLeadModal" tabindex="-1" aria-labelledby="newLeadModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="newLeadModalLabel">Create New Lead</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="leads.php" method="POST">
<div class="modal-body">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="company" class="form-label">Company</label>
<input type="text" class="form-control" id="company" name="company">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" id="message" name="message" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Lead</button>
</div>
</form>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>