Repository navigation
Expand file tree
/
Copy pathadmin_report.php
More file actions
1021 lines (930 loc) · 59.7 KB
/
Copy pathadmin_report.php
File metadata and controls
1021 lines (930 loc) · 59.7 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'includes/session_config.php';
include 'conn.php';
// Check if logout action is requested
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
// Clear all session variables
$_SESSION = array();
// Destroy the session cookie
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Destroy the session
session_destroy();
// Clear authentication cookies
setcookie('user_id', '', time() - 3600, '/');
setcookie('user_type', '', time() - 3600, '/');
setcookie('remember_me', '', time() - 3600, '/');
setcookie('redirect_after_login', '', time() - 3600, '/');
setcookie('last_page', '', time() - 3600, '/');
// Redirect to admin login page
header("Location: adminlogin.php");
exit();
}
// Check if user is logged in as admin
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true || $_SESSION['user_type'] !== 'admin') {
// Set a cookie to remember the page they were trying to access for 1 day
$one_day = time() + (24 * 60 * 60); // 1 day in seconds
setcookie('redirect_after_login', 'admin_report.php', $one_day, '/');
header("Location: adminlogin.php");
exit();
}
// Check for remember_me cookie to restore session if needed
if (!isset($_SESSION['logged_in']) && isset($_COOKIE['remember_me']) && $_COOKIE['remember_me'] === 'true') {
if (isset($_COOKIE['user_id']) && isset($_COOKIE['user_type']) && $_COOKIE['user_type'] === 'admin') {
// Verify the admin from database
$admin_id = $_COOKIE['user_id'];
$sql = "SELECT * FROM admin WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $admin_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$admin = $result->fetch_assoc();
// Restore session
$_SESSION['user_id'] = $admin['id'];
$_SESSION['user_name'] = $admin['username'];
$_SESSION['user_type'] = 'admin';
$_SESSION['logged_in'] = true;
// Set session to expire in 1 day
ini_set('session.gc_maxlifetime', 86400); // 1 day in seconds
session_set_cookie_params(86400);
}
}
}
// Refresh the cookies for 1 day
if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && $_SESSION['user_type'] === 'admin') {
$one_day = time() + (24 * 60 * 60); // 1 day in seconds
setcookie('user_id', $_SESSION['user_id'], $one_day, '/');
setcookie('user_type', $_SESSION['user_type'], $one_day, '/');
setcookie('remember_me', 'true', $one_day, '/');
}
// Set a cookie for the last visited page for 1 day
setcookie('last_page', 'admin_report', time() + (24 * 60 * 60), '/'); // 1 day
// Get admin info
$admin_id = $_SESSION['user_id'];
$admin_name = $_SESSION['user_name'];
// Determine which view to display
$current_view = isset($_GET['view']) ? $_GET['view'] : 'dashboard';
// Highlight the active sidebar link
$dashboard_active = $current_view === 'dashboard' ? 'text-white bg-indigo-800' : 'text-gray-300 hover:bg-indigo-800';
$users_active = $current_view === 'users' ? 'text-white bg-indigo-800' : 'text-gray-300 hover:bg-indigo-800';
$projects_active = $current_view === 'projects' ? 'text-white bg-indigo-800' : 'text-gray-300 hover:bg-indigo-800';
$payments_active = $current_view === 'payments' ? 'text-white bg-indigo-800' : 'text-gray-300 hover:bg-indigo-800';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard - BidProHub</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body {
font-family: 'Poppins', sans-serif;
background: #f3f4f6;
}
.sidebar {
background: #1e1b4b;
min-height: 100vh;
}
.content-area {
background: #f3f4f6;
}
.stat-card {
background: white;
border-radius: 1rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body class="flex">
<!-- Sidebar -->
<div class="sidebar w-64 fixed h-full">
<div class="p-4">
<div class="flex items-center space-x-2 text-white mb-8">
<span class="text-xl font-bold">BidProHub</span>
</div>
<nav class="space-y-4">
<a href="?view=dashboard" class="flex items-center space-x-3 <?php echo $dashboard_active; ?> p-3 rounded-lg">
<i class="fas fa-chart-line"></i>
<span>Dashboard</span>
</a>
<a href="?view=users" class="flex items-center space-x-3 <?php echo $users_active; ?> p-3 rounded-lg">
<i class="fas fa-users"></i>
<span>Users</span>
</a>
<a href="?view=projects" class="flex items-center space-x-3 <?php echo $projects_active; ?> p-3 rounded-lg">
<i class="fas fa-project-diagram"></i>
<span>Projects</span>
</a>
<a href="?view=payments" class="flex items-center space-x-3 <?php echo $payments_active; ?> p-3 rounded-lg">
<i class="fas fa-money-bill-wave"></i>
<span>Payments</span>
</a>
<a href="?action=logout" class="flex items-center space-x-3 text-gray-300 hover:bg-indigo-800 p-3 rounded-lg mt-8">
<i class="fas fa-sign-out-alt"></i>
<span>Logout</span>
</a>
</nav>
</div>
</div>
<!-- Main Content -->
<div class="content-area flex-1 ml-64">
<!-- Top Bar -->
<div class="bg-white p-4 flex justify-between items-center shadow-sm">
<div class="flex items-center">
<input type="text" placeholder="Search..." class="px-4 py-2 rounded-lg bg-gray-100 focus:outline-none">
</div>
<div class="flex items-center space-x-4">
<button class="p-2 rounded-full hover:bg-gray-100">
<i class="fas fa-bell text-gray-600"></i>
</button>
<span class="text-gray-700">Welcome, <?php echo htmlspecialchars($admin_name); ?></span>
</div>
</div>
<?php if ($current_view === 'dashboard' || $current_view === ''): ?>
<!-- Dashboard Content -->
<div class="p-6">
<!-- Stats Cards -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
<div class="stat-card p-6">
<div class="flex items-center">
<div class="rounded-full p-3 bg-blue-100">
<i class="fas fa-users text-blue-600"></i>
</div>
<div class="ml-4">
<h3 class="text-gray-500 text-sm">Total Users</h3>
<p class="text-2xl font-semibold">
<?php
// Query to get total number of users
$user_count_query = "SELECT COUNT(*) as total_users FROM users";
$user_count_result = $conn->query($user_count_query);
$user_count = 0;
if ($user_count_result && $user_count_result->num_rows > 0) {
$user_count = $user_count_result->fetch_assoc()['total_users'];
}
echo number_format($user_count);
?>
</p>
</div>
</div>
</div>
<div class="stat-card p-6">
<div class="flex items-center">
<div class="rounded-full p-3 bg-green-100">
<i class="fas fa-briefcase text-green-600"></i>
</div>
<div class="ml-4">
<h3 class="text-gray-500 text-sm">Total Projects</h3>
<p class="text-2xl font-semibold">
<?php
// Query to get total number of projects
$project_count_query = "SELECT COUNT(*) as total_projects FROM projects";
$project_count_result = $conn->query($project_count_query);
$project_count = 0;
if ($project_count_result && $project_count_result->num_rows > 0) {
$project_count = $project_count_result->fetch_assoc()['total_projects'];
}
echo number_format($project_count);
?>
</p>
</div>
</div>
</div>
<div class="stat-card p-6">
<div class="flex items-center">
<div class="rounded-full p-3 bg-purple-100">
<i class="fas fa-code text-purple-600"></i>
</div>
<div class="ml-4">
<h3 class="text-gray-500 text-sm">Active Bids</h3>
<p class="text-2xl font-semibold">
<?php
// Query to get active bids (bids on projects with 'open' status)
$active_bids_query = "SELECT COUNT(b.id) as active_bids
FROM bids b
JOIN projects p ON b.project_id = p.id
WHERE p.status = 'open'";
$active_bids_result = $conn->query($active_bids_query);
$active_bids_count = 0;
if ($active_bids_result && $active_bids_result->num_rows > 0) {
$active_bids_count = $active_bids_result->fetch_assoc()['active_bids'];
}
echo number_format($active_bids_count);
?>
</p>
</div>
</div>
</div>
<div class="stat-card p-6">
<div class="flex items-center">
<div class="rounded-full p-3 bg-yellow-100">
<i class="fas fa-dollar-sign text-yellow-600"></i>
</div>
<div class="ml-4">
<h3 class="text-gray-500 text-sm">Revenue</h3>
<p class="text-2xl font-semibold">
<?php
// Query to get total revenue from project_payments
$revenue_query = "SELECT SUM(amount) as total_revenue FROM project_payments";
$revenue_result = $conn->query($revenue_query);
$total_revenue = 0;
if ($revenue_result && $revenue_result->num_rows > 0) {
$total_revenue = $revenue_result->fetch_assoc()['total_revenue'];
// If null (no payments yet), set to 0
$total_revenue = $total_revenue ?: 0;
}
echo '$' . number_format($total_revenue, 2);
?>
</p>
</div>
</div>
</div>
</div>
<!-- Generate Reports Section -->
<div class="bg-white rounded-lg shadow-sm p-6 mb-6">
<h2 class="text-xl font-semibold mb-4">Generate Reports</h2>
<!-- Report Filters -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Date Range</label>
<div class="flex space-x-2">
<input type="date" class="form-input rounded-lg border-gray-300 w-full" id="startDate">
<input type="date" class="form-input rounded-lg border-gray-300 w-full" id="endDate">
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-2">Report Type</label>
<select class="form-select rounded-lg border-gray-300 w-full" id="reportType">
<option value="all">All Reports</option>
<option value="projects">Projects Report</option>
<option value="users">Users Report</option>
<option value="payments">Payments Report</option>
<option value="clients">Clients Report</option>
</select>
</div>
<div class="flex items-end">
<button class="bg-indigo-600 text-white px-6 py-2 rounded-lg hover:bg-indigo-700 w-full" id="generateReportBtn">
Generate Report
</button>
</div>
</div>
<!-- Add this script for date range functionality -->
<script>
document.addEventListener('DOMContentLoaded', function() {
// Set default dates (current month)
const today = new Date();
const firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
document.getElementById('startDate').valueAsDate = firstDay;
document.getElementById('endDate').valueAsDate = today;
// Add event listener to the generate report button
document.getElementById('generateReportBtn').addEventListener('click', function() {
const startDate = document.getElementById('startDate').value;
const endDate = document.getElementById('endDate').value;
const reportType = document.getElementById('reportType').value;
if (!startDate || !endDate) {
alert('Please select both start and end dates');
return;
}
// Send AJAX request to get report data
fetchReportData(startDate, endDate, reportType);
});
function fetchReportData(startDate, endDate, reportType) {
// Show loading indicator
const reportResults = document.querySelector('.space-y-6');
reportResults.innerHTML = '<div class="text-center py-4"><i class="fas fa-spinner fa-spin text-indigo-600 text-2xl"></i><p class="mt-2">Loading report data...</p></div>';
// Create form data
const formData = new FormData();
formData.append('action', 'generate_report');
formData.append('start_date', startDate);
formData.append('end_date', endDate);
formData.append('report_type', reportType);
// Send AJAX request
fetch('admin_report_handler.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update the report sections with the returned data
updateReportSections(data, reportType);
} else {
reportResults.innerHTML = `<div class="text-center py-4 text-red-600">${data.message || 'Error generating report'}</div>`;
}
})
.catch(error => {
console.error('Error:', error);
reportResults.innerHTML = '<div class="text-center py-4 text-red-600">An error occurred while generating the report. Please try again.</div>';
});
}
function updateReportSections(data, reportType) {
const reportResults = document.querySelector('.space-y-6');
let html = '';
// Always show projects summary if it's all reports or projects report
if (reportType === 'all' || reportType === 'projects') {
html += `
<div class="border rounded-lg p-4">
<h3 class="font-medium mb-3">Projects Summary (${data.date_range})</h3>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div class="bg-blue-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Total Projects</p>
<p class="text-xl font-semibold">${data.projects.total}</p>
</div>
<div class="bg-green-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Active Projects</p>
<p class="text-xl font-semibold">${data.projects.active}</p>
</div>
<div class="bg-yellow-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Completed Projects</p>
<p class="text-xl font-semibold">${data.projects.completed}</p>
</div>
<div class="bg-purple-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Average Project Value</p>
<p class="text-xl font-semibold">$${data.projects.avg_value}</p>
</div>
</div>
</div>`;
}
// Show users summary if it's all reports or users report
if (reportType === 'all' || reportType === 'users' || reportType === 'clients') {
html += `
<div class="border rounded-lg p-4">
<h3 class="font-medium mb-3">User Activity (${data.date_range})</h3>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div class="bg-blue-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">New Users</p>
<p class="text-xl font-semibold">${data.users.total}</p>
</div>
<div class="bg-green-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">New Clients</p>
<p class="text-xl font-semibold">${data.users.clients}</p>
</div>
<div class="bg-yellow-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">New Developers</p>
<p class="text-xl font-semibold">${data.users.freelancers}</p>
</div>
<div class="bg-purple-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">User Growth</p>
<p class="text-xl font-semibold">${data.users.growth}%</p>
</div>
</div>
</div>`;
}
// Show payments summary if it's all reports or payments report
if (reportType === 'all' || reportType === 'payments') {
html += `
<div class="border rounded-lg p-4">
<h3 class="font-medium mb-3">Payment Statistics (${data.date_range})</h3>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div class="bg-blue-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Total Transactions</p>
<p class="text-xl font-semibold">${data.payments.total}</p>
</div>
<div class="bg-green-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Revenue</p>
<p class="text-xl font-semibold">$${data.payments.revenue}</p>
</div>
<div class="bg-yellow-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Pending Payments</p>
<p class="text-xl font-semibold">$${data.payments.pending}</p>
</div>
<div class="bg-purple-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Average Transaction</p>
<p class="text-xl font-semibold">$${data.payments.avg_amount}</p>
</div>
</div>
</div>`;
}
reportResults.innerHTML = html;
}
});
</script>
<!-- Report Results -->
<div class="space-y-6">
<!-- Projects Summary -->
<div class="border rounded-lg p-4">
<h3 class="font-medium mb-3">Projects Summary</h3>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<?php
// Get total projects count
$total_projects_query = "SELECT COUNT(*) as count FROM projects";
$total_projects_result = $conn->query($total_projects_query);
$total_projects = $total_projects_result->fetch_assoc()['count'];
// Get active projects count (Open status)
$active_projects_query = "SELECT COUNT(*) as count FROM projects WHERE status = 'Open'";
$active_projects_result = $conn->query($active_projects_query);
$active_projects = $active_projects_result->fetch_assoc()['count'];
// Get completed projects count (Closed status)
$completed_projects_query = "SELECT COUNT(*) as count FROM projects WHERE status = 'Closed'";
$completed_projects_result = $conn->query($completed_projects_query);
$completed_projects = $completed_projects_result->fetch_assoc()['count'];
// Get average project budget
$avg_project_query = "SELECT AVG(budget) as avg_budget FROM projects";
$avg_project_result = $conn->query($avg_project_query);
$avg_project_value = $avg_project_result->fetch_assoc()['avg_budget'];
$avg_project_value = $avg_project_value ? $avg_project_value : 0;
?>
<div class="bg-blue-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Total Projects</p>
<p class="text-xl font-semibold"><?php echo number_format($total_projects); ?></p>
</div>
<div class="bg-green-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Active Projects</p>
<p class="text-xl font-semibold"><?php echo number_format($active_projects); ?></p>
</div>
<div class="bg-yellow-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Completed Projects</p>
<p class="text-xl font-semibold"><?php echo number_format($completed_projects); ?></p>
</div>
<div class="bg-purple-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Average Project Value</p>
<p class="text-xl font-semibold">$<?php echo number_format($avg_project_value, 2); ?></p>
</div>
</div>
</div>
<!-- User Activity -->
<div class="border rounded-lg p-4">
<h3 class="font-medium mb-3">User Activity</h3>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<?php
// Get total users count
$total_users_query = "SELECT COUNT(*) as count FROM users";
$total_users_result = $conn->query($total_users_query);
$total_users = $total_users_result->fetch_assoc()['count'];
// Get active clients count (user_type = 'client')
$active_clients_query = "SELECT COUNT(*) as count FROM users WHERE user_type = 'client'";
$active_clients_result = $conn->query($active_clients_query);
$active_clients = $active_clients_result->fetch_assoc()['count'];
// Get active developers count (user_type = 'freelancer')
$active_devs_query = "SELECT COUNT(*) as count FROM users WHERE user_type = 'freelancer'";
$active_devs_result = $conn->query($active_devs_query);
$active_devs = $active_devs_result->fetch_assoc()['count'];
// Get new users this month
$current_month = date('Y-m-01 00:00:00');
$new_users_query = "SELECT COUNT(*) as count FROM users WHERE created_at >= '$current_month'";
$new_users_result = $conn->query($new_users_query);
$new_users = $new_users_result->fetch_assoc()['count'];
?>
<div class="bg-blue-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Total Users</p>
<p class="text-xl font-semibold"><?php echo number_format($total_users); ?></p>
</div>
<div class="bg-green-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Active Clients</p>
<p class="text-xl font-semibold"><?php echo number_format($active_clients); ?></p>
</div>
<div class="bg-yellow-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Active Developers</p>
<p class="text-xl font-semibold"><?php echo number_format($active_devs); ?></p>
</div>
<div class="bg-purple-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">New Users This Month</p>
<p class="text-xl font-semibold"><?php echo number_format($new_users); ?></p>
</div>
</div>
</div>
<!-- Payment Statistics -->
<div class="border rounded-lg p-4">
<h3 class="font-medium mb-3">Payment Statistics</h3>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<?php
// Get total transactions count
$total_trans_query = "SELECT COUNT(*) as count FROM project_payments";
$total_trans_result = $conn->query($total_trans_query);
$total_transactions = $total_trans_result->fetch_assoc()['count'];
// Get total revenue
$total_revenue_query = "SELECT SUM(amount) as total FROM project_payments WHERE status = 'Completed'";
$total_revenue_result = $conn->query($total_revenue_query);
$total_revenue = $total_revenue_result->fetch_assoc()['total'];
$total_revenue = $total_revenue ? $total_revenue : 0;
// Get pending payments
$pending_payments_query = "SELECT SUM(amount) as total FROM project_payments WHERE status = 'pending'";
$pending_payments_result = $conn->query($pending_payments_query);
$pending_payments = $pending_payments_result->fetch_assoc()['total'];
$pending_payments = $pending_payments ? $pending_payments : 0;
// Get average transaction amount
$avg_trans_query = "SELECT AVG(amount) as avg_amount FROM project_payments WHERE status = 'Completed'";
$avg_trans_result = $conn->query($avg_trans_query);
$avg_transaction = $avg_trans_result->fetch_assoc()['avg_amount'];
$avg_transaction = $avg_transaction ? $avg_transaction : 0;
?>
<div class="bg-blue-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Total Transactions</p>
<p class="text-xl font-semibold"><?php echo number_format($total_transactions); ?></p>
</div>
<div class="bg-green-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Total Revenue</p>
<p class="text-xl font-semibold">$<?php echo number_format($total_revenue, 2); ?></p>
</div>
<div class="bg-yellow-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Pending Payments</p>
<p class="text-xl font-semibold">$<?php echo number_format($pending_payments, 2); ?></p>
</div>
<div class="bg-purple-50 p-3 rounded-lg">
<p class="text-sm text-gray-600">Average Transaction</p>
<p class="text-xl font-semibold">$<?php echo number_format($avg_transaction, 2); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php elseif ($current_view === 'users'): ?>
<!-- Users View -->
<div class="p-6">
<div class="bg-white rounded-lg shadow-sm p-6">
<div class="overflow-x-auto">
<table class="min-w-full bg-white">
<thead>
<tr class="text-left text-gray-500 border-b">
<th class="py-3 px-4">ID</th>
<th class="py-3 px-4">Name</th>
<th class="py-3 px-4">Email</th>
<th class="py-3 px-4">User Type</th>
<th class="py-3 px-4">Joined Date</th>
<th class="py-3 px-4">Actions</th>
</tr>
</thead>
<tbody>
<?php
// Query to get all users
$users_query = "SELECT * FROM users ORDER BY user_id DESC";
$users_result = $conn->query($users_query);
if ($users_result && $users_result->num_rows > 0) {
while ($user = $users_result->fetch_assoc()) {
echo '<tr class="border-b hover:bg-gray-50">';
echo '<td class="py-3 px-4">' . htmlspecialchars($user['user_id']) . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($user['name']) . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($user['email']) . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($user['user_type']) . '</td>';
// Format date if it exists
$joined_date = isset($user['created_at']) ? date('M d, Y', strtotime($user['created_at'])) : 'N/A';
echo '<td class="py-3 px-4">' . $joined_date . '</td>';
echo '<td class="py-3 px-4">';
echo '<div class="flex justify-center"><button class="text-blue-600 hover:text-blue-800" onclick="editUser(' . $user['user_id'] . ', \'' . htmlspecialchars($user['name'], ENT_QUOTES) . '\', \'' . htmlspecialchars($user['email'], ENT_QUOTES) . '\', \'' . htmlspecialchars($user['user_type'], ENT_QUOTES) . '\')"><i class="fas fa-edit"></i></button></div>';
echo '</td>';
echo '</tr>';
}
} else {
echo '<tr><td colspan="6" class="py-4 text-center">No users found</td></tr>';
}
?>
</tbody>
</table>
</div>
<script>
function editUser(userId, name, email, userType) {
// Set form values
document.getElementById('edit_user_id').value = userId;
document.getElementById('edit_name').value = name;
document.getElementById('edit_email').value = email;
document.getElementById('edit_user_type').value = userType;
// Show the modal
const modal = document.getElementById('editUserModal');
modal.classList.remove('hidden');
}
function closeEditModal() {
// Hide the modal
const modal = document.getElementById('editUserModal');
modal.classList.add('hidden');
}
</script>
<!-- Edit User Modal -->
<div id="editUserModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden flex items-center justify-center z-50">
<div class="bg-white rounded-lg shadow-lg p-6 w-full max-w-md">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-medium">Edit User</h3>
<button onclick="closeEditModal()" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times"></i>
</button>
</div>
<form action="admin_edit.php" method="POST">
<input type="hidden" id="edit_user_id" name="user_id">
<div class="mb-4">
<label for="edit_name" class="block text-sm font-medium text-gray-700 mb-1">Name</label>
<input type="text" id="edit_name" name="name" class="w-full px-3 py-2 border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="edit_email" class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" id="edit_email" name="email" class="w-full px-3 py-2 border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="edit_user_type" class="block text-sm font-medium text-gray-700 mb-1">User Type</label>
<select id="edit_user_type" name="user_type" class="w-full px-3 py-2 border border-gray-300 rounded-md">
<option value="client">Client</option>
<option value="freelancer">Freelancer</option>
</select>
</div>
<div class="flex justify-end space-x-3">
<button type="button" onclick="closeEditModal()" class="px-4 py-2 border border-gray-300 rounded-md text-gray-700 hover:bg-gray-50">
Cancel
</button>
<button type="submit" class="px-4 py-2 bg-indigo-600 text-white rounded-md hover:bg-indigo-700">
Save Changes
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php elseif ($current_view === 'projects'): ?>
<!-- Projects View -->
<div class="p-6">
<div class="bg-white rounded-lg shadow-sm p-6">
<div class="flex justify-between items-center mb-6">
<h2 class="text-xl font-semibold">All Projects</h2>
<div>
<button class="bg-indigo-600 text-white px-4 py-2 rounded-lg hover:bg-indigo-700">
<i class="fas fa-plus mr-2"></i> Add New Project
</button>
</div>
</div>
<div class="overflow-x-auto">
<table class="min-w-full bg-white">
<thead>
<tr class="text-left text-gray-500 border-b">
<th class="py-3 px-4">ID</th>
<th class="py-3 px-4">Project Title</th>
<th class="py-3 px-4">Client</th>
<th class="py-3 px-4">Budget</th>
<th class="py-3 px-4">Status</th>
<th class="py-3 px-4">Created Date</th>
<th class="py-3 px-4">Actions</th>
</tr>
</thead>
<tbody>
<?php
// Query to get all projects
$projects_query = "SELECT p.*, u.name as client_name
FROM projects p
LEFT JOIN users u ON p.user_id = u.user_id
ORDER BY p.id DESC";
$projects_result = $conn->query($projects_query);
if ($projects_result && $projects_result->num_rows > 0) {
while ($project = $projects_result->fetch_assoc()) {
// Determine status class
$status_class = '';
switch($project['status']) {
case 'Open':
$status_class = 'bg-green-100 text-green-800';
break;
case 'Closed':
$status_class = 'bg-red-100 text-red-800';
break;
case 'On Progress':
$status_class = 'bg-blue-100 text-blue-800';
break;
default:
$status_class = 'bg-gray-100 text-gray-800';
}
echo '<tr class="border-b hover:bg-gray-50">';
echo '<td class="py-3 px-4">' . htmlspecialchars($project['id']) . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($project['title']) . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($project['client_name'] ?? 'N/A') . '</td>';
echo '<td class="py-3 px-4">$' . number_format($project['budget'], 2) . '</td>';
echo '<td class="py-3 px-4"><span class="px-2 py-1 rounded-full text-xs ' . $status_class . '">' . htmlspecialchars($project['status']) . '</span></td>';
// Format date if it exists
$created_date = isset($project['created_at']) ? date('M d, Y', strtotime($project['created_at'])) : 'N/A';
echo '<td class="py-3 px-4">' . $created_date . '</td>';
echo '<td class="py-3 px-4">';
echo '<div class="flex justify-center"><button class="text-blue-600 hover:text-blue-800" onclick="editProject(' . $project['id'] . ', \'' . htmlspecialchars($project['title'], ENT_QUOTES) . '\', ' . $project['budget'] . ', \'' . htmlspecialchars($project['status'], ENT_QUOTES) . '\')"><i class="fas fa-edit"></i></button></div>';
echo '</td>';
echo '</tr>';
}
} else {
echo '<tr><td colspan="7" class="py-4 text-center">No projects found</td></tr>';
}
?>
</tbody>
</table>
</div>
<!-- Edit Project Modal -->
<div id="editProjectModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden flex items-center justify-center">
<div class="bg-white rounded-lg shadow-lg p-6 w-full max-w-md">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">Edit Project</h3>
<button onclick="closeEditProjectModal()" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times"></i>
</button>
</div>
<form id="editProjectForm" method="post" action="admin_edit.php">
<input type="hidden" id="edit_project_id" name="project_id">
<div class="mb-4">
<label for="edit_title" class="block text-sm font-medium text-gray-700 mb-2">Project Title</label>
<input type="text" id="edit_title" name="title" class="w-full px-3 py-2 border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="edit_budget" class="block text-sm font-medium text-gray-700 mb-2">Budget</label>
<input type="number" step="0.01" id="edit_budget" name="budget" class="w-full px-3 py-2 border border-gray-300 rounded-md">
</div>
<div class="mb-4">
<label for="edit_status" class="block text-sm font-medium text-gray-700 mb-2">Status</label>
<select id="edit_status" name="status" class="w-full px-3 py-2 border border-gray-300 rounded-md">
<option value="Open">Open</option>
<option value="On Progress">On Progress</option>
<option value="Closed">Closed</option>
</select>
</div>
<div class="flex justify-end">
<button type="button" onclick="closeEditProjectModal()" class="bg-gray-300 text-gray-700 px-4 py-2 rounded-md mr-2">Cancel</button>
<button type="submit" class="bg-indigo-600 text-white px-4 py-2 rounded-md">Save Changes</button>
</div>
</form>
</div>
</div>
<!-- JavaScript for Edit Project Modal -->
<script>
function editProject(projectId, title, budget, status) {
document.getElementById('edit_project_id').value = projectId;
document.getElementById('edit_title').value = title;
document.getElementById('edit_budget').value = budget;
document.getElementById('edit_status').value = status;
document.getElementById('editProjectModal').classList.remove('hidden');
}
function closeEditProjectModal() {
document.getElementById('editProjectModal').classList.add('hidden');
}
</script>
</div>
</div>
<?php elseif ($current_view === 'payments'): ?>
<!-- Payments View -->
<div class="p-6">
<div class="bg-white rounded-lg shadow-sm p-6">
<div class="flex justify-between items-center mb-6">
<h2 class="text-xl font-semibold">All Payments</h2>
<div>
<button class="bg-indigo-600 text-white px-4 py-2 rounded-lg hover:bg-indigo-700">
<i class="fas fa-plus mr-2"></i> Record Payment
</button>
</div>
</div>
<div class="overflow-x-auto">
<table class="min-w-full bg-white">
<thead>
<tr class="text-left text-gray-500 border-b">
<th class="py-3 px-4">ID</th>
<th class="py-3 px-4">Project</th>
<th class="py-3 px-4">Client</th>
<th class="py-3 px-4">Freelancer</th>
<th class="py-3 px-4">Amount</th>
<th class="py-3 px-4">Payment Date</th>
<th class="py-3 px-4">Status</th>
<th class="py-3 px-4 text-center">Actions</th>
</tr>
</thead>
<tbody>
<?php
// Query to get all payments with project details and user names
$payments_query = "SELECT pp.*, p.title as project_title,
c.name as client_name, f.name as freelancer_name
FROM project_payments pp
LEFT JOIN projects p ON pp.project_id = p.id
LEFT JOIN users c ON pp.client_id = c.user_id
LEFT JOIN users f ON pp.freelancer_id = f.user_id
ORDER BY pp.payment_date DESC";
$payments_result = $conn->query($payments_query);
if ($payments_result && $payments_result->num_rows > 0) {
while ($payment = $payments_result->fetch_assoc()) {
// Determine status class
$status_class = '';
switch(strtolower($payment['status'])) {
case 'completed':
$status_class = 'bg-green-100 text-green-800';
break;
case 'pending':
$status_class = 'bg-yellow-100 text-yellow-800';
break;
case 'failed':
$status_class = 'bg-red-100 text-red-800';
break;
default:
$status_class = 'bg-gray-100 text-gray-800';
}
echo '<tr class="border-b hover:bg-gray-50">';
echo '<td class="py-3 px-4">' . htmlspecialchars($payment['payment_id']) . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($payment['project_title'] ?? 'N/A') . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($payment['client_name'] ?? 'N/A') . '</td>';
echo '<td class="py-3 px-4">' . htmlspecialchars($payment['freelancer_name'] ?? 'N/A') . '</td>';
echo '<td class="py-3 px-4">$' . number_format($payment['amount'], 2) . '</td>';
// Format payment date
$payment_date = isset($payment['payment_date']) ? date('M d, Y', strtotime($payment['payment_date'])) : 'N/A';
echo '<td class="py-3 px-4">' . $payment_date . '</td>';
echo '<td class="py-3 px-4"><span class="px-2 py-1 rounded-full text-xs ' . $status_class . '">' . ucfirst($payment['status'] ?? 'Unknown') . '</span></td>';
echo '<td class="py-3 px-4 text-center">';
echo '<div class="relative inline-block text-left">';
echo '<button onclick="toggleDropdown(' . $payment['payment_id'] . ')" class="text-blue-600 hover:text-blue-800 mr-3"><i class="fas fa-edit"></i></button>';
echo '<div id="dropdown-' . $payment['payment_id'] . '" class="hidden absolute right-0 mt-2 w-48 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 z-10">';
echo '<div class="py-1" role="menu" aria-orientation="vertical" aria-labelledby="options-menu">';
// Only show release payment option if status is not already completed
if (strtolower($payment['status']) !== 'completed' && strtolower($payment['status']) !== 'rejected') {
echo '<a href="#" onclick="releasePayment(' . $payment['payment_id'] . '); return false;" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem">Release Payment</a>';
echo '<a href="#" onclick="rejectPayment(' . $payment['payment_id'] . '); return false;" class="block px-4 py-2 text-sm text-red-600 hover:bg-gray-100" role="menuitem">Reject Payment</a>';
}
echo '</div>';
echo '</div>';
echo '</div>';
echo '</td>';
echo '</tr>';
}
} else {
echo '<tr><td colspan="8" class="py-4 text-center">No payment records found</td></tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<script>
// Toggle dropdown menu for payment actions
function toggleDropdown(paymentId) {
const dropdown = document.getElementById(`dropdown-${paymentId}`);
dropdown.classList.toggle('hidden');
}
// Close all dropdowns when clicking outside
document.addEventListener('click', function(event) {
if (!event.target.closest('button')) {
const dropdowns = document.querySelectorAll('[id^="dropdown-"]');
dropdowns.forEach(dropdown => {
dropdown.classList.add('hidden');
});
}
});
// Function to release payment
function releasePayment(paymentId) {
if (confirm('Are you sure you want to release this payment?')) {
const formData = new FormData();
formData.append('payment_id', paymentId);
formData.append('action', 'release');
fetch('update_payment_status.php', {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data.success) {
// Update UI to show success
const row = document.querySelector(`tr[data-payment-id="${paymentId}"]`);
if (row) {
const statusCell = row.querySelector('.payment-status');
const actionCell = row.querySelector('.payment-actions');
statusCell.innerHTML = '<span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">Completed</span>';
actionCell.innerHTML = '<button class="text-gray-600 hover:text-gray-900">View Details</button>';
}
alert('Payment released successfully!');
} else {
alert(data.message || 'Failed to release payment');
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while processing your request.');
});
}
}
// Function to reject payment
function rejectPayment(paymentId) {
if (confirm('Are you sure you want to reject this payment?')) {
// Send AJAX request to update payment status
fetch('update_payment_status.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',