-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_dashboard.php
More file actions
351 lines (323 loc) · 21 KB
/
Copy pathadmin_dashboard.php
File metadata and controls
351 lines (323 loc) · 21 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
session_start();
include 'DBConn.php';
// --- SECURITY GATE ---
if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
header("Location: admin_login.php");
exit();
}
// --- PHP DB LOGIC & ACTION HANDLERS ---
// 1. User Access Approval Logic
if (isset($_GET['approve_id'])) {
$id = mysqli_real_escape_string($conn, $_GET['approve_id']);
$conn->query("UPDATE users SET is_approved = 1 WHERE id = '$id'");
header("Location: admin_dashboard.php");
exit();
}
// 2. Marketplace Seller Item Approval Logic (Feature 6 Compliance)
if (isset($_GET['approve_item_id'])) {
$item_id = mysqli_real_escape_string($conn, $_GET['approve_item_id']);
$check_column = mysqli_query($conn, "SHOW COLUMNS FROM products LIKE 'listing_status'");
if (mysqli_num_rows($check_column) > 0) {
$conn->query("UPDATE products SET listing_status = 'approved' WHERE id = '$item_id'");
}
header("Location: admin_dashboard.php");
exit();
}
// 3. Marketplace Seller Item Rejection Logic
if (isset($_GET['reject_item_id'])) {
$item_id = mysqli_real_escape_string($conn, $_GET['reject_item_id']);
$check_column = mysqli_query($conn, "SHOW COLUMNS FROM products LIKE 'listing_status'");
if (mysqli_num_rows($check_column) > 0) {
$conn->query("UPDATE products SET listing_status = 'rejected' WHERE id = '$item_id'");
}
header("Location: admin_dashboard.php");
exit();
}
// 4. Order Status Live Pipeline Update Logic (Matches your 'orders' schema)
if (isset($_GET['update_order_id']) && isset($_GET['new_status'])) {
$order_id = mysqli_real_escape_string($conn, $_GET['update_order_id']);
$new_status = mysqli_real_escape_string($conn, $_GET['new_status']);
// Kept lowercase to match your existing database rows ('pending')
$valid_statuses = ['pending', 'processed', 'delivered'];
if (in_array(strtolower($new_status), $valid_statuses)) {
$conn->query("UPDATE orders SET status = '" . strtolower($new_status) . "' WHERE id = '$order_id'");
}
header("Location: admin_dashboard.php");
exit();
}
// 5. Admin Communication Dispatch Handler (Feature 7 Compliance)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['send_dispatch'])) {
$order_id = mysqli_real_escape_string($conn, $_POST['order_id']);
$recipient = mysqli_real_escape_string($conn, $_POST['recipient_type']);
$raw_message = mysqli_real_escape_string($conn, $_POST['dispatch_msg']);
if (!empty($raw_message)) {
// Safe alternative update behavior if your table layout doesn't use a tracking notes text column yet
$audit_payload = " [Admin Note to $recipient (" . date('Y-m-d H:i') . "): " . $raw_message . "]";
// Checks if there's an address field, otherwise safe to skip or append to text fields
$check_addr = mysqli_query($conn, "SHOW COLUMNS FROM orders LIKE 'delivery_address'");
if (mysqli_num_rows($check_addr) > 0) {
$conn->query("UPDATE orders SET delivery_address = CONCAT(IFNULL(delivery_address,''), '$audit_payload') WHERE id = '$order_id'");
}
}
header("Location: admin_dashboard.php");
exit();
}
// --- DATA FETCHING ENGINE ---
// A. Pending User Approvals
$pending_users = mysqli_query($conn, "SELECT id, full_name, email, created_at FROM users WHERE is_approved = 0 AND role != 'admin' ORDER BY created_at DESC");
// B. Metric: Total Inventory Pieces
$inventory_query = mysqli_query($conn, "SELECT COUNT(*) as total FROM products");
$inventory_data = mysqli_fetch_assoc($inventory_query);
$total_inventory_pieces = $inventory_data['total'] ?? 0;
// C. Metric: Active Orders Count (Queries your exact 'orders' table)
$active_orders_count = 0;
$orders_count_query = mysqli_query($conn, "SELECT COUNT(*) as total FROM orders WHERE status != 'delivered'");
if ($orders_count_query) {
$orders_count_data = mysqli_fetch_assoc($orders_count_query);
$active_orders_count = $orders_count_data['total'] ?? 0;
}
// D. Metric: Cumulative Enterprise Revenue Calculation (ZAR from your total_amount column)
$revenue_total_zar = 0.00;
$revenue_query = mysqli_query($conn, "SELECT SUM(total_amount) as total FROM orders WHERE status = 'delivered'");
if ($revenue_query) {
$revenue_data = mysqli_fetch_assoc($revenue_query);
$revenue_total_zar = $revenue_data['total'] ?? 0.00;
}
// E. Pending Product Requests submitted by Sellers (Feature 6 Verification)
$check_column = mysqli_query($conn, "SHOW COLUMNS FROM products LIKE 'listing_status'");
$has_seller_id = mysqli_query($conn, "SHOW COLUMNS FROM products LIKE 'sellerID'");
if (mysqli_num_rows($check_column) > 0 && mysqli_num_rows($has_seller_id) > 0) {
$pending_items = mysqli_query($conn, "SELECT p.*, u.full_name as seller_name FROM products p JOIN users u ON p.sellerID = u.id WHERE p.listing_status = 'pending' ORDER BY p.id DESC");
} else {
$pending_items = false;
}
// F. Live Tracking Logistics Pipeline (Feature 7 Verification using your exact orders schema)
// Handles NULL fields seamlessly if user accounts are unlinked or guest accounts
$recent_orders = mysqli_query($conn, "SELECT o.id as orderID, o.status, o.order_date, o.total_amount, IFNULL(u.full_name, 'Guest Customer') as buyer_name
FROM orders o
LEFT JOIN users u ON o.user_id = u.id
ORDER BY o.id DESC LIMIT 15");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Control Unit | Pastimes®</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;700;900&display=swap');
:root {
--sidebar-width: 260px;
--accent-blue: #0678eb;
--bg-pure: #000000;
--card-bg: #0c0c0c;
--border-dim: #222;
--nav-text-muted: #666;
--nav-text-hover: #fff;
--terminate-red: #ff3e3e;
}
body { background-color: var(--bg-pure); color: #fff; font-family: 'Inter', sans-serif; margin: 0; }
.sidebar { width: var(--sidebar-width); height: 100vh; background: #000; border-right: 1px solid var(--border-dim); position: fixed; top: 0; left: 0; padding: 20px 0; display: flex; flex-direction: column; justify-content: space-between; }
.brand-section { padding: 20px 30px; }
.brand-logo { font-weight: 900; letter-spacing: 4px; font-size: 20px; color: #fff; text-decoration: none; display: block; text-transform: uppercase; }
.admin-unit-label { font-size: 9px; text-transform: uppercase; letter-spacing: 1px; color: #444; display: block; margin-top: 5px; }
.nav-menu { display: flex; flex-direction: column; width: 100%; margin-top: 50px; flex-grow: 1; }
.nav-item-pastimes { width: 100%; padding-left: 30px; }
.nav-link-pastimes { color: var(--nav-text-muted); text-transform: uppercase; font-size: 11px; font-weight: 800; letter-spacing: 2px; padding: 18px 0; transition: 0.3s; display: flex; align-items: center; text-decoration: none; width: 100%; }
.nav-link-pastimes:hover { color: var(--nav-text-hover); }
.nav-link-pastimes.active { color: var(--nav-text-hover); }
.nav-link-pastimes i { margin-right: 15px; width: 20px; text-align: center; }
.terminate-section { width: 100%; padding-left: 30px; margin-bottom: 20px; border-top: 1px solid var(--border-dim); }
.btn-terminate-pastimes { color: var(--terminate-red); font-weight: 800; text-transform: uppercase; font-size: 11px; letter-spacing: 2px; padding: 18px 0; display: flex; align-items: center; text-decoration: none; background: none; border: none; width: 100%; }
.btn-terminate-pastimes:hover { color: #fff; }
.main-content { margin-left: var(--sidebar-width); padding: 60px; }
.welcome-header { text-transform: uppercase; font-size: 26px; font-weight: 900; letter-spacing: 1px; margin-bottom: 5px; color: #fff; }
.status-line { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: #666; }
.stat-card-pastimes { background: #000; border: 1px solid var(--border-dim); padding: 30px; height: 100%; }
.stat-label-pastimes { font-size: 10px; text-transform: uppercase; font-weight: 800; letter-spacing: 2px; color: #444; margin-bottom: 12px; }
.stat-value-pastimes { font-size: 34px; font-weight: 900; color: #fff; line-height: 1; }
.section-title { font-size: 11px; text-transform: uppercase; font-weight: 900; letter-spacing: 3px; margin-bottom: 30px; color: var(--accent-blue); }
.custom-table { width: 100%; border-collapse: separate; border-spacing: 0 10px; margin-bottom: 40px; }
.table-header { color: #888; font-size: 10px; text-transform: uppercase; font-weight: 800; letter-spacing: 1px; }
.table-row { background: #080808; border: 1px solid #111; }
.table-row td { padding: 20px; border-top: 1px solid #151515; border-bottom: 1px solid #151515; color: #eee; font-size: 13px; vertical-align: middle; }
.btn-approve { background: #00ff88; color: #000; font-size: 10px; font-weight: 900; text-transform: uppercase; border: none; padding: 8px 20px; text-decoration: none; display: inline-block; }
.btn-reject { background: var(--terminate-red); color: #fff; font-size: 10px; font-weight: 900; text-transform: uppercase; border: none; padding: 8px 20px; text-decoration: none; display: inline-block; }
.btn-pipeline { background: transparent; color: #888; border: 1px solid #333; font-size: 9px; font-weight: 700; text-transform: uppercase; padding: 4px 10px; text-decoration: none; margin-right: 4px; }
.btn-pipeline:hover { color: #fff; border-color: #fff; }
.thumb-preview { width: 50px; height: 50px; object-fit: cover; border: 1px solid #222; }
.dispatch-input { background: #111; border: 1px solid #333; color: #fff; font-size: 11px; padding: 6px; border-radius: 0; }
.dispatch-select { background: #111; border: 1px solid #333; color: #888; font-size: 10px; text-transform: uppercase; padding: 6px; border-radius: 0; }
</style>
</head>
<body>
<div class="sidebar">
<div class="brand-section">
<a href="admin_dashboard.php" class="brand-logo">PASTIMES®</a>
<span class="admin-unit-label">Admin Control Unit</span>
</div>
<nav class="nav-menu">
<div class="nav-item-pastimes"><a href="admin_dashboard.php" class="nav-link-pastimes active"><i class="fa-solid fa-chart-line"></i> Overview</a></div>
<div class="nav-item-pastimes"><a href="admin_products.php" class="nav-link-pastimes"><i class="fa-solid fa-tags"></i> Inventory</a></div>
<div class="nav-item-pastimes"><a href="admin_orders.php" class="nav-link-pastimes"><i class="fa-solid fa-receipt"></i> Orders</a></div>
<div class="nav-item-pastimes"><a href="admin_users.php" class="nav-link-pastimes"><i class="fa-solid fa-users"></i> Customers</a></div>
<div class="nav-item-pastimes"><a href="admin_consignments.php" class="nav-link-pastimes"><i class="fa-solid fa-handshake"></i> Consignments</a></div>
</nav>
<div class="terminate-section">
<a href="logout.php" class="btn-terminate-pastimes"><i class="fa-solid fa-power-off"></i> Terminate Session</a>
</div>
</div>
<div class="main-content">
<div class="d-flex justify-content-end mb-4">
<a href="index.php" class="btn btn-outline-light btn-sm rounded-0 text-uppercase" style="font-size: 10px; font-weight: 900; padding: 10px 20px;">Live Site</a>
</div>
<h1 class="welcome-header">Welcome back, Administrator</h1>
<div class="status-line mb-5">System operational. Midrand HQ Status: <span style="color: #00ff88;">Online</span></div>
<div class="row g-4 mb-5">
<div class="col-md-4">
<div class="stat-card-pastimes">
<div class="stat-label-pastimes">Total Inventory</div>
<div class="stat-value-pastimes"><?php echo $total_inventory_pieces; ?> Pieces</div>
</div>
</div>
<div class="col-md-4">
<div class="stat-card-pastimes">
<div class="stat-label-pastimes">Active Orders</div>
<div class="stat-value-pastimes"><?php echo $active_orders_count; ?></div>
</div>
</div>
<div class="col-md-4">
<div class="stat-card-pastimes">
<div class="stat-label-pastimes">Revenue (ZAR)</div>
<div class="stat-value-pastimes"><span class="currency-symbol">R</span><?php echo number_format($revenue_total_zar, 2); ?></div>
</div>
</div>
</div>
<div class="activity-section mt-5">
<h3 class="section-title">Pending Access Authorizations</h3>
<table class="custom-table">
<thead>
<tr class="table-header">
<th>User Identity</th>
<th>Email Address</th>
<th>Applied On</th>
<th class="text-end">Action</th>
</tr>
</thead>
<tbody>
<?php if($pending_users && mysqli_num_rows($pending_users) > 0): ?>
<?php while($user = mysqli_fetch_assoc($pending_users)): ?>
<tr class="table-row">
<td class="fw-bold text-uppercase"><?php echo htmlspecialchars($user['full_name']); ?></td>
<td style="color: #666;"><?php echo htmlspecialchars($user['email']); ?></td>
<td><?php echo date('M d, Y', strtotime($user['created_at'])); ?></td>
<td class="text-end">
<a href="admin_dashboard.php?approve_id=<?php echo $user['id']; ?>" class="btn-approve">Approve Access</a>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr><td colspan="4" class="text-center text-muted py-4">No pending user access authorizations.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="activity-section mt-5">
<h3 class="section-title">Pending Marketplace Seller Items</h3>
<table class="custom-table">
<thead>
<tr class="table-header">
<th>Asset Preview</th>
<th>Item Details</th>
<th>Brand Identity</th>
<th>Submitting Seller</th>
<th>Price point</th>
<th class="text-end">Verification Actions</th>
</tr>
</thead>
<tbody>
<?php if($pending_items && mysqli_num_rows($pending_items) > 0): ?>
<?php while($item = mysqli_fetch_assoc($pending_items)): ?>
<?php
$img = htmlspecialchars($item['image']);
$path = (stripos($img, "images/") === 0) ? $img : "images/" . $img;
?>
<tr class="table-row">
<td><img src="<?php echo $path; ?>" class="thumb-preview" alt="Asset"></td>
<td>
<span class="fw-bold text-white d-block"><?php echo htmlspecialchars($item['name']); ?></span>
<small style="color:#555;"><?php echo htmlspecialchars($item['description']); ?></small>
</td>
<td class="text-uppercase fw-bold text-info" style="font-size:11px;"><?php echo htmlspecialchars($item['brand'] ?? 'Generic'); ?></td>
<td><?php echo htmlspecialchars($item['seller_name']); ?></td>
<td class="fw-bold">R<?php echo number_format($item['price'], 2); ?></td>
<td class="text-end">
<a href="admin_dashboard.php?approve_item_id=<?php echo $item['id']; ?>" class="btn-approve me-1">Approve Listing</a>
<a href="admin_dashboard.php?reject_item_id=<?php echo $item['id']; ?>" class="btn-reject">Reject</a>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr><td colspan="6" class="text-center text-muted py-4">No marketplace listing requests found. To track item approval workflows, ensure your products table contains 'listing_status' and 'sellerID' columns.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
<div class="activity-section mt-5">
<h3 class="section-title">Logistics Pipeline & Mediation Control (Feature 7)</h3>
<table class="custom-table">
<thead>
<tr class="table-header">
<th>Order Ref</th>
<th>Consignee</th>
<th>Order Timestamp</th>
<th>Transaction Total</th>
<th>Pipeline Status</th>
<th>Pipeline Action</th>
<th style="width: 300px;">Quality Compliance Dispatch Note</th>
</tr>
</thead>
<tbody>
<?php if($recent_orders && mysqli_num_rows($recent_orders) > 0): ?>
<?php while($order = mysqli_fetch_assoc($recent_orders)): ?>
<tr class="table-row">
<td class="fw-bold text-info">#<?php echo $order['orderID']; ?></td>
<td><?php echo htmlspecialchars($order['buyer_name']); ?></td>
<td style="color:#888;"><?php echo date('Y-m-d H:i', strtotime($order['order_date'])); ?></td>
<td class="fw-bold">R<?php echo number_format($order['total_amount'], 2); ?></td>
<td>
<span style="font-size:11px; font-weight:800; text-transform:uppercase; color: <?php echo (strtolower($order['status']) === 'delivered') ? '#00ff88' : ((strtolower($order['status']) === 'processed') ? '#0678eb' : '#ffcc00'); ?>;">
<?php echo htmlspecialchars($order['status']); ?>
</span>
</td>
<td>
<div class="d-flex">
<a href="admin_dashboard.php?update_order_id=<?php echo $order['orderID']; ?>&new_status=pending" class="btn-pipeline">Pending</a>
<a href="admin_dashboard.php?update_order_id=<?php echo $order['orderID']; ?>&new_status=processed" class="btn-pipeline">Process</a>
<a href="admin_dashboard.php?update_order_id=<?php echo $order['orderID']; ?>&new_status=delivered" class="btn-pipeline">Deliver</a>
</div>
</td>
<td>
<form method="POST" action="admin_dashboard.php" class="d-flex gap-1">
<input type="hidden" name="order_id" value="<?php echo $order['orderID']; ?>">
<select name="recipient_type" class="dispatch-select">
<option value="Buyer">Buyer</option>
<option value="Seller">Seller</option>
</select>
<input type="text" name="dispatch_msg" placeholder="Flag quality or tracking info..." required class="form-control dispatch-input">
<button type="submit" name="send_dispatch" class="btn btn-primary rounded-0" style="font-size:9px; padding:0 8px;"><i class="fa-solid fa-paper-plane"></i></button>
</form>
</td>
</tr>
<?php endwhile; ?>
<?php else: ?>
<tr><td colspan="7" class="text-center text-muted py-4">No records found inside your 'orders' table.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</body>
</html>