-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
521 lines (454 loc) · 22.4 KB
/
Copy pathadmin.js
File metadata and controls
521 lines (454 loc) · 22.4 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Admin Panel JavaScript
document.addEventListener('DOMContentLoaded', () => {
initLucide();
checkAdminSession();
initAdminLogin();
initAdminLogout();
initMobileEnhancements();
});
// Toggle show/hide admin password
function toggleAdminPassword() {
const passwordInput = document.getElementById('admin-password');
const icon = document.getElementById('toggle-password-icon');
const btn = document.getElementById('toggle-password');
if (!passwordInput || !icon) return;
const isHidden = passwordInput.type === 'password';
passwordInput.type = isHidden ? 'text' : 'password';
// Swap icon between eye and eye-off
icon.setAttribute('data-lucide', isHidden ? 'eye-off' : 'eye');
btn.style.color = isHidden ? 'var(--color-primary, #10b981)' : 'var(--color-text-muted, #888)';
// Re-render Lucide icons
if (typeof lucide !== 'undefined') lucide.createIcons();
}
window.toggleAdminPassword = toggleAdminPassword;
// Initialize Lucide icons
function initLucide() {
if (typeof lucide !== 'undefined') {
lucide.createIcons();
}
}
// Check if admin is logged in
async function checkAdminSession() {
try {
const response = await fetch('/api/admin/check', { credentials: 'same-origin' });
const result = await response.json();
if (result.isAdmin) {
showAdminDashboard();
} else {
showAdminLogin();
}
} catch (error) {
console.error('Session check failed:', error);
showAdminLogin();
}
}
// Show admin login form
function showAdminLogin() {
document.getElementById('admin-login-section').classList.remove('hidden');
document.getElementById('admin-dashboard-section').classList.add('hidden');
}
// Show admin dashboard
function showAdminDashboard() {
document.getElementById('admin-login-section').classList.add('hidden');
document.getElementById('admin-dashboard-section').classList.remove('hidden');
initRevenueDashboard();
initAdminLedger();
}
// Initialize admin login form
function initAdminLogin() {
const form = document.getElementById('admin-login-form');
form.addEventListener('submit', async (e) => {
e.preventDefault();
const username = document.getElementById('admin-username').value;
const password = document.getElementById('admin-password').value;
try {
const response = await fetch('/api/admin/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),
credentials: 'same-origin'
});
const result = await response.json();
if (result.success) {
showAdminDashboard();
} else {
showLoginError(result.error);
}
} catch (error) {
showLoginError('Login failed. Please try again.');
}
});
}
// Show login error
function showLoginError(message) {
const errorDiv = document.getElementById('login-error');
errorDiv.textContent = message;
errorDiv.classList.remove('hidden');
setTimeout(() => {
errorDiv.classList.add('hidden');
}, 3000);
}
// Initialize admin logout
function initAdminLogout() {
const logoutBtn = document.getElementById('admin-logout-btn');
if (logoutBtn) {
logoutBtn.addEventListener('click', async () => {
try {
await fetch('/api/admin/logout', { method: 'POST', credentials: 'same-origin' });
showAdminLogin();
} catch (error) {
console.error('Logout failed:', error);
}
});
}
}
// Revenue Dashboard Functions
async function fetchRevenueAnalytics(timeframe = 'all') {
try {
const response = await fetch(`/api/revenue/analytics?timeframe=${timeframe}`, { credentials: 'same-origin' });
const result = await response.json();
if (result.success) {
updateRevenueDashboard(result.analytics);
}
} catch (error) {
console.error('Failed to fetch revenue analytics:', error);
}
}
function updateRevenueDashboard(analytics) {
// Update revenue metrics
document.getElementById('revenue-total').textContent = `${analytics.totalRevenue.toFixed(2)} NCH`;
document.getElementById('revenue-transactions').textContent = analytics.totalTransactions;
document.getElementById('revenue-avg-fee').textContent = `${analytics.averageFeePerTransaction} NCH`;
document.getElementById('revenue-growth').textContent = `+${analytics.revenueGrowth}%`;
// Update fee breakdown
document.getElementById('revenue-transaction-fees').textContent = `${analytics.feeBreakdown.transactionFees.toFixed(2)} NCH`;
document.getElementById('revenue-premium-fees').textContent = `${analytics.feeBreakdown.premiumFees.toFixed(2)} NCH`;
document.getElementById('revenue-verification-fees').textContent = `${analytics.feeBreakdown.verificationFees.toFixed(2)} NCH`;
document.getElementById('revenue-buyer-registration-fees').textContent = `${analytics.feeBreakdown.buyerRegistrationFees.toFixed(2)} NCH`;
document.getElementById('revenue-buyer-premium-fees').textContent = `${analytics.feeBreakdown.buyerPremiumFees.toFixed(2)} NCH`;
document.getElementById('revenue-matching-fees').textContent = `${analytics.feeBreakdown.buyerMatchingFees.toFixed(2)} NCH`;
// Update progress bars
const total = analytics.totalRevenue || 1;
document.getElementById('revenue-transaction-bar').style.width = `${(analytics.feeBreakdown.transactionFees / total) * 100}%`;
document.getElementById('revenue-premium-bar').style.width = `${(analytics.feeBreakdown.premiumFees / total) * 100}%`;
document.getElementById('revenue-verification-bar').style.width = `${(analytics.feeBreakdown.verificationFees / total) * 100}%`;
document.getElementById('revenue-buyer-registration-bar').style.width = `${(analytics.feeBreakdown.buyerRegistrationFees / total) * 100}%`;
document.getElementById('revenue-buyer-premium-bar').style.width = `${(analytics.feeBreakdown.buyerPremiumFees / total) * 100}%`;
document.getElementById('revenue-matching-bar').style.width = `${(analytics.feeBreakdown.buyerMatchingFees / total) * 100}%`;
// Update recent transactions
updateRecentTransactions(analytics.recentTransactions);
}
function updateRecentTransactions(transactions) {
const container = document.getElementById('recent-transactions-list');
if (!transactions || transactions.length === 0) {
container.innerHTML = '<div class="no-transactions">No transactions yet</div>';
return;
}
container.innerHTML = transactions.map(tx => `
<div class="transaction-item">
<div>
<div class="transaction-type">${tx.type.charAt(0).toUpperCase() + tx.type.slice(1)} Fee</div>
<div class="transaction-time">${new Date(tx.timestamp).toLocaleString()}</div>
</div>
<div class="transaction-amount">${tx.amount.toFixed(2)} NCH</div>
</div>
`).join('');
}
function initRevenueDashboard() {
// Fetch revenue analytics on load
fetchRevenueAnalytics();
// Set up timeframe filter
const timeframeSelect = document.getElementById('revenue-timeframe');
if (timeframeSelect) {
timeframeSelect.addEventListener('change', (e) => {
fetchRevenueAnalytics(e.target.value);
});
}
}
// Mobile Enhancements for Admin Panel
function initMobileEnhancements() {
addTouchFeedback();
optimizeMobileInputs();
preventInputZoom();
}
function addTouchFeedback() {
const touchElements = document.querySelectorAll('button, .card, .revenue-breakdown-item');
touchElements.forEach(element => {
element.addEventListener('touchstart', () => {
element.style.transform = 'scale(0.98)';
element.style.opacity = '0.8';
}, { passive: true });
element.addEventListener('touchend', () => {
element.style.transform = '';
element.style.opacity = '';
}, { passive: true });
});
}
function optimizeMobileInputs() {
const usernameInput = document.getElementById('admin-username');
const passwordInput = document.getElementById('admin-password');
if (usernameInput) {
usernameInput.setAttribute('inputmode', 'text');
usernameInput.setAttribute('autocomplete', 'username');
}
if (passwordInput) {
passwordInput.setAttribute('inputmode', 'text');
passwordInput.setAttribute('autocomplete', 'current-password');
}
}
function preventInputZoom() {
const viewport = document.querySelector('meta[name=viewport]');
if (viewport) {
const inputs = document.querySelectorAll('input, select');
inputs.forEach(input => {
input.addEventListener('focus', () => {
viewport.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0');
});
input.addEventListener('blur', () => {
viewport.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=5.0, user-scalable=yes');
});
});
}
}
// ===== CROP REGISTRATION & VERIFICATION CONSOLE =====
let adminRegistrations = [];
async function initAdminLedger() {
await fetchAdminRegistrations();
}
async function fetchAdminRegistrations() {
try {
const response = await fetch('/api/farmers/registrations', { credentials: 'same-origin' });
const result = await response.json();
if (result.success) {
adminRegistrations = result.registrations;
renderAdminLedgerTable();
} else {
console.error('Failed to load admin registrations:', result.error);
}
} catch (error) {
console.error('Error fetching admin registrations:', error);
}
}
function renderAdminLedgerTable() {
const tbody = document.getElementById('admin-ledger-tbody');
if (!tbody) return;
if (adminRegistrations.length === 0) {
tbody.innerHTML = `
<tr>
<td colspan="8" style="text-align: center; color: var(--color-text-muted); padding: 36px 0; font-style: italic;">
No crop registrations found.
</td>
</tr>
`;
return;
}
tbody.innerHTML = adminRegistrations.map(r => {
const status = r.verificationStatus || 'Pending';
let statusBadge = '';
if (status === 'Geo-Verified') {
statusBadge = `
<span class="status-badge geo-verified" style="background-color: rgba(59, 130, 246, 0.1); color: #3b82f6; border: 1px solid rgba(59, 130, 246, 0.3); display: inline-flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 6px; font-size: 0.85rem; font-weight: 500;">
<i data-lucide="map-pin" style="width: 14px; height: 14px;"></i>
<span>Geo-Verified</span>
</span>
`;
} else if (status === 'Oracle Confirmed') {
statusBadge = `
<span class="status-badge oracle-confirmed" style="background-color: rgba(16, 185, 129, 0.1); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.3); display: inline-flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 6px; font-size: 0.85rem; font-weight: 500;">
<i data-lucide="shield-check" style="width: 14px; height: 14px;"></i>
<span>Oracle Confirmed</span>
</span>
`;
} else if (status === 'Cancelled') {
statusBadge = `
<span class="status-badge cancelled" style="background-color: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.3); display: inline-flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 6px; font-size: 0.85rem; font-weight: 500;">
<i data-lucide="x-circle" style="width: 14px; height: 14px;"></i>
<span>Cancelled</span>
</span>
`;
} else {
statusBadge = `
<span class="status-badge pending" style="background-color: rgba(245, 158, 11, 0.1); color: #f59e0b; border: 1px solid rgba(245, 158, 11, 0.3); display: inline-flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 6px; font-size: 0.85rem; font-weight: 500;">
<i data-lucide="clock" style="width: 14px; height: 14px;"></i>
<span>Pending</span>
</span>
`;
}
let actionButtons = '';
if (status === 'Pending') {
actionButtons = `
<div style="display: flex; gap: 8px; flex-wrap: wrap;">
<button class="btn-secondary btn-sm" onclick="verifyRegistration('${r.id}', 'Geo-Verified')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; cursor: pointer;">
<i data-lucide="map-pin" style="width: 12px; height: 12px;"></i>
<span>Geo-Verify</span>
</button>
<button class="btn-primary btn-sm" onclick="verifyRegistration('${r.id}', 'Oracle Confirmed')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; background: var(--color-primary); cursor: pointer;">
<i data-lucide="shield-check" style="width: 12px; height: 12px;"></i>
<span>Confirm</span>
</button>
<button class="btn-danger btn-sm" onclick="cancelRegistration('${r.id}')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; background: rgba(239, 68, 68, 0.15); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.3); cursor: pointer;">
<i data-lucide="x-circle" style="width: 12px; height: 12px;"></i>
<span>Cancel</span>
</button>
<button class="btn-danger btn-sm" onclick="deleteRegistration('${r.id}')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; background: rgba(239, 68, 68, 0.15); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.3); cursor: pointer;">
<i data-lucide="trash-2" style="width: 12px; height: 12px;"></i>
<span>Delete</span>
</button>
</div>
`;
} else if (status === 'Geo-Verified') {
actionButtons = `
<div style="display: flex; gap: 8px; flex-wrap: wrap;">
<button class="btn-primary btn-sm" onclick="verifyRegistration('${r.id}', 'Oracle Confirmed')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; background: var(--color-primary); cursor: pointer;">
<i data-lucide="shield-check" style="width: 12px; height: 12px;"></i>
<span>Oracle Confirm</span>
</button>
<button class="btn-danger btn-sm" onclick="cancelRegistration('${r.id}')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; background: rgba(239, 68, 68, 0.15); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.3); cursor: pointer;">
<i data-lucide="x-circle" style="width: 12px; height: 12px;"></i>
<span>Cancel</span>
</button>
<button class="btn-danger btn-sm" onclick="deleteRegistration('${r.id}')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; background: rgba(239, 68, 68, 0.15); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.3); cursor: pointer;">
<i data-lucide="trash-2" style="width: 12px; height: 12px;"></i>
<span>Delete</span>
</button>
</div>
`;
} else {
actionButtons = `
<div style="display: flex; gap: 8px; flex-wrap: wrap;">
<button class="btn-danger btn-sm" onclick="deleteRegistration('${r.id}')" style="padding: 4px 8px; font-size: 0.8rem; border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; background: rgba(239, 68, 68, 0.15); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.3); cursor: pointer;">
<i data-lucide="trash-2" style="width: 12px; height: 12px;"></i>
<span>Delete</span>
</button>
</div>
`;
}
const areaHa = parseFloat(r.areaHa) || 0;
const yieldTons = parseFloat(r.expectedYieldTons) || 0;
return `
<tr>
<td data-label="Registry ID"><span class="registry-id">${r.id}</span></td>
<td data-label="Farmer Name"><strong style="color: var(--color-text-primary); font-weight: 600;">${r.farmerName}</strong></td>
<td data-label="Contact Number"><code style="font-family: monospace; font-size: 0.9rem; color: #10b981; background: rgba(16, 185, 129, 0.1); padding: 2px 6px; border-radius: 4px;">${r.contact}</code></td>
<td data-label="Location">
<div>${r.barangay}, ${r.municipality}</div>
<div style="font-size: 0.8rem; color: var(--color-text-secondary);">${r.province}</div>
</td>
<td data-label="Crop">
<span style="font-weight: 500;">${r.vegetableId.toUpperCase()}</span>
</td>
<td data-label="Area & Yield">
<div>${areaHa.toFixed(2)} ha</div>
<div style="font-size: 0.8rem; color: var(--color-text-secondary);">${yieldTons.toFixed(2)} Tons</div>
</td>
<td data-label="Verification Status">
${statusBadge}
</td>
<td data-label="Actions">
${actionButtons}
</td>
</tr>
`;
}).join('');
initLucide();
}
async function verifyRegistration(id, status) {
if (!confirm(`Are you sure you want to verify registration ${id} as ${status}?`)) {
return;
}
try {
const response = await fetch('/api/admin/farmers/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, status }),
credentials: 'same-origin'
});
const result = await response.json();
if (result.success) {
alert(`Registration ${id} successfully updated to ${status}`);
// Refresh registration table
await fetchAdminRegistrations();
// Refresh revenue metrics
const timeframeSelect = document.getElementById('revenue-timeframe');
const timeframe = timeframeSelect ? timeframeSelect.value : 'all';
await fetchRevenueAnalytics(timeframe);
} else {
alert(`Failed to verify: ${result.error}`);
}
} catch (error) {
console.error('Error verifying registration:', error);
alert('An error occurred during verification.');
}
}
// Cancel registration function
async function cancelRegistration(id) {
if (!confirm(`Are you sure you want to CANCEL registration ${id}? This cannot be undone.`)) {
return;
}
try {
const response = await fetch('/api/admin/farmers/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ id, status: 'Cancelled' }),
credentials: 'same-origin'
});
const result = await response.json();
if (result.success) {
alert(`Registration ${id} has been cancelled.`);
// Refresh registration table
await fetchAdminRegistrations();
// Refresh revenue metrics
const timeframeSelect = document.getElementById('revenue-timeframe');
const timeframe = timeframeSelect ? timeframeSelect.value : 'all';
await fetchRevenueAnalytics(timeframe);
} else {
alert(`Failed to cancel: ${result.error}`);
}
} catch (error) {
console.error('Error cancelling registration:', error);
alert('An error occurred while cancelling the registration.');
}
}
// Delete registration permanently
async function deleteRegistration(id) {
if (!confirm(`⚠️ WARNING: Are you sure you want to PERMANENTLY DELETE registration ${id} from the database? This action is IRREVERSIBLE and will purge the record.`)) {
return;
}
try {
const response = await fetch(`/api/admin/farmers/delete/${id}`, {
method: 'DELETE',
credentials: 'same-origin'
});
const result = await response.json();
if (result.success) {
alert(`Registration ${id} has been permanently deleted.`);
await fetchAdminRegistrations();
} else {
alert(`Failed to delete: ${result.error}`);
}
} catch (error) {
console.error('Error deleting registration:', error);
alert('An error occurred while deleting the registration.');
}
}
// Show admin credentials help modal
function showAdminCredentialsHelp() {
const modal = document.getElementById('admin-recovery-modal');
if (modal) {
modal.classList.remove('hidden');
lucide.createIcons(); // Ensure Lucide icons render in modal
}
}
// Hide admin credentials help modal
function hideAdminCredentialsHelp() {
const modal = document.getElementById('admin-recovery-modal');
if (modal) {
modal.classList.add('hidden');
}
}
// Bind to window for inline onclick handlers
window.verifyRegistration = verifyRegistration;
window.cancelRegistration = cancelRegistration;
window.deleteRegistration = deleteRegistration;
window.showAdminCredentialsHelp = showAdminCredentialsHelp;
window.hideAdminCredentialsHelp = hideAdminCredentialsHelp;