35429-vm/contacts.php
2025-11-02 20:12:50 +00:00

111 lines
5.0 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'includes/header.php';
try {
$pdo = db();
$stmt_contacts = $pdo->query("SELECT contacts.*, clients.name as client_name FROM contacts JOIN clients ON contacts.client_id = clients.id ORDER BY contacts.name ASC");
$contacts = $stmt_contacts->fetchAll(PDO::FETCH_ASSOC);
$stmt_clients = $pdo->query("SELECT id, name FROM clients ORDER BY name ASC");
$clients = $stmt_clients->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo '<div class="alert alert-danger">Error fetching data: ' . $e->getMessage() . '</div>';
$contacts = [];
$clients = [];
}
?>
<div class="container mt-4">
<div class="d-flex justify-content-between align-items-center mb-3">
<h1 class="h2">Contacts</h1>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addContactModal">
Add Contact
</button>
</div>
<div class="card">
<div class="card-body">
<table class="table table-hover">
<thead class="table-light">
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Client</th>
<th>Role</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($contacts)): ?>
<tr>
<td colspan="6" class="text-center">No contacts found.</td>
</tr>
<?php else: ?>
<?php foreach ($contacts as $contact): ?>
<tr>
<td><?php echo htmlspecialchars($contact['name']); ?></td>
<td><?php echo htmlspecialchars($contact['email']); ?></td>
<td><?php echo htmlspecialchars($contact['phone']); ?></td>
<td><?php echo htmlspecialchars($contact['client_name']); ?></td>
<td><?php echo htmlspecialchars($contact['role']); ?></td>
<td>
<!-- Actions like edit/delete can be added here -->
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<!-- Add Contact Modal -->
<div class="modal fade" id="addContactModal" tabindex="-1" aria-labelledby="addContactModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addContactModalLabel">Add New Contact</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form action="add_contact.php" method="POST">
<div class="mb-3">
<label for="client_id" class="form-label">Client</label>
<select class="form-select" id="client_id" name="client_id" required>
<option value="">Select a client</option>
<?php foreach ($clients as $client): ?>
<option value="<?php echo $client['id']; ?>"><?php echo htmlspecialchars($client['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<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="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="tel" class="form-control" id="phone" name="phone">
</div>
<div class="mb-3">
<label for="role" class="form-label">Role</label>
<input type="text" class="form-control" id="role" name="role">
</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 Contact</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>