764 lines
25 KiB
HTML
764 lines
25 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Dashboard - Water Station Tracker</title>
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background-color: #f5f5f5;
|
|
}
|
|
|
|
.header {
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
padding: 1rem;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 1.3rem;
|
|
}
|
|
|
|
.header-right {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 1rem;
|
|
}
|
|
|
|
.city-selector {
|
|
background: rgba(255,255,255,0.2);
|
|
color: white;
|
|
border: 1px solid rgba(255,255,255,0.3);
|
|
padding: 0.5rem 1rem;
|
|
border-radius: 20px;
|
|
font-size: 0.9rem;
|
|
cursor: pointer;
|
|
text-decoration: none;
|
|
}
|
|
|
|
.city-selector:hover {
|
|
background: rgba(255,255,255,0.3);
|
|
}
|
|
|
|
.user-info {
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.logout-btn {
|
|
background: rgba(255,255,255,0.2);
|
|
color: white;
|
|
border: 1px solid rgba(255,255,255,0.3);
|
|
padding: 0.5rem 1rem;
|
|
border-radius: 20px;
|
|
cursor: pointer;
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.main-content {
|
|
display: flex;
|
|
height: calc(100vh - 80px);
|
|
}
|
|
|
|
.sidebar {
|
|
width: 100%;
|
|
max-width: 400px;
|
|
background: white;
|
|
border-right: 1px solid #e1e1e1;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.map-container {
|
|
flex: 1;
|
|
position: relative;
|
|
}
|
|
|
|
#map {
|
|
height: 100%;
|
|
width: 100%;
|
|
}
|
|
|
|
.tab-buttons {
|
|
display: flex;
|
|
background: #f8f9fa;
|
|
border-bottom: 1px solid #e1e1e1;
|
|
}
|
|
|
|
.tab-button {
|
|
flex: 1;
|
|
padding: 1rem;
|
|
background: none;
|
|
border: none;
|
|
cursor: pointer;
|
|
font-size: 0.9rem;
|
|
color: #666;
|
|
border-bottom: 2px solid transparent;
|
|
}
|
|
|
|
.tab-button.active {
|
|
color: #667eea;
|
|
border-bottom-color: #667eea;
|
|
background: white;
|
|
}
|
|
|
|
.tab-content {
|
|
padding: 1rem;
|
|
}
|
|
|
|
.tab-panel {
|
|
display: none;
|
|
}
|
|
|
|
.tab-panel.active {
|
|
display: block;
|
|
}
|
|
|
|
.form-group {
|
|
margin-bottom: 1rem;
|
|
}
|
|
|
|
label {
|
|
display: block;
|
|
margin-bottom: 0.5rem;
|
|
color: #333;
|
|
font-weight: 500;
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
input[type="text"],
|
|
input[type="number"],
|
|
textarea,
|
|
select {
|
|
width: 100%;
|
|
padding: 0.6rem;
|
|
border: 2px solid #e1e1e1;
|
|
border-radius: 6px;
|
|
font-size: 0.9rem;
|
|
transition: border-color 0.3s;
|
|
}
|
|
|
|
input:focus,
|
|
textarea:focus,
|
|
select:focus {
|
|
outline: none;
|
|
border-color: #667eea;
|
|
}
|
|
|
|
textarea {
|
|
resize: vertical;
|
|
min-height: 2em;
|
|
}
|
|
|
|
.btn {
|
|
width: 49%;
|
|
padding: 0.7rem;
|
|
border: none;
|
|
border-radius: 6px;
|
|
font-size: 0.9rem;
|
|
font-weight: 500;
|
|
cursor: pointer;
|
|
transition: background-color 0.3s;
|
|
}
|
|
|
|
.btn-primary {
|
|
background: #667eea;
|
|
color: white;
|
|
}
|
|
|
|
.btn-primary:hover {
|
|
background: #5a6fd8;
|
|
}
|
|
|
|
.btn-secondary {
|
|
background: #6c757d;
|
|
color: white;
|
|
}
|
|
|
|
.btn-secondary:hover {
|
|
background: #5a6268;
|
|
}
|
|
|
|
.station-list {
|
|
max-height: 300px;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.station-item {
|
|
padding: 0.8rem;
|
|
border-bottom: 1px solid #e1e1e1;
|
|
cursor: pointer;
|
|
transition: background-color 0.3s;
|
|
display: flex;
|
|
}
|
|
|
|
.station-item:hover {
|
|
background: #f8f9fa;
|
|
}
|
|
|
|
.station-item.selected {
|
|
background: #e3f2fd;
|
|
border-left: 4px solid #667eea;
|
|
}
|
|
|
|
.station-name {
|
|
font-weight: 500;
|
|
color: #333;
|
|
margin-bottom: 0.3rem;
|
|
}
|
|
|
|
.station-status {
|
|
font-size: 0.8rem;
|
|
color: #666;
|
|
display: flex;
|
|
align-items: center;
|
|
margin-left: 2em;
|
|
}
|
|
|
|
.status-dot {
|
|
width: 8px;
|
|
height: 8px;
|
|
border-radius: 50%;
|
|
margin-right: 0.5rem;
|
|
}
|
|
|
|
.map-controls {
|
|
position: absolute;
|
|
top: 10px;
|
|
right: 10px;
|
|
z-index: 1000;
|
|
}
|
|
|
|
.add-pin-btn {
|
|
background: white;
|
|
border: 1px solid #ccc;
|
|
padding: 0.5rem;
|
|
margin-bottom: 0.5rem;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
font-size: 0.8rem;
|
|
display: inline-block;
|
|
width: 40%;
|
|
}
|
|
|
|
input#coordinates {
|
|
display: inline-block;
|
|
width: 58%;
|
|
}
|
|
|
|
.map-control-btn {
|
|
background: white;
|
|
border: 1px solid #ccc;
|
|
padding: 0.5rem;
|
|
margin-bottom: 0.5rem;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
font-size: 0.8rem;
|
|
}
|
|
|
|
.message {
|
|
padding: 0.8rem;
|
|
margin-bottom: 1rem;
|
|
border-radius: 6px;
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.message.success {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
border: 1px solid #c3e6cb;
|
|
}
|
|
|
|
.message.error {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
border: 1px solid #f5c6cb;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.main-content {
|
|
flex-direction: column;
|
|
}
|
|
|
|
.sidebar {
|
|
max-width: none;
|
|
height: 55vh;
|
|
border-right: none;
|
|
border-bottom: 1px solid #e1e1e1;
|
|
}
|
|
|
|
.map-container {
|
|
height: 40vh;
|
|
}
|
|
|
|
.header {
|
|
flex-direction: column;
|
|
gap: 0.5rem;
|
|
text-align: center;
|
|
}
|
|
|
|
.header-right {
|
|
justify-content: center;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="header">
|
|
<div class="header-right">
|
|
<div class="user-info">
|
|
<h1>💧 Water Stations - <span id="cityName">Loading...</span></h1>
|
|
Welcome, <span id="username"></span>
|
|
</div>
|
|
<a href="/city-select" class="city-selector">Change City</a>
|
|
<button class="logout-btn" onclick="logout()">Logout</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="main-content">
|
|
<div class="sidebar">
|
|
<div class="tab-buttons">
|
|
<button class="tab-button active" onclick="switchTab('update')">Update Status</button>
|
|
<button class="tab-button" onclick="switchTab('add')">Add Station</button>
|
|
</div>
|
|
|
|
<div class="tab-content">
|
|
<div id="update-tab" class="tab-panel active">
|
|
<div id="update-message"></div>
|
|
<div class="form-group">
|
|
<div class="station-list" id="stationList">
|
|
<!-- Stations will be populated here -->
|
|
</div>
|
|
</div>
|
|
|
|
<form id="updateStationForm" style="display: none;">
|
|
<div class="form-group">
|
|
<label for="updateDescription">Status Description</label>
|
|
<textarea id="updateDescription" name="description" placeholder="e.g., Refilled and working well"></textarea>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="estimatedHours">Estimated hours until empty</label>
|
|
<input type="number" id="estimatedHours" name="estimatedHours" min="1" max="48" value="6">
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary">Update Status</button>
|
|
<button type="button" class="btn btn-secondary" onclick="cancelUpdate()">Cancel</button>
|
|
</form>
|
|
</div>
|
|
<div id="add-tab" class="tab-panel">
|
|
<div id="add-message"></div>
|
|
<form id="addStationForm">
|
|
<div class="form-group">
|
|
<label for="stationName">Station Name</label>
|
|
<input type="text" id="stationName" name="name" required>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="stationDescription">Description</label>
|
|
<textarea id="stationDescription" name="description" placeholder="e.g., Public fountain in park"></textarea>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Location</label>
|
|
<!--<p style="font-size: 0.8rem; color: #666; margin-bottom: 0.5rem;">
|
|
Click on the map to set location
|
|
</p>-->
|
|
<button class="add-pin-btn" onclick="startAddMode()">
|
|
<span id="addModeText">Select Location</span>
|
|
</button>
|
|
<input type="text" id="coordinates" readonly>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary">Add Station</button>
|
|
</form>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<div class="map-container">
|
|
<div id="map"></div>
|
|
<!--<div class="map-controls">
|
|
<button class="map-control-btn" onclick="centerMap()">Center Map</button>
|
|
</div>-->
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
|
<script>
|
|
let map;
|
|
let stations = [];
|
|
let selectedStation = null;
|
|
let addMode = false;
|
|
let tempMarker = null;
|
|
let user = null;
|
|
let currentCity = null;
|
|
|
|
function initDashboard() {
|
|
// Get city name from URL
|
|
const pathParts = window.location.pathname.split('/');
|
|
currentCity = pathParts[2];
|
|
|
|
if (!currentCity) {
|
|
window.location.href = '/city-select';
|
|
return;
|
|
}
|
|
|
|
document.getElementById('cityName').textContent = currentCity.charAt(0).toUpperCase() + currentCity.slice(1);
|
|
|
|
checkAuth();
|
|
initMap();
|
|
loadStations();
|
|
}
|
|
|
|
async function checkAuth() {
|
|
try {
|
|
const response = await fetch('/api/user');
|
|
const data = await response.json();
|
|
|
|
if (!data.user) {
|
|
window.location.href = '/login';
|
|
return;
|
|
}
|
|
|
|
user = data.user;
|
|
document.getElementById('username').textContent = user.display_name || user.username || 'User';
|
|
} catch (error) {
|
|
window.location.href = '/login';
|
|
}
|
|
}
|
|
|
|
function initMap() {
|
|
map = L.map('map').setView([37.7749, -122.4194], 13);
|
|
|
|
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
attribution: '© OpenStreetMap contributors'
|
|
}).addTo(map);
|
|
|
|
map.on('click', function(e) {
|
|
if (addMode) {
|
|
setStationLocation(e.latlng);
|
|
}
|
|
});
|
|
}
|
|
|
|
function setStationLocation(latlng) {
|
|
if (tempMarker) {
|
|
map.removeLayer(tempMarker);
|
|
}
|
|
|
|
tempMarker = L.marker(latlng).addTo(map);
|
|
document.getElementById('coordinates').value = `${latlng.lat.toFixed(6)}, ${latlng.lng.toFixed(6)}`;
|
|
stopAddMode();
|
|
}
|
|
|
|
function startAddMode() {
|
|
addMode = true;
|
|
map.getContainer().style.cursor = 'crosshair';
|
|
}
|
|
|
|
function stopAddMode() {
|
|
addMode = false;
|
|
map.getContainer().style.cursor = '';
|
|
}
|
|
|
|
async function loadStations() {
|
|
try {
|
|
const response = await fetch(`/api/cities/${currentCity}/stations`);
|
|
const data = await response.json();
|
|
stations = data;
|
|
if (data.length > 0) {
|
|
document.getElementById('cityName').textContent = data[0].city_name;
|
|
}
|
|
displayStations();
|
|
populateStationList();
|
|
fitMapToStations();
|
|
} catch (error) {
|
|
console.error('Error loading stations:', error);
|
|
}
|
|
}
|
|
|
|
function getStationColor(station) {
|
|
if (!station.last_refill_time) {
|
|
return '#333';
|
|
}
|
|
|
|
const now = new Date();
|
|
const refillTime = new Date(station.last_refill_time);
|
|
const timeSinceRefill = now - refillTime;
|
|
const daysSinceRefill = timeSinceRefill / (1000 * 60 * 60 * 24);
|
|
|
|
if (daysSinceRefill > 7) {
|
|
return '#333';
|
|
}
|
|
|
|
if (!station.estimated_empty_time) {
|
|
return '#4CAF50';
|
|
}
|
|
|
|
const emptyTime = new Date(station.estimated_empty_time);
|
|
const timeUntilEmpty = emptyTime - now;
|
|
const hoursUntilEmpty = timeUntilEmpty / (1000 * 60 * 60);
|
|
|
|
if (hoursUntilEmpty <= 0) {
|
|
return '#f44336';
|
|
} else if (hoursUntilEmpty <= 3) {
|
|
return '#FFC107';
|
|
} else {
|
|
return '#4CAF50';
|
|
}
|
|
}
|
|
|
|
function displayStations() {
|
|
stations.forEach(station => {
|
|
const color = getStationColor(station);
|
|
|
|
const marker = L.circleMarker([station.latitude, station.longitude], {
|
|
color: color,
|
|
fillColor: color,
|
|
fillOpacity: 0.8,
|
|
radius: 10,
|
|
weight: 2
|
|
}).addTo(map);
|
|
|
|
const popupContent = createPopupContent(station);
|
|
marker.bindPopup(popupContent);
|
|
});
|
|
}
|
|
|
|
function createPopupContent(station) {
|
|
const refillTime = station.last_refill_time ?
|
|
new Date(station.last_refill_time).toLocaleString() : 'Never';
|
|
|
|
const estimatedEmpty = station.estimated_empty_time ?
|
|
new Date(station.estimated_empty_time).toLocaleString() : 'Unknown';
|
|
|
|
return `
|
|
<div style="min-width: 200px;">
|
|
<h3>${station.name}</h3>
|
|
<p><strong>Description:</strong> ${station.latest_description || 'No description'}</p>
|
|
<p><strong>Last Refill:</strong> ${refillTime}</p>
|
|
<p><strong>Estimated Empty:</strong> ${estimatedEmpty}</p>
|
|
<p><strong>Last Updated:</strong> ${station.last_updated ? new Date(station.last_updated).toLocaleString() : 'Never'}</p>
|
|
<p><strong>Last Updated By:</strong> ${station.updated_by_name || 'Unknown'}</p>
|
|
</div>
|
|
`;
|
|
}
|
|
|
|
function populateStationList() {
|
|
const stationList = document.getElementById('stationList');
|
|
stationList.innerHTML = '';
|
|
|
|
stations.forEach(station => {
|
|
const item = document.createElement('div');
|
|
item.className = 'station-item';
|
|
item.onclick = () => selectStation(station);
|
|
|
|
const color = getStationColor(station);
|
|
const statusText = getStatusText(station);
|
|
|
|
item.innerHTML = `
|
|
<span class="station-name">${station.name}</span>
|
|
<span class="station-status">
|
|
<span class="status-dot" style="background-color: ${color}"></span>
|
|
${statusText}
|
|
</span>
|
|
`;
|
|
|
|
stationList.appendChild(item);
|
|
});
|
|
}
|
|
|
|
function getStatusText(station) {
|
|
if (!station.last_refill_time) return 'Never updated';
|
|
|
|
const now = new Date();
|
|
const refillTime = new Date(station.last_refill_time);
|
|
const daysSinceRefill = (now - refillTime) / (1000 * 60 * 60 * 24);
|
|
|
|
if (daysSinceRefill > 7) return 'Old data (7+ days)';
|
|
|
|
if (!station.estimated_empty_time) return 'Recently refilled';
|
|
|
|
const emptyTime = new Date(station.estimated_empty_time);
|
|
const hoursUntilEmpty = (emptyTime - now) / (1000 * 60 * 60);
|
|
|
|
if (hoursUntilEmpty <= 0) return 'Empty';
|
|
if (hoursUntilEmpty <= 3) return 'Needs refill soon';
|
|
return 'Recently refilled';
|
|
}
|
|
|
|
function selectStation(station) {
|
|
selectedStation = station;
|
|
|
|
// Update UI
|
|
document.querySelectorAll('.station-item').forEach(item => {
|
|
item.classList.remove('selected');
|
|
});
|
|
|
|
event.target.closest('.station-item').classList.add('selected');
|
|
|
|
// Show update form
|
|
document.getElementById('updateStationForm').style.display = 'block';
|
|
|
|
// Center map on selected station
|
|
map.setView([station.latitude, station.longitude], 16);
|
|
}
|
|
|
|
function cancelUpdate() {
|
|
selectedStation = null;
|
|
document.getElementById('updateStationForm').style.display = 'none';
|
|
document.querySelectorAll('.station-item').forEach(item => {
|
|
item.classList.remove('selected');
|
|
});
|
|
}
|
|
|
|
function fitMapToStations() {
|
|
if (stations.length === 0) return;
|
|
|
|
const bounds = L.latLngBounds();
|
|
stations.forEach(station => {
|
|
bounds.extend([station.latitude, station.longitude]);
|
|
});
|
|
|
|
map.fitBounds(bounds, { padding: [20, 20] });
|
|
}
|
|
|
|
function centerMap() {
|
|
fitMapToStations();
|
|
}
|
|
|
|
function switchTab(tabName) {
|
|
// Update tab buttons
|
|
document.querySelectorAll('.tab-button').forEach(btn => {
|
|
btn.classList.remove('active');
|
|
});
|
|
|
|
document.querySelectorAll('.tab-panel').forEach(panel => {
|
|
panel.classList.remove('active');
|
|
});
|
|
|
|
event.target.classList.add('active');
|
|
document.getElementById(tabName + '-tab').classList.add('active');
|
|
}
|
|
|
|
function showMessage(elementId, message, type = 'success') {
|
|
const messageDiv = document.getElementById(elementId);
|
|
messageDiv.innerHTML = `<div class="message ${type}">${message}</div>`;
|
|
|
|
setTimeout(() => {
|
|
messageDiv.innerHTML = '';
|
|
}, 5000);
|
|
}
|
|
|
|
// Form handlers
|
|
document.getElementById('addStationForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const coordinates = document.getElementById('coordinates').value;
|
|
if (!coordinates) {
|
|
showMessage('add-message', 'Please select a location on the map', 'error');
|
|
return;
|
|
}
|
|
|
|
const [lat, lng] = coordinates.split(',').map(coord => parseFloat(coord.trim()));
|
|
|
|
const formData = new FormData(e.target);
|
|
const data = {
|
|
name: formData.get('name'),
|
|
description: formData.get('description'),
|
|
latitude: lat,
|
|
longitude: lng
|
|
};
|
|
|
|
try {
|
|
const response = await fetch(`/api/cities/${currentCity}/stations`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(data)
|
|
});
|
|
|
|
if (response.ok) {
|
|
showMessage('add-message', 'Station added successfully!');
|
|
e.target.reset();
|
|
document.getElementById('coordinates').value = '';
|
|
if (tempMarker) {
|
|
map.removeLayer(tempMarker);
|
|
tempMarker = null;
|
|
}
|
|
loadStations();
|
|
} else {
|
|
const result = await response.json();
|
|
showMessage('add-message', result.error || 'Failed to add station', 'error');
|
|
}
|
|
} catch (error) {
|
|
showMessage('add-message', 'Failed to add station', 'error');
|
|
}
|
|
});
|
|
|
|
document.getElementById('updateStationForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
if (!selectedStation) {
|
|
showMessage('update-message', 'Please select a station first', 'error');
|
|
return;
|
|
}
|
|
|
|
const formData = new FormData(e.target);
|
|
const data = {
|
|
description: formData.get('description'),
|
|
estimatedHours: formData.get('estimatedHours')
|
|
};
|
|
|
|
try {
|
|
const response = await fetch(`/api/stations/${selectedStation.id}/update`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(data)
|
|
});
|
|
|
|
if (response.ok) {
|
|
showMessage('update-message', 'Station updated successfully!');
|
|
e.target.reset();
|
|
cancelUpdate();
|
|
loadStations();
|
|
} else {
|
|
const result = await response.json();
|
|
showMessage('update-message', result.error || 'Failed to update station', 'error');
|
|
}
|
|
} catch (error) {
|
|
showMessage('update-message', 'Failed to update station', 'error');
|
|
}
|
|
});
|
|
|
|
async function logout() {
|
|
try {
|
|
await fetch('/auth/logout', { method: 'POST' });
|
|
window.location.href = '/';
|
|
} catch (error) {
|
|
console.error('Logout failed:', error);
|
|
}
|
|
}
|
|
|
|
// Initialize dashboard when page loads
|
|
document.addEventListener('DOMContentLoaded', initDashboard);
|
|
</script>
|
|
</body>
|
|
</html> |