Spaces:
Configuration error

File size: 13,677 Bytes
8df07b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```php
<?php
include 'includes/auth.php';
redirectIfNotAdmin();
include 'config/database.php';

$db = new Database();
$connection = $db->getConnection();

// Get system statistics
$statsQueries = [
    'total_users' => "SELECT COUNT(*) as count FROM users",
    'total_finders' => "SELECT COUNT(*) as count FROM users WHERE role = 'finder'",
    'total_owners' => "SELECT COUNT(*) as count FROM users WHERE role = 'owner'",
    'total_reports' => "SELECT COUNT(*) as count FROM reports",
    'verified_reports' => "SELECT COUNT(*) as count FROM reports WHERE status = 'verified'",
    'pending_verifications' => "SELECT COUNT(*) as count FROM verifications WHERE result = 'pending'",
    'total_claims' => "SELECT COUNT(*) as count FROM claims",
    'approved_claims' => "SELECT COUNT(*) as count FROM claims WHERE status = 'approved'",
    'total_items' => "SELECT COUNT(*) as count FROM items",
    'available_items' => "SELECT COUNT(*) as count FROM items WHERE status = 'available'"
];

$stats = [];
foreach ($statsQueries as $key => $query) {
    $stmt = $connection->prepare($query);
    $stmt->execute();
    $stats[$key] = $stmt->fetch(PDO::FETCH_ASSOC)['count'];
}

// Get recent users
$usersQuery = "SELECT * FROM users ORDER BY createdAt DESC LIMIT 5";
$usersStmt = $connection->prepare($usersQuery);
$usersStmt->execute();
$recentUsers = $usersStmt->fetchAll(PDO::FETCH_ASSOC);

// Get recent reports
$reportsQuery = "
    SELECT r.*, u.name as userName 
    FROM reports r 
    JOIN users u ON r.userId = u.userId 
    ORDER BY r.timestamp DESC LIMIT 5
";
$reportsStmt = $connection->prepare($reportsQuery);
$reportsStmt->execute();
$recentReports = $reportsStmt->fetchAll(PDO::FETCH_ASSOC);

// Handle user management actions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['delete_user'])) {
        $userId = $_POST['user_id'];
        
        // Check if user has reports or claims
        $checkQuery = "
            SELECT (SELECT COUNT(*) FROM reports WHERE userId = :userId) as reportCount,
                   (SELECT COUNT(*) FROM claims WHERE ownerUserId = :userId) as claimCount
        ";
        $checkStmt = $connection->prepare($checkQuery);
        $checkStmt->bindParam(':userId', $userId);
        $checkStmt->execute();
        $result = $checkStmt->fetch(PDO::FETCH_ASSOC);
        
        if ($result['reportCount'] > 0 || $result['claimCount'] > 0) {
            $error = "Cannot delete user with existing reports or claims.";
        } else {
            $deleteQuery = "DELETE FROM users WHERE userId = :userId";
            $deleteStmt = $connection->prepare($deleteQuery);
            $deleteStmt->bindParam(':userId', $userId);
            
            if ($deleteStmt->execute()) {
                $success = "User deleted successfully!";
            } else {
                $error = "Error deleting user.";
            }
        }
    }
}

// Get all users for management
$allUsersQuery = "SELECT * FROM users ORDER BY createdAt DESC";
$allUsersStmt = $connection->prepare($allUsersQuery);
$allUsersStmt->execute();
$allUsers = $allUsersStmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel - Lost & Found</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <div class="container">
            <nav>
                <div class="logo">Lost & Found Pro</div>
                <ul class="nav-links">
                    <li><a href="home.php">Home</a></li>
                    <li><a href="dashboard.php">Dashboard</a></li>
                    <li><a href="admin.php">Admin</a></li>
                    <li><a href="verifications.php">Verifications</a></li>
                    <li><a href="storage.php">Storage</a></li>
                    <li><a href="notifications.php">Notifications</a></li>
                    <li><a href="profile.php">Profile</a></li>
                    <li><a href="logout.php">Logout</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <main class="container">
        <div class="card">
            <div class="card-header">
                <h1>Admin Dashboard</h1>
                <p>System overview and management</p>
            </div>
            
            <?php if (isset($success)): ?>
                <div style="background: #D1FAE5; color: #065F46; padding: 1rem; border-radius: 5px; margin-bottom: 1rem;">
                    <?php echo $success; ?>
                </div>
            <?php endif; ?>
            
            <?php if (isset($error)): ?>
                <div style="background: #FEE2E2; color: #DC2626; padding: 1rem; border-radius: 5px; margin-bottom: 1rem;">
                    <?php echo $error; ?>
                </div>
            <?php endif; ?>
            
            <!-- Statistics Overview -->
            <div class="row mt-3">
                <div class="col-3">
                    <div class="card text-center">
                        <h3><?php echo $stats['total_users']; ?></h3>
                        <p>Total Users</p>
                        <small><?php echo $stats['total_finders']; ?> Finders, <?php echo $stats['total_owners']; ?> Owners</small>
                    </div>
                </div>
                <div class="col-3">
                    <div class="card text-center">
                        <h3><?php echo $stats['total_reports']; ?></h3>
                        <p>Total Reports</p>
                        <small><?php echo $stats['verified_reports']; ?> Verified</small>
                    </div>
                </div>
                <div class="col-3">
                    <div class="card text-center">
                        <h3><?php echo $stats['total_claims']; ?></h3>
                        <p>Total Claims</p>
                        <small><?php echo $stats['approved_claims']; ?> Approved</small>
                    </div>
                </div>
                <div class="col-3">
                    <div class="card text-center">
                        <h3><?php echo $stats['total_items']; ?></h3>
                        <p>Total Items</p>
                        <small><?php echo $stats['available_items']; ?> Available</small>
                    </div>
                </div>
            </div>
            
            <!-- Recent Activity -->
            <div class="row mt-4">
                <div class="col-6">
                    <div class="card">
                        <h3>Recent Users</h3>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Email</th>
                                        <th>Role</th>
                                        <th>Joined</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($recentUsers as $user): ?>
                                        <tr>
                                            <td><?php echo $user['name']; ?></td>
                                            <td><?php echo $user['email']; ?></td>
                                            <td>
                                                <span class="badge badge-<?php echo $user['role'] == 'finder' ? 'info' : 'success'; ?>">
                                                    <?php echo ucfirst($user['role']); ?>
                                                </span>
                                            </td>
                                            <td><?php echo date('M j, Y', strtotime($user['createdAt'])); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <div class="col-6">
                    <div class="card">
                        <h3>Recent Reports</h3>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Report ID</th>
                                        <th>User</th>
                                        <th>Location</th>
                                        <th>Status</th>
                                        <th>Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($recentReports as $report): ?>
                                        <tr>
                                            <td>#<?php echo $report['reportID']; ?></td>
                                            <td><?php echo $report['userName']; ?></td>
                                            <td><?php echo substr($report['locationFound'], 0, 20); ?>...</td>
                                            <td>
                                                <span class="badge badge-<?php 
                                                    switch($report['status']) {
                                                        case 'verified': echo 'success'; break;
                                                        case 'rejected': echo 'danger'; break;
                                                        default: echo 'warning';
                                                    }
                                                ?>">
                                                    <?php echo ucfirst(str_replace('_', ' ', $report['status'])); ?>
                                                </span>
                                            </td>
                                            <td><?php echo date('M j', strtotime($report['timestamp'])); ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- User Management -->
            <div class="mt-4">
                <h3>User Management</h3>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Role</th>
                                <th>Joined</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($allUsers as $user): ?>
                                <tr>
                                    <td><?php echo $user['userId']; ?></td>
                                    <td><?php echo $user['name']; ?></td>
                                    <td><?php echo $user['email']; ?></td>
                                    <td>
                                        <span class="badge badge-<?php echo $user['role'] == 'finder' ? 'info' : 'success'; ?>">
                                            <?php echo ucfirst($user['role']); ?>
                                        </span>
                                    </td>
                                    <td><?php echo date('M j, Y', strtotime($user['createdAt'])); ?></td>
                                    <td>
                                        <form method="POST" style="display: inline;">
                                            <input type="hidden" name="user_id" value="<?php echo $user['userId']; ?>">
                                            <button type="submit" name="delete_user" class="btn btn-danger btn-sm"
                                                    onclick="return confirm('Are you sure you want to delete this user? This action cannot be undone.')">
                                                Delete
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            
            <!-- Quick Actions -->
            <div class="mt-4">
                <h3>Quick Actions</h3>
                <div class="d-flex gap-2">
                    <a href="verifications.php" class="btn btn-primary">Manage Verifications</a>
                    <a href="storage.php" class="btn btn-success">Manage Storage</a>
                    <a href="notifications.php" class="btn btn-secondary">View Notifications</a>
                </div>
            </div>
        </div>
    </main>

    <footer>
        <div class="container">
            <div class="footer-content">
                <div class="logo">Lost & Found Pro</div>
                <p>&copy; 2024 Lost & Found Management System. All rights reserved.</p>
            </div>
        </div>
    </footer>
</body>
</html>
```