38348-vm/customers.php
2026-02-11 01:46:33 +00:00

116 lines
4.9 KiB
PHP

<?php
$page_title = "Customers";
require_once 'includes/header.php';
$search = $_GET['search'] ?? '';
$where = "deleted_at IS NULL";
$params = [];
if ($search) {
$where .= " AND (name LIKE ? OR email LIKE ? OR phone LIKE ?)";
$params = ["%$search%", "%$search%", "%$search%"];
}
$stmt = db()->prepare("SELECT * FROM customers WHERE $where ORDER BY created_at DESC");
$stmt->execute($params);
$customers = $stmt->fetchAll();
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="fw-bold m-0">Customers</h2>
<?php if (in_array($user_role, ['Admin', 'Sales'])): ?>
<a href="customer_form.php" class="btn btn-primary">
<i class="bi bi-plus-lg me-2"></i>New Customer
</a>
<?php endif; ?>
</div>
<div class="card mb-4">
<div class="card-body p-0">
<div class="p-3 border-bottom bg-light">
<form class="row g-2" method="GET">
<div class="col-auto">
<div class="input-group input-group-sm">
<span class="input-group-text bg-white border-end-0"><i class="bi bi-search"></i></span>
<input type="text" name="search" class="form-control border-start-0" placeholder="Search customers..." value="<?= e($search) ?>">
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-sm btn-outline-secondary">Search</button>
</div>
<?php if ($search): ?>
<div class="col-auto">
<a href="customers.php" class="btn btn-sm btn-link text-decoration-none">Clear</a>
</div>
<?php endif; ?>
</form>
</div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead>
<tr>
<th class="ps-4">Name</th>
<th>Contact</th>
<th>Category</th>
<th>Status</th>
<th>Created</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($customers)): ?>
<tr>
<td colspan="6" class="text-center py-5 text-muted">
<i class="bi bi-people fs-1 d-block mb-3"></i>
No customers found.
</td>
</tr>
<?php endif; ?>
<?php foreach ($customers as $c): ?>
<tr>
<td class="ps-4">
<div class="fw-bold"><?= e($c['name']) ?></div>
<div class="text-muted small"><?= e($c['address']) ?></div>
</td>
<td>
<div><?= e($c['email']) ?></div>
<div class="text-muted small"><?= e($c['phone']) ?></div>
</td>
<td><span class="text-secondary"><?= e($c['category']) ?></span></td>
<td>
<?php
$badge = 'bg-secondary';
if ($c['status'] == 'Active') $badge = 'bg-success';
if ($c['status'] == 'Prospect') $badge = 'bg-info text-dark';
if ($c['status'] == 'Inactive') $badge = 'bg-danger';
?>
<span class="badge <?= $badge ?>"><?= e($c['status']) ?></span>
</td>
<td class="text-muted"><?= date('M d, Y', strtotime($c['created_at'])) ?></td>
<td class="text-end pe-4">
<?php if (in_array($user_role, ['Admin', 'Sales'])): ?>
<a href="customer_form.php?id=<?= $c['id'] ?>" class="btn btn-sm btn-outline-secondary py-1 px-2" title="Edit"><i class="bi bi-pencil"></i></a>
<?php endif; ?>
<?php if ($user_role === 'Admin'): ?>
<button type="button" class="btn btn-sm btn-outline-danger py-1 px-2 ms-1" title="Delete" onclick="deleteCustomer(<?= $c['id'] ?>)"><i class="bi bi-trash"></i></button>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
function deleteCustomer(id) {
if (confirm('Are you sure you want to delete this customer?')) {
window.location.href = 'customer_delete.php?id=' + id;
}
}
</script>
<?php require_once 'includes/footer.php'; ?>