Repository navigation
Expand file tree
/
Copy pathadmin.html
More file actions
387 lines (349 loc) · 12 KB
/
Copy pathadmin.html
File metadata and controls
387 lines (349 loc) · 12 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Admin Dashboard</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header class="top">
<button id="themeToggle">🌙 Dark Mode</button>
<h1>Admin — Flights</h1>
<div>
<button id="btnHome">Search Flights</button>
<button id="btnLogout">Logout</button>
</div>
</header>
<main class="container">
<section class="card">
<h2>Create / Edit Flight</h2>
<form id="flightForm">
<input type="hidden" name="id" />
<label
>Airline
<select name="airline" required>
<option>IndiGo</option>
<option>Air India</option>
<option>Vistara</option>
<option>SpiceJet</option>
<option>Go First</option>
<option>Emirates</option>
<option>Qatar Airways</option>
<option>Singapore Airlines</option>
</select>
</label>
<label
>Aircraft Type
<select name="aircraftType" id="aircraftType" required>
<option value="">Select Type</option>
</select>
</label>
<label>Source <input name="source" required /></label>
<label>Destination <input name="destination" required /></label>
<label
>Departure (datetime-local)
<input name="departureTime" type="datetime-local" required />
</label>
<label
>Arrival (datetime-local)
<input name="arrivalTime" type="datetime-local" required />
</label>
<label
>Fare <input name="fare" type="number" step="0.01" required
/></label>
<label
>Seats Available
<input
name="seatsAvailable"
id="seatsAvailable"
type="number"
required
readonly
/>
</label>
<label>Seat Map Preview</label>
<div id="seatPreview" class="seat-preview">
<p>Select an aircraft type to see the layout.</p>
</div>
<div class="row">
<button type="submit">Save</button>
<button type="button" id="btnReset">Reset</button>
</div>
</form>
</section>
<section class="card">
<h2>Flights</h2>
<div class="row">
<button id="btnGenerate">Generate Sample Flights</button>
<input
id="fetchIata"
placeholder="Departure IATA (e.g. DEL)"
/>
<button id="btnFetch">Fetch Real Flights</button>
</div>
<div id="adminFlights">Loading...</div>
</section>
<section class="card">
<h2>Reports</h2>
<pre id="reportsArea">Loading...</pre>
</section>
</main>
<script src="app.js"></script>
<script>
ensureAuth();
document.getElementById("btnLogout").addEventListener("click", logout);
document
.getElementById("btnHome")
.addEventListener("click", () => (location.href = "flights.html"));
const flightForm = document.getElementById("flightForm");
flightForm.addEventListener("submit", async (e) => {
e.preventDefault();
const data = Object.fromEntries(new FormData(flightForm));
const payload = {
airline: data.airline,
aircraftType: data.aircraftType,
source: data.source,
destination: data.destination,
departureTime: new Date(data.departureTime).toISOString(),
arrivalTime: new Date(data.arrivalTime).toISOString(),
fare: Number(data.fare),
seatsAvailable: Number(data.seatsAvailable),
};
try {
if (data.id) {
await apiPut("/admin/flights/" + data.id, payload);
alert("Updated");
} else {
await apiPost("/admin/flights", payload);
alert("Created");
}
resetForm();
loadFlights();
loadReports();
} catch (err) {
alert("Save failed: " + (err.message || JSON.stringify(err)));
}
});
document.getElementById("btnReset").addEventListener("click", resetForm);
function resetForm() {
flightForm.reset();
flightForm.id.value = "";
}
let aircraftMetadata = {};
const aircraftTypeSelect = document.getElementById("aircraftType");
const seatsAvailableInput = document.getElementById("seatsAvailable");
const seatPreview = document.getElementById("seatPreview");
async function loadAircraftMetadata() {
try {
aircraftMetadata = await apiGet("/flights/aircraft-metadata");
console.log("Loaded aircraft metadata:", aircraftMetadata);
} catch (err) {
console.error("Failed to fetch aircraft metadata:", err);
}
//Load data into aircraftType
for (const type in aircraftMetadata) {
const option = document.createElement("option");
option.value = type;
option.textContent = `${type} (${aircraftMetadata[type].totalSeats} seats)`;
aircraftTypeSelect.appendChild(option);
}
}
aircraftTypeSelect.addEventListener("change", () => {
const type = aircraftTypeSelect.value;
const layout = aircraftMetadata[type];
if (!layout) {
seatsAvailableInput.value = "";
seatPreview.innerHTML =
"<p>Select an aircraft type to see the layout.</p>";
return;
}
// Update seat count
seatsAvailableInput.value = layout.totalSeats;
// Generate seat preview grid
let html = "";
for (let r = 1; r <= layout.rows; r++) {
html += `<div class="seat-row"><span class="row-label">${r}</span>`;
for (const c of layout.cols) {
html += `<div class="seat">${r}${c}</div>`;
}
html += `</div>`;
}
seatPreview.innerHTML = html;
});
async function loadFlights() {
const flights = await apiGet("/flights");
if (!flights || flights.length === 0) {
document.getElementById("adminFlights").innerHTML =
"<p>No flights.</p>";
return;
}
const html = flights
.map(
(f) => `
<div class="admin-flight">
<div><strong>${escapeHtml(f.airline)}</strong> ${escapeHtml(
f.source
)}→${escapeHtml(f.destination)}</div>
<div>Depart: ${formatDate(f.departureTime)}</div>
<div>Fare: ₹${f.fare} · Seats: ${f.seatsAvailable}</div>
<div>
<button class="btnEdit" data-id="${f.id}">Edit</button>
<button class="btnDel" data-id="${f.id}">Delete</button>
</div>
</div>`
)
.join("");
document.getElementById("adminFlights").innerHTML = html;
document.querySelectorAll(".btnEdit").forEach((b) =>
b.addEventListener("click", async (e) => {
const id = e.target.dataset.id;
const flights = await apiGet("/flights");
const f = flights.find((x) => String(x.id) === id);
if (!f) return alert("Flight not found");
flightForm.id.value = f.id;
flightForm.airline.value = f.airline;
flightForm.source.value = f.source;
flightForm.destination.value = f.destination;
flightForm.departureTime.value = toLocalInput(f.departureTime);
flightForm.arrivalTime.value = toLocalInput(f.arrivalTime);
flightForm.fare.value = f.fare;
flightForm.seatsAvailable.value = f.seatsAvailable;
window.scrollTo(0, 0);
})
);
document.querySelectorAll(".btnDel").forEach((b) =>
b.addEventListener("click", async (e) => {
if (!confirm("Delete flight?")) return;
const id = e.target.dataset.id;
try {
await apiDelete("/admin/flights/" + id);
alert("Deleted");
loadFlights();
loadReports();
} catch (err) {
alert("Delete failed");
}
})
);
}
async function loadReports() {
const rep = await apiGet("/admin/reports");
document.getElementById("reportsArea").textContent = JSON.stringify(
rep,
null,
2
);
}
document
.getElementById("btnGenerate")
.addEventListener("click", async () => {
const count = prompt("How many random flights to generate?", "20");
if (!count) return;
try {
const res = await apiPost(
`/admin/flights/generate?count=${encodeURIComponent(count)}`
);
alert(res);
loadFlights();
loadReports();
} catch (err) {
alert("Generation failed: " + (err.message || JSON.stringify(err)));
}
});
document
.getElementById("btnFetch")
.addEventListener("click", async () => {
const depIata =
document.getElementById("fetchIata").value.trim() || "DEL";
try {
const res = await apiPost(
`/admin/flights/fetch?depIata=${encodeURIComponent(depIata)}`
);
alert(res);
loadFlights();
loadReports();
} catch (err) {
alert("Fetch failed: " + (err.message || JSON.stringify(err)));
}
});
loadAircraftMetadata();
loadFlights();
loadReports();
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js"></script>
<script>
const themeToggle = document.getElementById("themeToggle");
// Load saved theme
if (localStorage.getItem("theme") === "dark") {
document.body.classList.add("dark");
themeToggle.textContent = "☀️ Light Mode";
}
// Toggle theme on click
themeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark");
const isDark = document.body.classList.contains("dark");
localStorage.setItem("theme", isDark ? "dark" : "light");
themeToggle.textContent = isDark ? "☀️ Light Mode" : "🌙 Dark Mode";
});
</script>
<canvas
id="ambientCanvas"
style="
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: -1;
"
></canvas>
<script>
const canvas = document.getElementById("ambientCanvas");
const ctx = canvas.getContext("2d");
let particles = [];
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener("resize", resizeCanvas);
// Generate particles
for (let i = 0; i < 50; i++) {
particles.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
r: Math.random() * 3 + 1,
dx: (Math.random() - 0.5) * 0.4,
dy: (Math.random() - 0.5) * 0.4,
});
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(255,255,255,0.85)";
particles.forEach((p) => {
ctx.beginPath();
ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2);
ctx.fill();
p.x += p.dx;
p.y += p.dy;
if (p.x < 0 || p.x > canvas.width) p.dx *= -1;
if (p.y < 0 || p.y > canvas.height) p.dy *= -1;
});
requestAnimationFrame(animate);
}
animate();
</script>
<script>
anime({
targets: ".card, .top",
opacity: [0, 1],
translateY: [20, 0],
duration: 900,
delay: anime.stagger(120),
easing: "easeOutQuint",
});
</script>
</body>
</html>