-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
754 lines (677 loc) · 37.6 KB
/
index.html
File metadata and controls
754 lines (677 loc) · 37.6 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RickshawGo - Passenger & Location</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
/* --- VARIABLES --- */
:root {
--primary: #FFC107;
--primary-dark: #e0a800;
--dark: #212529;
--grey: #6c757d;
--light: #f8f9fa;
--white: #ffffff;
--shadow: 0 8px 20px rgba(0,0,0,0.08);
--danger: #dc3545;
--success: #28a745;
}
* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', sans-serif; }
body {
background-color: #333;
display: flex; justify-content: center; height: 100vh; overflow: hidden;
}
.app-container {
width: 100%; max-width: 414px; height: 100%;
background-color: #f4f6f8; position: relative;
overflow: hidden; display: flex; flex-direction: column;
box-shadow: 0 0 30px rgba(0,0,0,0.5);
}
/* --- UTILITY --- */
.hidden { display: none !important; }
.fade-in { animation: fadeIn 0.3s ease-in-out; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
/* --- AUTH SCREEN --- */
.auth-screen {
width: 100%; height: 100%; background: var(--dark);
z-index: 200; position: absolute; display: flex; flex-direction: column;
padding: 20px; justify-content: center;
}
.role-switch-container {
background: rgba(255,255,255,0.1); border-radius: 50px; padding: 5px;
display: flex; margin-bottom: 20px;
}
.role-btn {
flex: 1; padding: 12px; border-radius: 40px; text-align: center;
color: #aaa; cursor: pointer; font-weight: 600; transition: 0.3s;
}
.role-btn.active { color: var(--dark); background: var(--primary); }
.auth-card { background: var(--white); border-radius: 20px; padding: 30px 25px; }
.auth-input {
width: 100%; padding: 12px 15px; border: 1px solid #ddd;
border-radius: 10px; margin-bottom: 15px; outline: none;
}
.action-btn {
width: 100%; padding: 14px; background: var(--dark); color: var(--primary);
border: none; border-radius: 10px; font-weight: bold; cursor: pointer;
}
.google-btn {
width: 100%; padding: 12px; border: 1px solid #ddd; border-radius: 10px;
display: flex; align-items: center; justify-content: center; gap: 10px;
margin-top: 15px; cursor: pointer; background: white;
}
/* --- SCREENS --- */
.driver-screen, .passenger-screen, .history-screen {
width: 100%; height: 100%; display: flex; flex-direction: column;
position: absolute; top: 0; background: #f4f6f8; z-index: 100;
}
.history-screen { z-index: 110; background: white; transform: translateX(100%); transition: 0.3s; }
.history-screen.active { transform: translateX(0); }
/* Header */
.header {
padding: 15px 20px; background: var(--white); display: flex;
justify-content: space-between; align-items: center;
box-shadow: 0 2px 10px rgba(0,0,0,0.05); z-index: 10;
}
.menu-btn { font-size: 20px; color: var(--dark); cursor: pointer; }
.app-title { font-weight: 800; color: var(--dark); font-size: 18px; }
.location-icon-btn {
width: 35px; height: 35px; background: rgba(255, 193, 7, 0.2);
color: #d39e00; border-radius: 50%; display: flex;
align-items: center; justify-content: center;
}
/* Content */
.content-area { flex: 1; overflow-y: auto; padding: 20px; padding-bottom: 80px; }
.content-area::-webkit-scrollbar { display: none; }
/* --- NEW TRIP CARD LAYOUT --- */
.trip-layout-card {
background: var(--white); border-radius: 16px; padding: 20px;
margin-bottom: 15px; box-shadow: var(--shadow); cursor: pointer;
transition: 0.2s; position: relative; border-left: 5px solid var(--primary);
}
.trip-header {
display: flex; justify-content: space-between; align-items: flex-start;
margin-bottom: 15px; border-bottom: 1px solid #f0f0f0; padding-bottom: 10px;
}
.trip-driver-box { display: flex; gap: 12px; align-items: center; }
.trip-avatar {
width: 45px; height: 45px; background: #212529; color: var(--primary);
border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 18px;
}
.trip-name { font-weight: 700; color: var(--dark); font-size: 15px; }
.trip-vehicle { font-size: 11px; color: #666; background: #eee; padding: 2px 6px; border-radius: 4px; margin-top: 3px; display: inline-block; }
.trip-price { text-align: right; }
.trip-amount { font-size: 18px; font-weight: 800; color: var(--dark); }
.trip-est { font-size: 10px; color: #888; text-transform: uppercase; }
.trip-route { display: flex; align-items: center; gap: 10px; margin-bottom: 15px; }
.route-icon { color: var(--success); font-size: 12px; }
.route-text { font-size: 14px; font-weight: 500; color: #444; }
.trip-footer { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: #777; }
.trip-time { display: flex; align-items: center; gap: 5px; }
/* --- BOTTOM NAV --- */
.bottom-nav {
position: absolute; bottom: 0; width: 100%; height: 70px;
background: var(--white); display: flex; justify-content: space-around;
align-items: center; border-top: 1px solid #eee; z-index: 20;
}
.nav-btn { display: flex; flex-direction: column; align-items: center; color: var(--grey); font-size: 11px; cursor: pointer; }
.nav-btn.active { color: var(--dark); font-weight: bold; }
.nav-btn i { font-size: 20px; margin-bottom: 4px; }
.nav-btn.active i { color: var(--primary); }
/* --- DRAWER --- */
.drawer-overlay {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.6); z-index: 150; opacity: 0; visibility: hidden; transition: 0.3s;
}
.drawer-overlay.active { opacity: 1; visibility: visible; }
.nav-drawer {
position: absolute; top: 0; left: 0; width: 80%; height: 100%;
background: var(--white); z-index: 160; transform: translateX(-100%);
transition: transform 0.3s; display: flex; flex-direction: column;
}
.nav-drawer.active { transform: translateX(0); }
.drawer-profile { background: var(--dark); color: white; padding: 40px 20px 30px 20px; }
.menu-item {
display: flex; align-items: center; padding: 15px 20px;
color: var(--dark); font-size: 15px; cursor: pointer; border-bottom: 1px solid #f0f0f0;
}
.menu-item i { width: 35px; color: var(--grey); font-size: 18px; }
/* --- DETAILS OVERLAY --- */
.details-overlay {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
background: #fff; z-index: 140; transform: translateX(100%);
transition: 0.3s; display: flex; flex-direction: column;
}
.details-overlay.active { transform: translateX(0); }
.details-map { height: 35%; background: #ddd url('https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/OpenStreetMap_Logo_2011.svg/1200px-OpenStreetMap_Logo_2011.svg.png') center/cover; position: relative; }
.back-btn-float {
position: absolute; top: 20px; left: 20px; width: 40px; height: 40px;
background: white; border-radius: 50%; display: flex;
align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}
.details-body { flex: 1; padding: 25px; border-radius: 25px 25px 0 0; background: white; margin-top: -20px; position: relative; display: flex; flex-direction: column; }
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin-top: 20px; }
.info-box { background: #f8f9fa; padding: 15px; border-radius: 10px; text-align: center; }
.info-label { font-size: 10px; color: #888; text-transform: uppercase; letter-spacing: 1px; }
.info-val { font-size: 14px; font-weight: 700; color: var(--dark); margin-top: 5px; }
/* ACTION BAR (Call & Book) */
.details-actions {
margin-top: auto;
display: flex; gap: 15px;
}
.btn-call {
width: 60px; height: 50px; background: #28a745; color: white;
border-radius: 12px; display: flex; align-items: center; justify-content: center;
font-size: 20px; text-decoration: none; box-shadow: 0 4px 10px rgba(40, 167, 69, 0.3);
}
.btn-book {
flex: 1; height: 50px; background: var(--dark); color: var(--primary);
border: none; border-radius: 12px; font-weight: 800; font-size: 14px;
display: flex; align-items: center; justify-content: center; gap: 10px;
cursor: pointer; box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}
.btn-cancel {
flex: 1; height: 50px; background: var(--danger); color: white;
border: none; border-radius: 12px; font-weight: 800; font-size: 14px;
display: flex; align-items: center; justify-content: center; gap: 10px;
cursor: pointer; box-shadow: 0 4px 10px rgba(220, 53, 69, 0.3);
}
/* Loader */
#loader {
position: absolute; top:0; left:0; width:100%; height:100%;
background: rgba(0,0,0,0.8); z-index: 999;
display: flex; justify-content: center; align-items: center;
}
.spinner {
width: 40px; height: 40px; border: 4px solid #f3f3f3;
border-top: 4px solid var(--primary); border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
</style>
</head>
<body>
<div class="app-container">
<!-- LOADER -->
<div id="loader" class="hidden"><div class="spinner"></div></div>
<!-- === 1. AUTH SCREEN === -->
<div id="authScreen" class="auth-screen">
<div style="text-align: center; margin-bottom: 20px; color: white;">
<h1 style="font-weight: 800; letter-spacing: -1px;">Rickshaw<span style="color:var(--primary)">Go</span></h1>
</div>
<div class="role-switch-container">
<div id="tab-passenger" class="role-btn active" onclick="switchRoleTab('passenger')">
<i class="fas fa-user"></i> Passenger
</div>
<div id="tab-driver" class="role-btn" onclick="switchRoleTab('driver')">
<i class="fas fa-taxi"></i> Driver
</div>
</div>
<div class="auth-card">
<h2 id="auth-title-text" style="text-align:center; margin-bottom:20px;">Welcome Back</h2>
<form onsubmit="handleAuth(event)">
<input type="email" id="email" class="auth-input" placeholder="Email Address" required>
<input type="password" id="password" class="auth-input" placeholder="Password" required>
<button type="submit" id="main-auth-btn" class="action-btn">LOGIN</button>
</form>
<div class="google-btn" onclick="handleGoogleSignIn()">
<img src="https://www.gstatic.com/firebasejs/ui/2.0.0/images/auth/google.svg" width="18" alt="G">
Continue with Google
</div>
<div style="text-align:center; margin-top:20px; font-size:13px;">
<span id="toggle-link" style="color:var(--dark); font-weight:bold; cursor:pointer; text-decoration:underline;" onclick="toggleAuthMode()">Create New Account</span>
</div>
</div>
</div>
<!-- === 2. DRIVER DASHBOARD === -->
<div id="driverScreen" class="driver-screen hidden">
<div class="header" style="background:var(--dark); color:var(--primary);">
<div class="menu-btn" style="color:white;" onclick="openDrawer()"><i class="fas fa-bars"></i></div>
<div class="app-title" style="color:white;">Driver<span style="color:var(--primary)">Board</span></div>
<i class="fas fa-history" style="color:white; cursor:pointer;" onclick="openHistoryScreen()"></i>
</div>
<div style="padding: 25px; flex: 1; overflow-y: auto;">
<div style="background: white; padding: 20px; border-radius: 15px; box-shadow: var(--shadow);">
<h3 style="margin-bottom: 15px;">Create New Trip</h3>
<form onsubmit="publishDriverTrip(event)">
<input type="text" id="d-name" class="auth-input" placeholder="Your Name" required>
<input type="tel" id="d-phone" class="auth-input" placeholder="Your Phone Number" required>
<input type="text" id="d-number" class="auth-input" placeholder="Vehicle Number (KA-01...)" required>
<input type="text" id="d-location" class="auth-input" placeholder="Current Location" required>
<input type="number" id="d-price" class="auth-input" placeholder="Price (₹)" required>
<button type="submit" class="action-btn">GO ONLINE</button>
</form>
</div>
</div>
</div>
<!-- === 3. PASSENGER HOME === -->
<div id="passengerScreen" class="passenger-screen hidden">
<div class="header">
<div class="menu-btn" onclick="openDrawer()"><i class="fas fa-bars"></i></div>
<div class="app-title">Rickshaw<span style="color:var(--primary)">Go</span></div>
<div class="location-icon-btn"><i class="fas fa-map-marker-alt"></i></div>
</div>
<div class="content-area">
<div style="background: linear-gradient(135deg, #212529, #343a40); padding: 20px; border-radius: 15px; color: white; margin-bottom: 25px; display:flex; justify-content:space-between; align-items:center;">
<div>
<div style="font-size:12px; color:var(--primary);">NEW OFFER</div>
<h2 style="font-size:18px;">Get 50% Off</h2>
<p style="font-size:11px; opacity:0.7;">On your first 3 EV rides</p>
</div>
<i class="fas fa-bolt" style="font-size:40px; color:var(--primary); opacity:0.8;"></i>
</div>
<div class="section-header" style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
<div style="font-size: 16px; font-weight: 700; color: var(--dark);">Available Rickshaws</div>
<div style="font-size:11px; color:grey;"><i class="fas fa-sync-alt"></i> Live</div>
</div>
<div id="trip-list-container">
<p style="text-align:center; color:gray; font-size:12px; margin-top:20px;">Scanning for drivers...</p>
</div>
</div>
<div class="bottom-nav">
<div class="nav-btn active" onclick="closeHistoryScreen()"><i class="fas fa-home"></i> Home</div>
<div class="nav-btn"><i class="fas fa-compass"></i> Explore</div>
<div class="nav-btn" onclick="openHistoryScreen()"><i class="fas fa-clipboard-list"></i> Activity</div>
</div>
</div>
<!-- === 4. HISTORY SCREEN === -->
<div id="historyScreen" class="history-screen">
<div class="header">
<div class="menu-btn" onclick="closeHistoryScreen()"><i class="fas fa-arrow-left"></i></div>
<div class="app-title">Trip History</div>
<div style="width:35px;"></div>
</div>
<div class="content-area" id="history-list-container">
<!-- Javascript fills this -->
</div>
</div>
<!-- === DRAWER === -->
<div class="drawer-overlay" id="drawerOverlay" onclick="closeDrawer()"></div>
<div class="nav-drawer" id="navDrawer">
<div class="drawer-profile">
<div class="profile-row" style="display:flex; gap:15px; align-items:center;">
<div style="width:50px; height:50px; background:#555; border-radius:50%;"></div>
<div>
<h3 id="drawer-user-name" style="font-size:16px;">User</h3>
<p id="drawer-user-email" style="font-size:11px; opacity:0.7;">user@example.com</p>
</div>
</div>
</div>
<div style="padding: 20px;">
<div class="menu-item" onclick="openHistoryScreen(); closeDrawer();"><i class="fas fa-history"></i> Your Rides</div>
<div class="menu-item"><i class="fas fa-wallet"></i> Payment</div>
<div class="menu-item"><i class="fas fa-headset"></i> Support</div>
<div class="menu-item" onclick="logoutUser()" style="color: #dc3545; border:none; margin-top:20px;">
<i class="fas fa-sign-out-alt" style="color:#dc3545;"></i> Logout
</div>
</div>
</div>
<!-- === DETAILS OVERLAY === -->
<div class="details-overlay" id="detailsScreen">
<div class="details-map">
<div class="back-btn-float" onclick="closeDetails()"><i class="fas fa-arrow-left"></i></div>
</div>
<div class="details-body">
<div style="display:flex; justify-content:space-between; align-items:center;">
<h2 id="dt-name" style="margin:0;">Driver Name</h2>
<h2 id="dt-price" style="margin:0; color:var(--dark);">₹0</h2>
</div>
<p style="color:grey; font-size:12px;">Verified Driver • <i class="fas fa-star" style="color:orange;"></i> 4.8</p>
<div class="info-grid">
<div class="info-box">
<div class="info-label">Vehicle No</div>
<div class="info-val" id="dt-vehicle">KA--</div>
</div>
<div class="info-box">
<div class="info-label">Vehicle Type</div>
<div class="info-val">Auto Rickshaw</div>
</div>
</div>
<div style="margin-top:20px; padding:15px; border-radius:10px; border:1px solid #eee;">
<div class="info-label">CURRENT LOCATION</div>
<div class="info-val" id="dt-loc" style="font-size:16px; display:flex; align-items:center; gap:10px;">
<i class="fas fa-map-pin" style="color:var(--danger);"></i> <span>...</span>
</div>
</div>
<!-- ACTIONS -->
<div class="details-actions" id="details-action-area">
<!-- Javascript populates buttons here based on status -->
</div>
</div>
</div>
</div>
<!-- === JAVASCRIPT === -->
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, onAuthStateChanged, GoogleAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js";
import { getFirestore, doc, setDoc, getDoc, collection, addDoc, query, where, onSnapshot, updateDoc, deleteDoc, deleteField } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js";
// CONFIG
const firebaseConfig = {
apiKey: "AIzaSyCdHRVDb7x_QDujZLmH4uuE2tqLOD5eziE",
authDomain: "achik-travel.firebaseapp.com",
projectId: "achik-travel",
storageBucket: "achik-travel.firebasestorage.app",
messagingSenderId: "463823387447",
appId: "1:463823387447:web:332f1f3dba246f0ecb78b2",
measurementId: "G-KB51NKQYTP"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
// STATE
let currentRole = 'passenger';
let isLoginMode = true;
let selectedTripData = null;
// --- AUTH ---
window.switchRoleTab = (role) => {
currentRole = role;
document.getElementById('tab-passenger').classList.toggle('active', role === 'passenger');
document.getElementById('tab-driver').classList.toggle('active', role === 'driver');
};
window.toggleAuthMode = () => {
isLoginMode = !isLoginMode;
document.getElementById('main-auth-btn').innerText = isLoginMode ? "LOGIN" : "REGISTER";
document.getElementById('auth-title-text').innerText = isLoginMode ? "Welcome Back" : "Join RickshawGo";
};
window.handleAuth = async (e) => {
e.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
showLoader(true);
try {
if (isLoginMode) {
await signInWithEmailAndPassword(auth, email, password);
} else {
const cred = await createUserWithEmailAndPassword(auth, email, password);
await setDoc(doc(db, "users", cred.user.uid), { email, role: currentRole });
}
} catch (err) { alert(err.message); showLoader(false); }
};
window.handleGoogleSignIn = async () => {
showLoader(true);
try {
const provider = new GoogleAuthProvider();
const res = await signInWithPopup(auth, provider);
const userDoc = await getDoc(doc(db, "users", res.user.uid));
if(!userDoc.exists()) {
await setDoc(doc(db, "users", res.user.uid), { email: res.user.email, role: currentRole });
}
} catch(err) { alert(err.message); showLoader(false); }
};
window.logoutUser = () => signOut(auth);
onAuthStateChanged(auth, async (user) => {
if (user) {
const docSnap = await getDoc(doc(db, "users", user.uid));
const role = docSnap.exists() ? docSnap.data().role : 'passenger';
currentRole = role;
document.getElementById('authScreen').classList.add('hidden');
document.getElementById('drawer-user-name').innerText = user.displayName || "User";
document.getElementById('drawer-user-email').innerText = user.email;
if (role === 'driver') {
document.getElementById('driverScreen').classList.remove('hidden');
} else {
document.getElementById('passengerScreen').classList.remove('hidden');
subscribeToTrips();
}
showLoader(false);
} else {
document.getElementById('authScreen').classList.remove('hidden');
document.getElementById('passengerScreen').classList.add('hidden');
document.getElementById('driverScreen').classList.add('hidden');
showLoader(false);
}
});
// --- DRIVER ---
window.publishDriverTrip = async (e) => {
e.preventDefault();
const name = document.getElementById('d-name').value;
const phone = document.getElementById('d-phone').value;
const number = document.getElementById('d-number').value;
const loc = document.getElementById('d-location').value;
const price = document.getElementById('d-price').value;
showLoader(true);
try {
await addDoc(collection(db, "trips"), {
driverId: auth.currentUser.uid,
driverName: name,
driverPhone: phone,
vehicleNumber: number,
location: loc,
price: price,
status: 'active',
createdAt: Date.now()
});
alert("Trip Published Successfully!");
document.getElementById('d-price').value = '';
showLoader(false);
openHistoryScreen();
} catch (err) { alert(err.message); showLoader(false); }
};
// --- PASSENGER: DISPLAY TRIPS ---
let unsubscribeTrips = null;
function subscribeToTrips() {
if(unsubscribeTrips) unsubscribeTrips();
const q = query(collection(db, "trips"), where("status", "==", "active"));
unsubscribeTrips = onSnapshot(q, (snapshot) => {
const container = document.getElementById('trip-list-container');
container.innerHTML = '';
if(snapshot.empty) {
container.innerHTML = '<p style="text-align:center; margin-top:20px; color:grey;">No drivers available currently.</p>';
return;
}
snapshot.forEach(docSnap => {
const data = docSnap.data();
const id = docSnap.id;
const timeAgo = getTimeAgo(data.createdAt);
const dataString = JSON.stringify({id, ...data}).replace(/"/g, """);
const html = `
<div class="trip-layout-card" onclick="openDetailsFromObj(${dataString})">
<div class="trip-header">
<div class="trip-driver-box">
<div class="trip-avatar"><i class="fas fa-taxi"></i></div>
<div>
<div class="trip-name">${data.driverName}</div>
<div class="trip-vehicle">${data.vehicleNumber}</div>
</div>
</div>
<div class="trip-price">
<div class="trip-amount">₹${data.price}</div>
<div class="trip-est">FIXED</div>
</div>
</div>
<div class="trip-route">
<i class="fas fa-circle route-icon"></i>
<div class="route-text">Current: ${data.location}</div>
</div>
<div class="trip-footer">
<div class="trip-time"><i class="far fa-clock"></i> Posted ${timeAgo}</div>
<div style="color:var(--primary-dark); font-weight:bold;">Tap to Book</div>
</div>
</div>
`;
container.innerHTML += html;
});
});
}
// --- HELPERS ---
function getTimeAgo(timestamp) {
const seconds = Math.floor((Date.now() - timestamp) / 1000);
if (seconds < 60) return "Just now";
const minutes = Math.floor(seconds / 60);
return minutes < 60 ? `${minutes}m ago` : `${Math.floor(minutes/60)}h ago`;
}
// --- BOOKING & DETAILS (WITH CANCEL) ---
window.openDetailsFromObj = (data) => {
selectedTripData = data;
// Populate Fields
document.getElementById('dt-name').innerText = data.driverName;
document.getElementById('dt-price').innerText = '₹' + data.price;
document.getElementById('dt-loc').innerText = data.location;
document.getElementById('dt-vehicle').innerText = data.vehicleNumber;
// Configure Actions Area
const actionArea = document.getElementById('details-action-area');
const currentUser = auth.currentUser ? auth.currentUser.uid : null;
const isMyBooking = (data.status === 'booked' && data.passengerId === currentUser);
// Call Button (Always visible but logic changes slightly)
const callBtnHtml = `
<a href="${data.driverPhone ? 'tel:'+data.driverPhone : '#'}" class="btn-call" onclick="${!data.driverPhone ? "alert('No phone'); return false;" : ""}">
<i class="fas fa-phone-alt"></i>
</a>
`;
if (isMyBooking) {
// SHOW CANCEL BUTTON
actionArea.innerHTML = `
${callBtnHtml}
<button class="btn-cancel" onclick="cancelBooking('${data.id}')">
CANCEL BOOKING <i class="fas fa-times-circle"></i>
</button>
`;
} else {
// SHOW BOOK BUTTON
actionArea.innerHTML = `
${callBtnHtml}
<button class="btn-book" onclick="bookTripWithLocation()">
SHARE LOC & BOOK <i class="fas fa-paper-plane"></i>
</button>
`;
}
document.getElementById('detailsScreen').classList.add('active');
};
// BOOKING LOGIC
window.bookTripWithLocation = async () => {
if(!selectedTripData) return;
const btn = document.querySelector('.btn-book');
btn.innerHTML = 'Booking... <div class="spinner" style="width:15px; height:15px; border-width:2px;"></div>';
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(async (position) => {
const lat = position.coords.latitude;
const lng = position.coords.longitude;
const mapLink = `https://www.google.com/maps?q=${lat},${lng}`;
performBooking(mapLink);
}, (error) => {
alert("Location denied. Booking anyway.");
performBooking(null);
});
} else {
performBooking(null);
}
};
async function performBooking(locLink) {
try {
const tripRef = doc(db, "trips", selectedTripData.id);
await updateDoc(tripRef, {
status: 'booked',
passengerId: auth.currentUser.uid,
passengerLocation: locLink,
bookedAt: Date.now()
});
alert("Booking Confirmed!");
closeDetails();
openHistoryScreen();
} catch(err) { alert(err.message); }
}
// CANCELLATION LOGIC
window.cancelBooking = async (tripId) => {
if(!confirm("Are you sure you want to cancel this booking?")) return;
showLoader(true);
try {
const tripRef = doc(db, "trips", tripId);
// Reset trip to active and remove passenger data
await updateDoc(tripRef, {
status: 'active',
passengerId: deleteField(),
passengerLocation: deleteField(),
bookedAt: deleteField()
});
alert("Booking Cancelled.");
closeDetails();
openHistoryScreen(); // Will refresh history list
} catch(err) {
alert("Error cancelling: " + err.message);
} finally {
showLoader(false);
}
};
// --- HISTORY ---
window.openHistoryScreen = async () => {
document.getElementById('historyScreen').classList.add('active');
const container = document.getElementById('history-list-container');
container.innerHTML = '<div style="text-align:center; margin-top:20px;"><div class="spinner"></div></div>';
const uid = auth.currentUser.uid;
let q;
if (currentRole === 'driver') {
q = query(collection(db, "trips"), where("driverId", "==", uid));
} else {
q = query(collection(db, "trips"), where("passengerId", "==", uid));
}
onSnapshot(q, (snapshot) => {
container.innerHTML = '';
if(snapshot.empty) {
container.innerHTML = '<p style="text-align:center; margin-top:20px; color:grey;">No history found.</p>';
return;
}
snapshot.forEach(docSnap => {
const data = docSnap.data();
const id = docSnap.id;
const isDriver = currentRole === 'driver';
const badgeClass = data.status === 'active' ? 'status-active' : 'status-booked';
const badgeColor = data.status === 'active' ? '#e3f2fd' : '#d4edda';
const badgeText = data.status === 'active' ? '#0d47a1' : '#155724';
// PASS DATA for clicking
const dataString = JSON.stringify({id, ...data}).replace(/"/g, """);
let actions = '';
if(isDriver) {
if(data.status === 'active') {
actions = `<button style="padding:5px 10px; background:red; color:white; border:none; border-radius:5px; margin-top:10px; font-size:12px;" onclick="deleteTrip('${id}')">Delete</button>`;
} else if(data.status === 'booked' && data.passengerLocation) {
actions = `
<div style="margin-top:10px; padding-top:10px; border-top:1px solid #eee;">
<a href="${data.passengerLocation}" target="_blank" style="display:inline-block; padding:8px 12px; background:var(--dark); color:var(--primary); text-decoration:none; border-radius:5px; font-size:12px; font-weight:bold;">
<i class="fas fa-map-marked-alt"></i> View Pax Location
</a>
</div>
`;
}
} else {
// Passenger actions inside history list
if(data.status === 'booked') {
actions = `
<div style="margin-top:10px; display:flex; gap:10px;">
<button onclick="openDetailsFromObj(${dataString})" style="flex:1; padding:8px; border:1px solid #ddd; background:white; border-radius:5px; cursor:pointer;">View/Call</button>
<button onclick="cancelBooking('${id}')" style="flex:1; padding:8px; background:#dc3545; color:white; border:none; border-radius:5px; cursor:pointer;">Cancel</button>
</div>
`;
}
}
const html = `
<div class="trip-layout-card" style="border-left: 5px solid ${data.status === 'active' ? 'orange' : 'green'}; cursor:default;">
<div class="trip-header">
<div>
<div class="trip-name">${data.location}</div>
<div class="trip-vehicle">${isDriver ? 'My Trip' : data.driverName}</div>
</div>
<div style="text-align:right;">
<div class="trip-amount">₹${data.price}</div>
<span style="background:${badgeColor}; color:${badgeText}; padding:2px 6px; border-radius:4px; font-size:10px; font-weight:bold;">${data.status.toUpperCase()}</span>
</div>
</div>
<div style="font-size:11px; color:grey;">Date: ${new Date(data.createdAt).toLocaleDateString()}</div>
${actions}
</div>
`;
container.innerHTML += html;
});
});
};
window.closeHistoryScreen = () => { document.getElementById('historyScreen').classList.remove('active'); };
window.deleteTrip = async (id) => { if(confirm("Delete?")) await deleteDoc(doc(db, "trips", id)); };
// UI Helpers
window.openDrawer = () => { document.getElementById('navDrawer').classList.add('active'); document.getElementById('drawerOverlay').classList.add('active'); }
window.closeDrawer = () => { document.getElementById('navDrawer').classList.remove('active'); document.getElementById('drawerOverlay').classList.remove('active'); }
window.closeDetails = () => { document.getElementById('detailsScreen').classList.remove('active'); }
function showLoader(show) { document.getElementById('loader').classList.toggle('hidden', !show); }
</script>
</body>
</html>