diff --git a/admin/loyalty.php b/admin/loyalty.php index 477502e..93a9c65 100644 --- a/admin/loyalty.php +++ b/admin/loyalty.php @@ -2,64 +2,187 @@ require_once __DIR__ . '/../db/config.php'; $pdo = db(); -$query = "SELECT * FROM loyalty_customers ORDER BY points DESC"; -$customers_pagination = paginate_query($pdo, $query); -$customers = $customers_pagination['data']; +// Handle Settings Update +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_settings'])) { + $points_per_order = intval($_POST['points_per_order']); + $points_for_free_meal = intval($_POST['points_for_free_meal']); + + $stmt = $pdo->prepare("UPDATE loyalty_settings SET points_per_order = ?, points_for_free_meal = ? WHERE id = 1"); + $stmt->execute([$points_per_order, $points_for_free_meal]); + + $success_msg = "Loyalty settings updated successfully!"; +} + +// Fetch Settings +$stmt = $pdo->query("SELECT * FROM loyalty_settings WHERE id = 1"); +$settings = $stmt->fetch(PDO::FETCH_ASSOC); + +if (!$settings) { + // Default fallback if migration failed or empty + $settings = ['points_per_order' => 10, 'points_for_free_meal' => 70]; +} + +// Fetch Customers with Points +$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1; +$limit = 20; +$offset = ($page - 1) * $limit; + +$count_stmt = $pdo->query("SELECT COUNT(*) FROM customers WHERE points > 0"); +$total_customers = $count_stmt->fetchColumn(); +$total_pages = ceil($total_customers / $limit); + +$query = "SELECT * FROM customers WHERE points > 0 ORDER BY points DESC LIMIT $limit OFFSET $offset"; +$stmt = $pdo->query($query); +$customers = $stmt->fetchAll(PDO::FETCH_ASSOC); include 'includes/header.php'; ?>
| ID | -Name | -Points Balance | -Joined | -|
|---|---|---|---|---|
| #= $customer['id'] ?> | -= htmlspecialchars($customer['name']) ?> | -= htmlspecialchars($customer['email']) ?> | -- - = $customer['points'] ?> pts - - | -= date('M d, Y', strtotime($customer['created_at'])) ?> | -
| No loyalty members yet. | -||||
| Customer | +Contact | +Points Balance | +Status | +Joined | +
|---|---|---|---|---|
|
+ = htmlspecialchars($customer['name']) ?>
+ |
+
+ = htmlspecialchars($customer['phone'] ?? '-') ?>
+ = htmlspecialchars($customer['email'] ?? '') ?>
+ |
+
+
+
+ = $customer['points'] ?> pts
+
+
+
+
+
+ |
+ + + + Eligible for Free Meal + + + + = $settings['points_for_free_meal'] - $customer['points'] ?> pts to go + + + | += date('M d, Y', strtotime($customer['created_at'])) ?> | +
| No active loyalty members found. | +||||