<?php
$pageTitle = 'Customers';
require_once __DIR__ . '/../../app/includes/header.php'; require_role_access('customer:view');
$customers = current_user()['role'] === 'admin' 
    ? db()->query('SELECT * FROM customers ORDER BY id DESC')->fetchAll() 
    : db()->query('SELECT * FROM customers WHERE created_by = ' . (int)current_user()['id'] . ' ORDER BY id DESC')->fetchAll();
?>
<div class="page-head"><div><h1>Customers</h1><p>Customer profiles used in quotations.</p></div><a class="btn btn-primary" href="<?= APP_BASE ?>/customers/create.php"><i class="bi bi-plus-lg me-1"></i>Add Customer</a></div>
<div class="card app-card"><div class="table-responsive"><table class="table align-middle mb-0">
<thead><tr><th>Name</th><th>Phone</th><th>Email</th><th>Location</th><th>Type</th><th>Created</th><th>Actions</th></tr></thead><tbody>
<?php foreach ($customers as $c): ?><tr><td><?= e($c['customer_name']) ?></td><td><?= e($c['phone']) ?></td><td><?= e($c['email']) ?></td><td><?= e($c['location']) ?></td><td><?= e($c['customer_type']) ?></td><td><?= e(date('Y-m-d', strtotime($c['created_at']))) ?></td><td><a href="<?= APP_BASE ?>/customers/edit.php?id=<?= $c['id'] ?>" class="btn btn-sm btn-outline-secondary">Edit</a></td></tr><?php endforeach; ?>
<?php if (!$customers): ?><tr><td colspan="7" class="text-center text-muted py-4">No customers found.</td></tr><?php endif; ?>
</tbody></table></div></div>
<?php require_once __DIR__ . '/../../app/includes/footer.php'; ?>
