-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorders.js
More file actions
264 lines (232 loc) Β· 9.36 KB
/
orders.js
File metadata and controls
264 lines (232 loc) Β· 9.36 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
/* ============================================
BUILDEASY β Delivery Tracking Scripts
Leaflet Map + Simulated GPS + Role Switching
============================================ */
/* -------- SIMULATED ROUTE (Chennai area) -------- */
const routeCoords = [
[13.0827, 80.2707], // Start: Warehouse (Egmore)
[13.0775, 80.2620],
[13.0720, 80.2560],
[13.0680, 80.2510],
[13.0640, 80.2460],
[13.0580, 80.2400],
[13.0520, 80.2350],
[13.0478, 80.2310],
[13.0440, 80.2268],
[13.0405, 80.2350],
[13.0380, 80.2410],
[13.0355, 80.2425],
[13.0340, 80.2440], // Destination: T. Nagar
];
const destination = routeCoords[routeCoords.length - 1];
let currentStep = 0;
let map, truckMarker, routeLine, destMarker;
let refreshInterval;
let currentView = 'customer';
/* -------- LEAFLET MAP INIT -------- */
document.addEventListener('DOMContentLoaded', () => {
initMap();
startTracking();
});
function initMap() {
map = L.map('trackMap', {
center: [13.0600, 80.2450],
zoom: 13,
zoomControl: false,
attributionControl: false
});
// Dark tile layer
L.tileLayer('https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}{r}.png', {
maxZoom: 19,
}).addTo(map);
// Zoom control bottom-right
L.control.zoom({ position: 'topright' }).addTo(map);
// Route polyline
routeLine = L.polyline(routeCoords, {
color: '#f97316',
weight: 4,
opacity: 0.7,
dashArray: '10 6',
}).addTo(map);
// Traveled portion polyline
window.traveledLine = L.polyline([], {
color: '#10b981',
weight: 5,
opacity: 0.9,
}).addTo(map);
// Destination marker
const destIcon = L.divIcon({
html: '<div style="font-size:28px;filter:drop-shadow(0 2px 6px rgba(0,0,0,0.4));">π</div>',
className: '',
iconSize: [28, 28],
iconAnchor: [14, 28],
});
destMarker = L.marker(destination, { icon: destIcon }).addTo(map)
.bindPopup('<b>Project Site</b><br>T. Nagar, Chennai');
// Warehouse marker
const warehouseIcon = L.divIcon({
html: '<div style="font-size:24px;filter:drop-shadow(0 2px 6px rgba(0,0,0,0.4));">π</div>',
className: '',
iconSize: [24, 24],
iconAnchor: [12, 24],
});
L.marker(routeCoords[0], { icon: warehouseIcon }).addTo(map)
.bindPopup('<b>BuildEasy Warehouse</b><br>Egmore, Chennai');
// Truck marker
const truckIcon = L.divIcon({
html: '<div style="font-size:30px;filter:drop-shadow(0 3px 8px rgba(249,115,22,0.5));transition:transform 0.8s ease;">π</div>',
className: '',
iconSize: [30, 30],
iconAnchor: [15, 15],
});
truckMarker = L.marker(routeCoords[0], { icon: truckIcon, zIndexOffset: 1000 }).addTo(map);
// Fit map to route
map.fitBounds(routeLine.getBounds().pad(0.15));
// Admin extra trucks (hidden by default)
window.adminMarkers = [];
const adminTrucks = [
{ pos: [13.0900, 80.2300], id: 'TN-01-CD-5678', driver: 'Arun' },
{ pos: [13.0300, 80.2600], id: 'TN-01-EF-9012', driver: 'Venkat' },
];
adminTrucks.forEach(t => {
const icon = L.divIcon({
html: '<div style="font-size:24px;opacity:0.7;">π</div>',
className: '', iconSize: [24, 24], iconAnchor: [12, 12],
});
const m = L.marker(t.pos, { icon })
.bindPopup(`<b>${t.driver}</b><br>${t.id}`);
window.adminMarkers.push(m);
});
}
/* -------- GPS SIMULATION -------- */
function startTracking() {
refreshInterval = setInterval(() => {
if (currentStep < routeCoords.length - 1) {
currentStep++;
moveTruck(currentStep);
} else {
clearInterval(refreshInterval);
markDelivered();
}
}, 5000);
}
function moveTruck(step) {
const pos = routeCoords[step];
truckMarker.setLatLng(pos);
map.panTo(pos, { animate: true, duration: 0.8 });
// Update traveled line
const traveled = routeCoords.slice(0, step + 1);
window.traveledLine.setLatLngs(traveled);
// Update stats
const remaining = routeCoords.length - 1 - step;
const distKm = (remaining * 0.45).toFixed(1);
const etaMin = Math.max(1, remaining * 1.5).toFixed(0);
const speed = (25 + Math.random() * 20).toFixed(0);
document.getElementById('statDist').textContent = distKm + ' km';
document.getElementById('driverSpeed').textContent = speed;
document.getElementById('driverDist').textContent = distKm;
document.getElementById('driverEta').textContent = etaMin;
document.getElementById('etaMin').textContent = `~${etaMin} min`;
// Calculate ETA time
const now = new Date();
now.setMinutes(now.getMinutes() + parseInt(etaMin));
const etaStr = now.toLocaleTimeString('en-IN', { hour: 'numeric', minute: '2-digit', hour12: true });
document.getElementById('etaTime').textContent = etaStr;
document.getElementById('aiEta').textContent = etaStr;
// Update AI delay risk based on progress
const delayEl = document.getElementById('aiDelay');
if (remaining > 8) {
delayEl.textContent = 'Medium';
delayEl.className = 'ai-val medium';
} else if (remaining > 3) {
delayEl.textContent = 'Low';
delayEl.className = 'ai-val low';
} else {
delayEl.textContent = 'Very Low';
delayEl.className = 'ai-val low';
}
// Update timeline β move to "Arriving Soon" when close
if (remaining <= 3 && remaining > 0) {
const items = document.querySelectorAll('.tl-item');
items[3].classList.remove('current');
items[3].classList.add('done');
items[3].querySelector('h5').textContent = 'β Out for Delivery';
items[4].classList.add('current');
}
}
function markDelivered() {
const items = document.querySelectorAll('.tl-item');
items.forEach(i => { i.classList.remove('current'); i.classList.add('done'); });
items[5].querySelector('h5').textContent = 'β Delivered';
items[5].querySelector('.tl-time').textContent = new Date().toLocaleTimeString('en-IN', { hour: 'numeric', minute: '2-digit', hour12: true });
items[4].querySelector('h5').textContent = 'β Arrived';
document.getElementById('etaBadge').innerHTML = 'β
<strong>Delivered!</strong> β’ Order completed';
document.getElementById('etaBadge').style.background = 'linear-gradient(135deg, #10b981, #059669)';
showToast('π¦ Delivery completed successfully!', 'success');
}
/* -------- ROLE VIEW SWITCH -------- */
function setView(view) {
currentView = view;
const btns = document.querySelectorAll('.topbar-center .view-btn');
btns.forEach(b => b.classList.remove('active'));
event.target.classList.add('active');
const customerActions = document.getElementById('customerActions');
const adminActions = document.getElementById('adminActions');
if (view === 'admin') {
customerActions.style.display = 'none';
adminActions.style.display = 'block';
document.getElementById('statTrucks').textContent = '3';
document.getElementById('statOrders').textContent = '8';
// Show extra trucks on map
window.adminMarkers.forEach(m => m.addTo(map));
showToast('π‘οΈ Admin view activated β 3 trucks visible', 'info');
} else {
customerActions.style.display = 'block';
adminActions.style.display = 'none';
document.getElementById('statTrucks').textContent = '1';
document.getElementById('statOrders').textContent = '3';
// Remove extra trucks
window.adminMarkers.forEach(m => m.remove());
showToast('π€ Customer view activated', 'info');
}
}
/* -------- ACTIONS -------- */
function callDriver() {
showToast('π Calling Sundar Murugan...', 'info');
}
function shareTracking() {
const url = window.location.href + '?track=ORD-2041';
navigator.clipboard.writeText(url).then(() => {
showToast('π Tracking link copied to clipboard!', 'success');
}).catch(() => {
showToast('π Link: ' + url, 'info');
});
}
function cancelDelivery() {
if (currentStep >= 3) {
showToast('β Cannot cancel β order is already dispatched', 'error');
} else {
showToast('Order cancelled. Refund will be processed.', 'success');
}
}
function updateStatus() {
const statuses = ['Confirmed', 'Packed', 'Out for Delivery', 'Arriving Soon', 'Delivered'];
const next = statuses[Math.min(currentStep + 1, statuses.length - 1)];
showToast(`π Status updated β ${next}`, 'success');
}
function refreshLocation() {
showToast('π Refreshing GPS location...', 'info');
if (currentStep < routeCoords.length - 1) {
currentStep++;
moveTruck(currentStep);
}
}
/* -------- TOAST -------- */
function showToast(msg, type = 'info') {
const c = document.getElementById('toastContainer');
const t = document.createElement('div');
t.className = `toast ${type}`;
t.innerHTML = `<span>${type === 'success' ? 'β' : type === 'error' ? 'β' : 'βΉ'}</span> ${msg}`;
c.appendChild(t);
setTimeout(() => t.remove(), 3000);
}