Spaces:
Configuration error

File size: 11,386 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
```php
<?php
include 'includes/auth.php';
redirectIfNotAdmin();
include 'config/database.php';

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

// Handle storage location actions
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['add_location'])) {
        $name = $_POST['name'];
        $address = $_POST['address'];
        $contactInfo = $_POST['contactInfo'];
        
        $query = "INSERT INTO storage_locations (name, address, contactInfo) VALUES (:name, :address, :contactInfo)";
        $stmt = $connection->prepare($query);
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':address', $address);
        $stmt->bindParam(':contactInfo', $contactInfo);
        
        if ($stmt->execute()) {
            $success = "Storage location added successfully!";
        } else {
            $error = "Error adding storage location.";
        }
    } elseif (isset($_POST['delete_location'])) {
        $locationID = $_POST['locationID'];
        
        // Check if location has items
        $checkQuery = "SELECT COUNT(*) as itemCount FROM items WHERE storageLocationID = :locationID";
        $checkStmt = $connection->prepare($checkQuery);
        $checkStmt->bindParam(':locationID', $locationID);
        $checkStmt->execute();
        $result = $checkStmt->fetch(PDO::FETCH_ASSOC);
        
        if ($result['itemCount'] > 0) {
            $error = "Cannot delete storage location that has items assigned. Reassign items first.";
        } else {
            $deleteQuery = "DELETE FROM storage_locations WHERE locationID = :locationID";
            $deleteStmt = $connection->prepare($deleteQuery);
            $deleteStmt->bindParam(':locationID', $locationID);
            
            if ($deleteStmt->execute()) {
                $success = "Storage location deleted successfully!";
            } else {
                $error = "Error deleting storage location.";
            }
        }
    }
}

// Get all storage locations
$locationsQuery = "SELECT * FROM storage_locations ORDER BY name";
$locationsStmt = $connection->prepare($locationsQuery);
$locationsStmt->execute();
$storageLocations = $locationsStmt->fetchAll(PDO::FETCH_ASSOC);

// Get items by storage location
$itemsQuery = "
    SELECT i.*, sl.name as storageName, r.locationFound, u.name as finderName
    FROM items i
    LEFT JOIN storage_locations sl ON i.storageLocationID = sl.locationID
    LEFT JOIN reports r ON i.reportID = r.reportID
    LEFT JOIN users u ON r.userId = u.userId
    WHERE i.storageLocationID IS NOT NULL
    ORDER BY sl.name, i.createdAt DESC
";
$itemsStmt = $connection->prepare($itemsQuery);
$itemsStmt->execute();
$storedItems = $itemsStmt->fetchAll(PDO::FETCH_ASSOC);

// Group items by storage location
$itemsByLocation = [];
foreach ($storedItems as $item) {
    $locationID = $item['storageLocationID'];
    if (!isset($itemsByLocation[$locationID])) {
        $itemsByLocation[$locationID] = [
            'location' => $item['storageName'],
            'items' => []
        ];
    }
    $itemsByLocation[$locationID]['items'][] = $item;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Storage Management - 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>Storage Management</h1>
                <p>Manage storage locations and view stored items</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; ?>
            
            <!-- Add Storage Location Form -->
            <div class="mt-3">
                <h3>Add New Storage Location</h3>
                <form method="POST" class="row">
                    <div class="col-4">
                        <div class="form-group">
                            <input type="text" name="name" class="form-control" placeholder="Location Name" required>
                        </div>
                    </div>
                    <div class="col-4">
                        <div class="form-group">
                            <input type="text" name="address" class="form-control" placeholder="Address" required>
                        </div>
                    </div>
                    <div class="col-3">
                        <div class="form-group">
                            <input type="text" name="contactInfo" class="form-control" placeholder="Contact Info">
                        </div>
                    </div>
                    <div class="col-1">
                        <button type="submit" name="add_location" class="btn btn-success w-full">Add</button>
                    </div>
                </form>
            </div>
            
            <!-- Storage Locations List -->
            <div class="mt-4">
                <h3>Storage Locations</h3>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Address</th>
                                <th>Contact Info</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($storageLocations as $location): ?>
                                <tr>
                                    <td><?php echo $location['name']; ?></td>
                                    <td><?php echo $location['address']; ?></td>
                                    <td><?php echo $location['contactInfo']; ?></td>
                                    <td>
                                        <form method="POST" style="display: inline;">
                                            <input type="hidden" name="locationID" value="<?php echo $location['locationID']; ?>">
                                            <button type="submit" name="delete_location" class="btn btn-danger btn-sm" 
                                                    onclick="return confirm('Are you sure you want to delete this storage location?')">
                                                Delete
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
            
            <!-- Items by Storage Location -->
            <div class="mt-4">
                <h3>Stored Items by Location</h3>
                <?php if (empty($itemsByLocation)): ?>
                    <p>No items currently stored in any location.</p>
                <?php else: ?>
                    <?php foreach ($itemsByLocation as $locationID => $data): ?>
                        <div class="card mt-3">
                            <h4><?php echo $data['location']; ?></h4>
                            <div class="row">
                                <?php foreach ($data['items'] as $item): ?>
                                    <div class="col-4">
                                        <div class="card">
                                            <?php if ($item['imagePath']): ?>
                                                <img src="<?php echo $item['imagePath']; ?>" alt="<?php echo $item['description']; ?>" 
                                                     style="width: 100%; height: 150px; object-fit: cover; border-radius: 5px;">
                                            <?php else: ?>
                                                <div style="width: 100%; height: 150px; background: #e9ecef; display: flex; align-items: center; justify-content: center; border-radius: 5px;">
                                                    <span>No Image</span>
                                                </div>
                                            <?php endif; ?>
                                            <h5 class="mt-2"><?php echo $item['category']; ?></h5>
                                            <p><?php echo substr($item['description'], 0, 100); ?>...</p>
                                            <p><small>Found by: <?php echo $item['finderName']; ?></small></p>
                                            <p><small>Location: <?php echo $item['locationFound']; ?></small></p>
                                            <p>
                                                <span class="badge badge-<?php 
                                                    switch($item['status']) {
                                                        case 'available': echo 'success'; break;
                                                        case 'claimed': echo 'warning'; break;
                                                        case 'returned': echo 'info'; break;
                                                        default: echo 'secondary';
                                                    }
                                                ?>">
                                                    <?php echo ucfirst($item['status']); ?>
                                                </span>
                                            </p>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </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>
```