diff --git a/public/dashboard.html b/public/dashboard.html index a4b0b1e..232dc84 100644 --- a/public/dashboard.html +++ b/public/dashboard.html @@ -92,6 +92,7 @@ input[type="text"], input[type="number"], + input[type="datetime-local"], textarea, select { width: 100%; @@ -287,9 +288,14 @@ +
+ + +
+
- +
@@ -343,6 +349,7 @@ + @@ -552,6 +559,7 @@ window.closeAddModal = closeAddModal; window.closeEditModal = closeEditModal; window.openEditModal = openEditModal; + window.deleteStation = deleteStation; async function loadStations() { try { @@ -644,6 +652,11 @@ selectedStation = station; document.getElementById('updateModal').style.display = 'block'; + // Set current date/time as default + const now = new Date(); + const localDateTime = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString().slice(0, 16); + document.getElementById('statusAsOf').value = localDateTime; + // Center map on selected station map.setView([station.latitude, station.longitude], 16); } @@ -698,6 +711,34 @@ document.getElementById('edit-message').innerHTML = ''; } + function deleteStation() { + if (!selectedStation) { + showMessage('edit-message', 'No station selected', 'error'); + return; + } + + if (confirm(`Are you sure you want to delete "${selectedStation.name}"? This action cannot be undone.`)) { + fetch(`/api/stations/${selectedStation.id}`, { + method: 'DELETE' + }) + .then(response => response.json()) + .then(result => { + if (result.success) { + showMessage('edit-message', 'Station deleted successfully!'); + setTimeout(() => { + closeEditModal(); + loadStations(); + }, 1500); + } else { + showMessage('edit-message', result.error || 'Failed to delete station', 'error'); + } + }) + .catch(error => { + showMessage('edit-message', 'Failed to delete station', 'error'); + }); + } + } + // Close modal when clicking outside window.onclick = function(event) { const updateModal = document.getElementById('updateModal'); @@ -803,7 +844,8 @@ const formData = new FormData(e.target); const data = { description: formData.get('description'), - estimatedHours: formData.get('estimatedHours') + estimatedHours: formData.get('estimatedHours'), + statusAsOf: formData.get('statusAsOf') }; try { diff --git a/server.js b/server.js index a08ed67..9ff6e23 100644 --- a/server.js +++ b/server.js @@ -396,15 +396,18 @@ app.post('/api/stations/:id/update', (req, res) => { return res.status(401).json({ error: 'Authentication required' }); } - const { description, estimatedHours } = req.body; + const { description, estimatedHours, statusAsOf } = req.body; const stationId = req.params.id; - const estimatedEmptyTime = new Date(); + // Use provided status date/time or current time + const refillTime = statusAsOf ? new Date(statusAsOf) : new Date(); + + const estimatedEmptyTime = new Date(refillTime); estimatedEmptyTime.setHours(estimatedEmptyTime.getHours() + parseInt(estimatedHours)); console.log("Updated by",req.user); - db.run('INSERT INTO station_updates (station_id, description, estimated_empty_time, updated_by) VALUES (?, ?, ?, ?)', - [stationId, description, estimatedEmptyTime.toISOString(), req.user.id], + db.run('INSERT INTO station_updates (station_id, description, last_refill_time, estimated_empty_time, updated_by) VALUES (?, ?, ?, ?, ?)', + [stationId, description, refillTime.toISOString(), estimatedEmptyTime.toISOString(), req.user.id], function(err) { if (err) return res.status(500).json({ error: 'Database error' }); res.json({ success: true }); @@ -429,6 +432,28 @@ app.put('/api/stations/:id', (req, res) => { ); }); +app.delete('/api/stations/:id', (req, res) => { + if (!req.user) { + return res.status(401).json({ error: 'Authentication required' }); + } + + const stationId = req.params.id; + + // Delete station updates first (foreign key constraint) + db.run('DELETE FROM station_updates WHERE station_id = ?', [stationId], function(err) { + if (err) return res.status(500).json({ error: 'Database error' }); + + // Then delete the station + db.run('DELETE FROM water_stations WHERE id = ?', [stationId], function(err) { + if (err) return res.status(500).json({ error: 'Database error' }); + if (this.changes === 0) { + return res.status(404).json({ error: 'Station not found' }); + } + res.json({ success: true }); + }); + }); +}); + app.get('/dashboard', (req, res) => { if (!req.user) { return res.redirect('/login');