-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver-dashboard.html
More file actions
437 lines (395 loc) · 20.7 KB
/
Copy pathdriver-dashboard.html
File metadata and controls
437 lines (395 loc) · 20.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Driver Dashboard | BusTrack</title>
<link rel="stylesheet" href="css/leaflet.css?v=12">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.4.0/css/all.min.css" crossorigin="anonymous" referrerpolicy="no-referrer">
<script src="js/icons.js?v=12"></script>
<link rel="stylesheet" href="css/driver.css">
<link rel="manifest" href="/manifest.json">
<meta name="theme-color" content="#1A1A1A">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="default">
<meta name="apple-mobile-web-app-title" content="BusTrack">
</style>
<script src="js/session-protection.js"></script>
<script>protectRoute('driver');</script>
</head>
<body>
<div id="offline-indicator" style="display:none; position:fixed; bottom:20px; left:50%; transform:translateX(-50%); background:#1A1A1A; color:white; padding:10px 20px; border-radius:20px; font-size:13px; font-weight:500; font-family:Inter,sans-serif; z-index:9999; box-shadow:0 4px 20px rgba(0,0,0,0.3); align-items:center; gap:8px;">
You are offline - showing cached data
</div>
<!-- NAVBAR -->
<nav class="navbar">
<div class="brand" style="display: flex; align-items: center; gap: 8px;">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 462 244" role="img" aria-label="Where Is My Bus" width="36" height="36"><path fill-rule="evenodd" fill="currentColor" d="M406.66 132.0 L428.16 111.0 L445.38 91.0 L453.18 76.0 L455.31 68.0 L456.2 57.0 L455.31 48.0 L451.19 35.0 L441.0 20.37 L433.0 13.89 L425.0 9.72 L415.0 6.68 L405.0 5.83 L396.0 6.7 L386.0 9.66 L377.0 14.62 L369.0 21.38 L363.67 28.0 L358.74 37.0 L355.69 47.0 L354.6 60.0 L357.58 76.0 L365.85 92.0 L396.63 126.0 L404.0 132.24 L406.66 132.0 Z M320.22 238.0 L331.0 236.31 L342.0 232.2 L353.0 225.19 L361.33 217.0 L369.37 205.0 L400.82 145.0 L381.0 127.84 L357.0 171.82 L352.58 175.0 L340.17 199.0 L335.94 204.0 L327.0 209.23 L314.0 211.33 L303.0 209.34 L292.31 203.0 L284.6 193.0 L252.69 131.0 L278.0 82.24 L332.0 185.4 L347.66 160.0 L289.3 45.0 L281.21 34.0 L272.0 25.69 L257.0 17.88 L239.0 14.76 L223.0 16.62 L208.0 22.76 L197.33 31.0 L188.89 41.0 L159.0 97.67 L155.62 93.0 L129.37 41.0 L120.0 29.62 L109.0 21.59 L98.0 16.84 L86.0 14.65 L5.43 15.0 L103.76 206.0 L112.0 217.56 L123.0 227.27 L139.0 235.2 L158.0 238.15 L175.0 236.27 L191.0 229.34 L202.0 220.51 L210.27 210.0 L237.0 159.79 L260.65 205.0 L267.8 216.0 L273.38 222.0 L283.0 229.37 L296.0 235.36 L304.0 237.35 L320.22 238.0 Z M409.0 77.03 L403.0 77.3 L395.0 74.38 L388.82 68.0 L386.55 63.0 L385.72 55.0 L387.79 48.0 L394.0 40.71 L400.0 37.67 L407.0 36.95 L414.0 38.78 L421.35 45.0 L424.41 51.0 L425.21 59.0 L422.29 68.0 L416.0 74.38 L409.0 77.03 Z M118.0 175.46 L49.0 41.52 L83.0 41.34 L91.0 42.69 L100.53 48.0 L107.45 57.0 L142.95 127.0 L118.0 175.46 Z M163.0 211.01 L156.0 211.11 L147.0 209.17 L139.0 205.13 L135.46 201.0 L208.8 60.0 L217.32 49.0 L230.0 42.65 L241.0 41.89 L249.0 43.71 L255.0 46.67 L262.34 54.0 L188.33 195.0 L183.29 202.0 L178.0 206.28 L171.0 209.45 L163.0 211.01 Z"/></svg>
<span>BusTrack <em>Driver Console</em></span>
</div>
<button class="start-btn" id="start-btn" onclick="startTrip()">Start Trip</button>
<button class="logout-btn" onclick="driverLogout()" style="margin-left: 10px; background: #2a2a2a; border: 1px solid #444; color: #fff; padding: 10px 16px; border-radius: 8px; font-weight: 600; cursor: pointer; display: flex; align-items: center; gap: 6px;"><i class="fas fa-sign-out-alt"></i> Logout</button>
</nav>
<!-- STAT CARDS — 3 cards, Next Stop hidden for JS only -->
<div class="stats-grid">
<div class="stat-card">
<div class="stat-label">Speed</div>
<div class="stat-value" id="speed-display">0.0 km/h</div>
</div>
<div class="stat-card">
<div class="stat-label">Duration</div>
<div class="stat-value" id="trip-duration-display">00:00:00</div>
</div>
<div class="stat-card">
<div class="stat-label">Current Position</div>
<div class="stat-value searching" id="location-display">Searching...</div>
</div>
</div>
<!-- Hidden next stop for JS reference — not visible on page -->
<span id="next-stop-display" style="display:none;"></span>
<!-- TRIP STATUS BAR — shows when trip is active -->
<div class="trip-status-bar" id="trip-status-bar">
<div class="status-dot-green"></div>
<span id="trip-status-text">Trip active — GPS tracking live</span>
</div>
<!-- SCREEN OFF WARNING — shown only while a trip is active -->
<div class="screen-off-warning" id="screen-off-warning">
<i class="fas fa-lock" style="font-size:16px; color:#d97706;"></i>
<span>⚠️ <strong>Do not lock your screen</strong> — GPS tracking will pause if the screen turns off. The app keeps your screen awake automatically.</span>
</div>
<!-- MAP -->
<div class="map-wrapper">
<div id="map"></div>
</div>
<!-- SOS BUTTON (only visible control during driving) -->
<button id="sos-btn" onclick="triggerSOS()" style="
position: fixed;
bottom: 40px;
right: 20px;
background: #ef4444;
color: white;
border: none;
padding: 16px 28px;
border-radius: 30px;
font-weight: 800;
font-size: 16px;
font-family: 'Inter', sans-serif;
letter-spacing: 0.5px;
box-shadow: 0 10px 30px rgba(239, 68, 68, 0.4), 0 0 0 0 rgba(239,68,68,0.4);
z-index: 9999;
cursor: pointer;
display: flex;
gap: 10px;
align-items: center;
animation: sos-pulse-idle 3s ease-in-out infinite;
transition: transform 0.15s ease, box-shadow 0.15s ease;
">
<i class="fas fa-exclamation-triangle"></i>
SOS EMERGENCY
</button>
<style>
@keyframes sos-pulse-idle {
0%, 100% { box-shadow: 0 10px 30px rgba(239,68,68,0.4), 0 0 0 0 rgba(239,68,68,0.35); }
60% { box-shadow: 0 10px 30px rgba(239,68,68,0.4), 0 0 0 12px rgba(239,68,68,0); }
}
#sos-btn:hover { transform: translateY(-2px); box-shadow: 0 16px 40px rgba(239,68,68,0.5) !important; animation: none; }
#sos-btn:active { transform: scale(0.97); }
#sos-btn:disabled { opacity: 0.7; cursor: not-allowed; animation: none; }
</style>
<!-- POST-SOS EMERGENCY PANEL (hidden by default, shown after SOS sent) -->
<div id="sos-panel" style="
position: fixed;
bottom: 110px;
right: 20px;
width: 340px;
background: rgba(255,255,255,0.97);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border: 1px solid #fecaca;
border-radius: 24px;
box-shadow: 0 20px 50px rgba(239,68,68,0.12), 0 8px 25px rgba(15,23,42,0.1);
z-index: 9998;
display: none;
flex-direction: column;
overflow: hidden;
animation: slide-up 0.35s cubic-bezier(0.34,1.56,0.64,1);
">
<style>
@keyframes slide-up { from { opacity:0; transform: translateY(24px); } to { opacity:1; transform: translateY(0); } }
</style>
<!-- Panel Header -->
<div style="background: linear-gradient(135deg, #7f1d1d 0%, #991b1b 100%); padding: 18px 20px; display: flex; align-items: center; gap: 12px;">
<div style="width: 36px; height: 36px; background: rgba(255,255,255,0.15); border-radius: 10px; display:flex; align-items:center; justify-content:center; flex-shrink:0;">
<i class="fas fa-shield-alt" style="color:white; font-size:15px;"></i>
</div>
<div style="flex:1; min-width:0;">
<div style="color:white; font-weight:700; font-size:14px; letter-spacing:-0.2px;">Emergency Alert Sent</div>
<div style="color:rgba(255,255,255,0.65); font-size:11px; margin-top:2px; font-family:'JetBrains Mono', monospace;" id="sos-sent-time">Notifying admin...</div>
</div>
<button onclick="closeSosPanel()" title="Close" style="background:rgba(255,255,255,0.1); border:none; color:rgba(255,255,255,0.7); width:30px; height:30px; border-radius:8px; cursor:pointer; display:flex; align-items:center; justify-content:center; flex-shrink:0; font-size:14px; transition:background 0.2s;">
<i class="fas fa-times"></i>
</button>
</div>
<!-- Status Row -->
<div style="padding: 14px 18px; background: #fef2f2; border-bottom: 1px solid #fecaca; display:flex; align-items:center; gap:10px;">
<div style="width: 8px; height: 8px; background: #ef4444; border-radius: 50%; animation: sos-pulse-idle 1.5s infinite; flex-shrink:0;"></div>
<span style="font-size: 12px; font-weight: 600; color: #991b1b;">Admin has been notified — Stay calm, help is coming</span>
</div>
<!-- Chat Section Toggle -->
<div id="sos-chat-collapsed" style="padding: 16px 18px;">
<p style="font-size: 13px; color: #64748b; margin-bottom: 12px; line-height: 1.5;">Need to send more details to the admin? Open the chat channel below.</p>
<button onclick="openSosChat()" style="
width: 100%;
padding: 12px 18px;
background: #0f172a;
color: white;
border: none;
border-radius: 12px;
font-weight: 700;
font-size: 13px;
font-family: 'Inter', sans-serif;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
transition: background 0.2s;
" onmouseover="this.style.background='#1e293b'" onmouseout="this.style.background='#0f172a'">
<i class="fas fa-comment-dots"></i>
Send Message to Administrator
</button>
</div>
<!-- Inline Chat (hidden until "Send Message" tapped) -->
<div id="sos-chat-open" style="display:none; flex-direction:column;">
<!-- Chat Header -->
<div style="padding: 12px 18px; border-top: 1px solid #f1f5f9; display:flex; align-items:center; justify-content:space-between;">
<span style="font-size: 12px; font-weight: 700; text-transform:uppercase; letter-spacing: 0.7px; color:#64748b;">
<i class="fas fa-comments" style="margin-right:5px;"></i>Admin Chat
</span>
<button onclick="closeSosChat()" style="background:none; border:none; color:#94a3b8; font-size:12px; cursor:pointer; font-family:'Inter',sans-serif;">
<i class="fas fa-chevron-down"></i> Collapse
</button>
</div>
<!-- Messages -->
<div id="sos-inline-messages" style="height: 200px; overflow-y: auto; padding: 12px 18px; display:flex; flex-direction:column; gap:8px; font-size:13px; background:#f8fafc;">
<div style="text-align:center; color:#94a3b8; font-size:12px; margin:auto;">No messages yet.</div>
</div>
<!-- Input -->
<div style="padding: 12px 18px; border-top: 1px solid #e2e8f0; display:flex; gap:8px; background:white;">
<input type="text" id="sos-inline-input" placeholder="Type your message…" autocomplete="off" style="flex:1; padding:10px 14px; border:1.5px solid #e2e8f0; border-radius:10px; outline:none; font-family:'Inter',sans-serif; font-size:13px; color:#0f172a; transition: border-color 0.2s;"
onfocus="this.style.borderColor='#4f46e5'" onblur="this.style.borderColor='#e2e8f0'">
<button onclick="sendSosPanelMessage()" style="background:#0f172a; color:white; border:none; border-radius:10px; width:40px; height:40px; cursor:pointer; display:flex; align-items:center; justify-content:center; flex-shrink:0; transition: background 0.2s;"
onmouseover="this.style.background='#4f46e5'" onmouseout="this.style.background='#0f172a'">
<i class="fas fa-paper-plane" style="font-size:13px;"></i>
</button>
</div>
</div>
</div>
<script>
function openSosPanel() {
const panel = document.getElementById('sos-panel');
const timeEl = document.getElementById('sos-sent-time');
const now = new Date();
timeEl.textContent = 'Sent at ' + now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: true });
document.getElementById('sos-chat-collapsed').style.display = 'block';
document.getElementById('sos-chat-open').style.display = 'none';
panel.style.display = 'flex';
}
function closeSosPanel() {
document.getElementById('sos-panel').style.display = 'none';
}
function openSosChat() {
document.getElementById('sos-chat-collapsed').style.display = 'none';
const chatOpen = document.getElementById('sos-chat-open');
chatOpen.style.display = 'flex';
chatOpen.style.flexDirection = 'column';
// Use the main floating chat panel for SOS chat (avoids DOM ID conflicts)
const session = JSON.parse(localStorage.getItem('driverSession') || '{}');
const busId = session.assignedBus || session.busId || null;
if (busId && typeof initChat === 'function') {
// Load messages into the inline SOS panel display
_loadSosChatMessages(busId);
}
setTimeout(() => document.getElementById('sos-inline-input')?.focus(), 100);
}
async function _loadSosChatMessages(busId) {
const session = JSON.parse(localStorage.getItem('driverSession') || '{}');
const token = session.token;
if (!token) return;
try {
const res = await fetch(`${BACKEND_URL}/api/chat/${encodeURIComponent(busId)}`, {
headers: { 'Authorization': `Bearer ${token}` }
});
const data = await res.json();
const container = document.getElementById('sos-inline-messages');
if (!container || !data.success) return;
container.innerHTML = '';
if (!data.messages || data.messages.length === 0) {
container.innerHTML = '<div style="text-align:center;color:#94a3b8;font-size:12px;margin:auto;">No messages yet.</div>';
return;
}
data.messages.forEach(msg => {
const isMine = msg.sender_role === 'driver';
const time = new Date(msg.created_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const div = document.createElement('div');
div.style.cssText = `display:flex; flex-direction:column; align-items:${isMine ? 'flex-end' : 'flex-start'}; max-width:85%; align-self:${isMine ? 'flex-end' : 'flex-start'};`;
div.innerHTML = `
<div style="font-size:10px;color:#94a3b8;margin-bottom:2px;">${isMine ? 'You' : '📡 Dispatcher'} · ${time}</div>
<div style="padding:8px 12px;border-radius:12px;font-size:12px;background:${isMine ? '#0f172a' : '#e2e8f0'};color:${isMine ? 'white' : '#0f172a'};">${msg.message}</div>`;
container.appendChild(div);
});
container.scrollTop = container.scrollHeight;
} catch (e) { console.warn('[SOS Chat]', e); }
}
async function sendSosPanelMessage() {
const input = document.getElementById('sos-inline-input');
const msg = input?.value.trim();
if (!msg) return;
const session = JSON.parse(localStorage.getItem('driverSession') || '{}');
const token = session.token;
const busId = session.assignedBus || session.busId;
if (!token || !busId) return;
input.value = '';
try {
await fetch(`${BACKEND_URL}/api/chat/send`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` },
body: JSON.stringify({ bus_id: busId, message: msg })
});
// Refresh inline display
_loadSosChatMessages(busId);
} catch (e) { console.error('[SOS Chat send]', e); }
}
function closeSosChat() {
document.getElementById('sos-chat-collapsed').style.display = 'block';
document.getElementById('sos-chat-open').style.display = 'none';
}
</script>
<script src="js/leaflet.js?v=12"></script>
<script src="js/supabase.min.js"></script>
<script src="js/supabase-client.js?v=5"></script>
<script src="js/haversine.js"></script>
<script src="js/kalman.js"></script>
<script src="js/routes.js"></script>
<script src="js/map.js?v=12"></script>
<script src="js/location.js"></script>
<script src="js/timer.js"></script>
<script src="js/trip.js?v=2"></script>
<script src="js/sync.js?v=5"></script>
<script src="js/driver-auth.js?v=5"></script>
<script src="js/session-protection.js"></script>
<script src="js/chat.js"></script>
<script>
protectRoute('driver');
const driver = JSON.parse(localStorage.getItem('driverSession'));
if (!driver || !driver.driverId) window.location.href = 'driver-login.html';
// Make initChat accessible inside trip.js or call it when trip starts.
// We will patch startTrip in trip.js if needed, or we can listen for trip start events.
// For now, we'll patch startTrip here, but it's defined in trip.js.
// We will update trip.js to call initChat.
// UPDATE 8 — Driver Bus Assignment Restriction
function lockBusToDriver() {
const session = JSON.parse(localStorage.getItem('driverSession'));
const assignedBus = session.assignedBus || session.busId; // fallback to busId if assignedBus not set
const select = document.getElementById('bus-select');
if (select && assignedBus) {
// Clear and add only the assigned bus
select.innerHTML = `<option value="${assignedBus}">${assignedBus}</option>`;
select.value = assignedBus;
select.disabled = true;
}
if (assignedBus) {
const label = document.getElementById('assigned-bus-label');
if (label) label.textContent = assignedBus;
}
}
const driverNameDisplay = document.getElementById('driver-name-display');
if (driverNameDisplay) driverNameDisplay.textContent = driver.username;
lockBusToDriver();
// Initialize real-time chat with dispatcher
if (driver.assignedBus || driver.busId) {
initChat(driver.assignedBus || driver.busId, 'driver');
}
// Init map defaults
const defaultType = 'morning';
initMap(defaultType);
// Get immediate location so the map isn't empty before starting the trip
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((pos) => {
const lat = pos.coords.latitude;
const lon = pos.coords.longitude;
const busLabel = driver.assignedBus ? driver.assignedBus.replace(/bus\s*/i, 'B').toUpperCase() : 'BUS';
if (typeof updateBusMarker === 'function') {
updateBusMarker(lat, lon, busLabel);
}
}, () => {}, { timeout: 10000 });
}
// Removed shift change dropdown logic as it's no longer needed
// Subscribe to realtime location updates for map/chart synchronization
if (driver && (driver.assignedBus || driver.busId)) {
subscribeToBus(driver.assignedBus || driver.busId, defaultType);
}
// UPDATE 13 — Trip Recovery After Page Refresh
if (typeof recoverActiveTrip === 'function') {
recoverActiveTrip();
}
</script>
<script>
window.addEventListener('online', () => {
document.getElementById('offline-indicator').style.display = 'none';
console.log('[Network] Back online');
});
window.addEventListener('offline', () => {
document.getElementById('offline-indicator').style.display = 'flex';
console.log('[Network] Gone offline');
});
if (!navigator.onLine) {
document.getElementById('offline-indicator').style.display = 'flex';
}
</script>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./sw.js')
.then((reg) => {
console.log('[BusTrack] Service Worker registered:', reg.scope);
})
.catch((err) => {
console.warn('[BusTrack] Service Worker failed:', err);
});
});
}
// Ensure offline on unload and maintain heartbeat
(function() {
const driverSession = JSON.parse(localStorage.getItem('driverSession'));
const activeBus = driverSession ? (driverSession.assignedBus || driverSession.busId) : null;
const token = driverSession ? driverSession.token : null;
if (driverSession && activeBus && token) {
window.addEventListener('beforeunload', () => {
// Fire-and-forget offline status update
fetch(`${BACKEND_URL}/api/auth/logout-driver`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}` }
});
});
const sendHeartbeat = () => {
if (navigator.onLine) {
fetch(`${BACKEND_URL}/api/trip/heartbeat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` },
body: JSON.stringify({ bus_id: activeBus })
});
}
};
// Run immediately, then every 30 seconds to be more responsive
sendHeartbeat();
setInterval(sendHeartbeat, 30000);
}
})();
</script>
</body>
</html>