-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomer_dashboard.php
More file actions
230 lines (214 loc) Β· 9.74 KB
/
customer_dashboard.php
File metadata and controls
230 lines (214 loc) Β· 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'customer') {
header("Location: login.php");
exit();
}
include 'db.php';
// Fetch customer details
$customer_id = $_SESSION['user_id'];
$customer_sql = "SELECT username, email FROM user WHERE id = ?";
$stmt = $conn->prepare($customer_sql);
$stmt->bind_param("i", $customer_id);
$stmt->execute();
$customer_result = $stmt->get_result();
$customer = $customer_result->fetch_assoc();
// Fetch order history
$order_sql = "SELECT id, order_date, total_price, status FROM orders WHERE user_id = ? ORDER BY order_date DESC";
$stmt = $conn->prepare($order_sql);
$stmt->bind_param("i", $customer_id);
$stmt->execute();
$order_result = $stmt->get_result();
// Fetch cart items
$cart_items = isset($_SESSION['cart']) ? $_SESSION['cart'] : [];
// Fetch sales trends
$sales_sql = "SELECT DATE(order_date) as order_date, SUM(total_price) as total_revenue
FROM orders WHERE status = 'Completed'
GROUP BY DATE(order_date)";
$sales_result = $conn->query($sales_sql);
$dates = [];
$sales = [];
while ($row = $sales_result->fetch_assoc()) {
$dates[] = $row['order_date'];
$sales[] = $row['total_revenue'];
}
// Fetch total orders & amount spent
$order_summary_sql = "SELECT COUNT(id) AS total_orders, SUM(total_price) AS total_spent FROM orders WHERE user_id = ?";
$stmt = $conn->prepare($order_summary_sql);
$stmt->bind_param("i", $customer_id);
$stmt->execute();
$result = $stmt->get_result()->fetch_assoc();
$total_orders = $result['total_orders'] ?? 0;
$total_spent = $result['total_spent'] ?? 0.00;
// Fetch most purchased product
$product_sql = "SELECT products.name, COUNT(order_items.product_id) AS count
FROM order_items
JOIN products ON order_items.product_id = products.id
JOIN orders ON order_items.order_id = orders.id
WHERE orders.user_id = ?
GROUP BY order_items.product_id
ORDER BY count DESC LIMIT 1";
$stmt = $conn->prepare($product_sql);
$stmt->bind_param("i", $customer_id);
$stmt->execute();
$product_result = $stmt->get_result()->fetch_assoc();
$most_purchased_product = $product_result['name'] ?? 'N/A';
// Next Offer (Static for now, but can be based on DB logic)
$next_discount_offer = "Get 10% off on your next purchase!";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Customer Dashboard</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body { background-color: #f8f9fa; }
.sidebar { background-color: #343a40; height: 100vh; padding-top: 20px; position: fixed; width: 250px; }
.sidebar a { color: #fff; font-size: 18px; padding: 12px; display: block; text-decoration: none; }
.sidebar a:hover { background-color: #495057; }
.main-content { margin-left: 250px; padding: 20px; }
</style>
</head>
<body>
<!-- Sidebar -->
<div class="sidebar position-fixed">
<h3 class="text-center text-white">Customer Panel</h3>
<a href="browse_products.php">ποΈ Shop Products</a>
<a href="order_history.php">π Orders History</a>
<a href="customer_orders.php">π my Orders</a>
<a href="view_cart.php">π View Cart</a>
<a href="wishlist.php">β€οΈ My Wishlist</a>
<a href="profile.php">π€ My Profile</a>
<a href="logout.php" class="text-danger">πͺ Logout</a>
</div>
<!-- Main Content -->
<div class="main-content">
<div class="container mt-4">
<!-- Customer Insights Card -->
<div class="card mt-4 shadow-lg">
<div class="card-header text-center bg-success text-white">
<h5>π Your Purchase Insights</h5>
</div>
<div class="card-body">
<ul class="list-group">
<li class="list-group-item">π <strong>Total Orders Placed:</strong> <?php echo $total_orders; ?></li>
<li class="list-group-item">π° <strong>Total Amount Spent:</strong> βΉ<?php echo number_format($total_spent, 2); ?></li>
<li class="list-group-item">π¦ <strong>Most Purchased Product:</strong> <?php echo $most_purchased_product; ?></li>
<li class="list-group-item">π <strong>Next Offer:</strong> <?php echo $next_discount_offer; ?></li>
</ul>
</div>
</div>
<!-- Order History -->
<div class="card mt-4 shadow-lg">
<div class="card-header text-center bg-primary text-white">
<h5>π My Order History</h5>
</div>
<div class="card-body">
<table class="table table-striped">
<thead>
<tr>
<th>Order ID</th>
<th>Date</th>
<th>Total Price</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while ($row = $order_result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['order_date']; ?></td>
<td>βΉ<?php echo number_format($row['total_price'], 2); ?></td>
<td><?php echo $row['status']; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
<!-- Cart Items -->
<div class="card mt-4 shadow-lg">
<div class="card-header text-center bg-warning text-white">
<h5>π Your Cart</h5>
</div>
<div class="card-body">
<?php if (empty($cart_items)): ?>
<p class="text-danger">Your cart is empty.</p>
<?php else: ?>
<table class="table table-bordered">
<thead>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$total_price = 0;
foreach ($cart_items as $id => $item):
$total = $item['price'] * $item['quantity'];
$total_price += $total;
?>
<tr>
<td><?php echo htmlspecialchars($item['name']); ?></td>
<td>βΉ<?php echo number_format($item['price'], 2); ?></td>
<td><?php echo $item['quantity']; ?></td>
<td>βΉ<?php echo number_format($total, 2); ?></td>
<td><a href="remove_from_cart.php?id=<?php echo $id; ?>" class="text-danger">Remove</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h5>Total Price: βΉ<?php echo number_format($total_price, 2); ?></h5>
<a href="checkout.php" class="btn btn-success">π Proceed to Checkout</a>
<?php endif; ?>
</div>
</div>
<!-- Wishlist -->
<div class="card mt-4 shadow-lg">
<div class="card-header text-center bg-danger text-white">
<h5>β€οΈ My Wishlist</h5>
</div>
<div class="card-body">
<p class="text-muted">Feature coming soon...</p>
</div>
</div>
<!-- Profile -->
<div class="card mt-4 shadow-lg">
<div class="card-header text-center bg-info text-white">
<h5>π€ My Profile</h5>
</div>
<div class="card-body">
<p><strong>Name:</strong> <?php echo htmlspecialchars($customer['username']); ?></p>
<p><strong>Email:</strong> <?php echo htmlspecialchars($customer['email']); ?></p>
<a href="profile.php" class="btn btn-primary">Edit Profile</a>
</div>
</div>
<script>
var ctx = document.getElementById('salesChart').getContext('2d');
var salesChart = new Chart(ctx, {
type: 'line',
data: {
labels: <?php echo json_encode($dates); ?>,
datasets: [{
label: 'Total Revenue',
data: <?php echo json_encode($sales); ?>,
borderColor: 'rgba(75, 192, 192, 1)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderWidth: 2
}]
},
options: {
responsive: true,
maintainAspectRatio: false
}
});
</script>
</body>
</html>