-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
368 lines (301 loc) · 11.4 KB
/
main.js
File metadata and controls
368 lines (301 loc) · 11.4 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
// ------------------------------
// FIREBASE IMPORTS
// ------------------------------
import {
doc,
setDoc,
updateDoc,
onSnapshot
} from "https://www.gstatic.com/firebasejs/11.1.0/firebase-firestore.js";
// ------------------------------
// CONSTANTS
// ------------------------------
const STORAGE_KEY = "SmartLaundryOrder";
const RATE_PER_KG = 0.5;
const UPI_ID = "ajeet212009@fam";
const MERCHANT_ADDRESS = "0x7e14c764065e7c0f96dfe365251ddd50f4a1940916c67e87464c08ce5f520d82";
const APT_RATE = 0.001;
// ------------------------------
// HELPERS
// ------------------------------
const saveOrder = (order) => localStorage.setItem(STORAGE_KEY, JSON.stringify(order));
const loadOrder = () => JSON.parse(localStorage.getItem(STORAGE_KEY) || "null");
const newOrderID = () => "ORD-" + Date.now();
const nowString = () => new Date().toLocaleString();
const disable = (btn) => { if (btn) { btn.disabled = true; btn.style.opacity = "0.5"; } };
function addTimeline(order, label) {
order.timeline.push({ label, time: nowString() });
}
// ------------------------------
// PAGE ROUTER
// ------------------------------
document.addEventListener("DOMContentLoaded", () => {
const page = document.body.dataset.page;
if (page === "step1") initStep1();
if (page === "step2") initStep2();
if (page === "step3") initStep3();
if (page === "step4") initStep4();
if (page === "locker") initLocker(); // 👈 locker page hook
});
// ------------------------------
// STEP 1 — ORDER CREATION
// ------------------------------
function initStep1() {
document.getElementById("btnSimWeight").addEventListener("click", () => {
const w = (Math.random() * 5 + 1).toFixed(2);
document.getElementById("weightInput").value = w;
document.getElementById("priceInput").value = (w * RATE_PER_KG).toFixed(0);
});
document.getElementById("btnStep1Next").addEventListener("click", async () => {
const weight = document.getElementById("weightInput").value;
if (!weight) return alert("⚠ Please simulate weight first.");
const order = {
id: newOrderID(),
name: document.getElementById("custName").value || "Guest",
phone: document.getElementById("custPhone").value,
email: document.getElementById("custEmail").value,
weightKg: weight,
priceInr: document.getElementById("priceInput").value,
status: "Created",
timeline: []
};
addTimeline(order, "Order Created");
saveOrder(order);
await setDoc(doc(window.db, "orders", order.id), order);
window.location.href = "payment.html";
});
}
// ------------------------------
// STEP 2 — PAYMENT PAGE
// ------------------------------
async function initStep2() {
const order = loadOrder();
if (!order) return window.location.href = "index.html";
document.getElementById("orderSummary").innerHTML = `
<p><strong>Order ID:</strong> ${order.id}</p>
<p><strong>Name:</strong> ${order.name}</p>
<p><strong>Total Amount:</strong> ₹${order.priceInr}</p>
`;
// UPI QR
const upiString = `upi://pay?pa=${UPI_ID}&pn=SmartLaundry&am=${order.priceInr}&cu=INR`;
document.getElementById("qrPayloadPreview").innerText = upiString;
document.getElementById("qrContainer").innerHTML = "";
new QRCode(document.getElementById("qrContainer"), {
text: upiString,
width: 220,
height: 220
});
// PETRA WALLET BUTTONS
const aptos = window.aptos;
const aptAmount = (order.priceInr * APT_RATE).toFixed(6);
const btnConnect = document.getElementById("btnConnectWallet");
const btnPayAptos = document.getElementById("btnPayWithAptos");
const btnManualVerify = document.getElementById("btnVerifyOnChain");
if (btnConnect) {
btnConnect.addEventListener("click", async () => {
if (!aptos) {
alert("Petra wallet not detected. Install Petra extension in desktop browser to use Aptos payments.");
return;
}
try {
await aptos.connect();
order.userWallet = await aptos.account();
saveOrder(order);
alert("Wallet Connected: " + order.userWallet.address);
disable(btnConnect);
if (btnPayAptos) btnPayAptos.disabled = false;
} catch {
alert("❌ Wallet connection failed");
}
});
}
if (btnPayAptos) {
btnPayAptos.addEventListener("click", async () => {
if (!aptos) {
alert("Petra wallet not detected.");
return;
}
if (!order.userWallet) return alert("Connect wallet first!");
const payload = {
type: "entry_function_payload",
function: "0x1::coin::transfer",
arguments: [MERCHANT_ADDRESS, Math.floor(aptAmount * 1e8)],
type_arguments: ["0x1::aptos_coin::AptosCoin"]
};
try {
const txHash = await aptos.signAndSubmitTransaction(payload);
order.aptosTxHash = txHash;
order.status = "Blockchain Payment Confirmed";
addTimeline(order, "Aptos Payment Successful");
await updateDoc(doc(window.db, "orders", order.id), {
status: order.status,
aptosTxHash: txHash,
blockchainAmountAPT: aptAmount,
walletAddress: order.userWallet.address,
paymentMethod: "UPI + Aptos",
timeline: order.timeline
});
saveOrder(order);
disable(btnPayAptos);
alert("Payment Success!\nTX: " + txHash);
window.open(`https://explorer.aptoslabs.com/txn/${txHash}?network=testnet`);
} catch {
alert("❌ Blockchain transaction failed");
}
});
}
if (btnManualVerify) {
btnManualVerify.addEventListener("click", () => {
alert("Manual verification coming soon. Use Aptos button or UPI for now.");
});
}
// Move to locker assignment, not directly to status
document.getElementById("btnStep2Next").addEventListener("click", () => {
window.location.href = "locker.html";
});
}
// ------------------------------
// LOCKER ASSIGNMENT PAGE
// ------------------------------
async function initLocker() {
const order = loadOrder();
if (!order) return window.location.href = "index.html";
const lockerBox = document.getElementById("lockerBox");
const btnNext = document.getElementById("btnLockerNext");
if (!lockerBox || !btnNext) return;
// If locker already assigned (user returns to page), just show it
if (order.locker && order.pickupCode) {
lockerBox.innerHTML = `
<p><strong>Locker:</strong> #${order.locker}</p>
<p><strong>Pickup Code:</strong> ${order.pickupCode}</p>
<small>Use this code at the locker station to collect your clothes.</small>
`;
btnNext.style.display = "inline-flex";
btnNext.onclick = () => window.location.href = "status.html";
return;
}
// Simple mock locker assignment (later you can get this from Firestore)
const availableLockers = [1, 2, 3, 4, 5];
const assignedLocker = availableLockers[Math.floor(Math.random() * availableLockers.length)];
const pickupCode = "PIN-" + Math.floor(1000 + Math.random() * 9000);
order.locker = assignedLocker;
order.pickupCode = pickupCode;
order.status = "Locker Assigned";
addTimeline(order, "Locker Assigned");
saveOrder(order);
try {
await updateDoc(doc(window.db, "orders", order.id), {
locker: assignedLocker,
pickupCode,
status: order.status,
timeline: order.timeline
});
} catch (e) {
console.error("Failed to update locker info in Firestore", e);
}
lockerBox.innerHTML = `
<p><strong>Locker Assigned:</strong> #${assignedLocker}</p>
<p><strong>Pickup Code:</strong> ${pickupCode}</p>
<small>Share this code with the customer. It will be required to unlock the locker.</small>
`;
btnNext.style.display = "inline-flex";
btnNext.onclick = () => window.location.href = "status.html";
}
// ------------------------------
// STEP 3 — LIVE STATUS + IoT SIMULATION
// ------------------------------
function initStep3() {
const order = loadOrder();
if (!order) return window.location.href = "index.html";
const timelineBox = document.getElementById("timeline");
const statusTitle = document.getElementById("statusTitle");
const btnFlow = document.getElementById("btnStartFlow");
const btnNext = document.getElementById("btnStep3Next");
// IoT UI elements
const iotStatus = document.getElementById("iotStatus");
const machineAnimation = document.getElementById("machineAnimation");
const simulateBtn = document.getElementById("simulateMachine");
function renderTimeline() {
timelineBox.innerHTML = order.timeline
.map(e => `<div class="timeline-item">${e.label}<div class="time">${e.time}</div></div>`)
.join("");
}
// IoT Simulation Logic
function updateMachineUI(state) {
if (!iotStatus || !machineAnimation) return;
machineAnimation.style.display = "none";
const states = {
"Paid": "🔓 Machine Unlocked — Load Clothes",
"Washing": "🌀 Washing... Machine Running",
"Ready": "🔔 Wash Complete — Collect Clothes",
"Collected": "💤 Machine Reset",
default: "🛑 Machine Idle"
};
iotStatus.innerHTML = states[state] || states.default;
if (state === "Washing") machineAnimation.style.display = "block";
}
if (simulateBtn) {
simulateBtn.addEventListener("click", () => updateMachineUI(order.status));
}
// Realtime Firestore Sync
onSnapshot(doc(window.db, "orders", order.id), (snap) => {
const data = snap.data();
if (!data) return;
Object.assign(order, data);
saveOrder(order);
statusTitle.textContent = `Status: ${order.status}`;
renderTimeline();
});
if (btnFlow) {
btnFlow.addEventListener("click", async () => {
disable(btnFlow);
const flow = ["Paid", "Washing", "Ready", "Collected"];
let i = 0;
const timer = setInterval(async () => {
if (i >= flow.length) {
clearInterval(timer);
if (btnNext) btnNext.disabled = false;
return;
}
order.status = flow[i];
addTimeline(order, flow[i]);
saveOrder(order);
await updateDoc(doc(window.db, "orders", order.id), {
status: order.status,
timeline: order.timeline
});
updateMachineUI(order.status);
i++;
}, 1600);
});
}
if (btnNext) {
btnNext.addEventListener("click", () => window.location.href = "summary.html");
}
renderTimeline();
}
// ------------------------------
// STEP 4 — SUMMARY PAGE
// ------------------------------
function initStep4() {
const order = loadOrder();
if (!order) return window.location.href = "index.html";
document.getElementById("summaryOrderId").textContent = order.id;
document.getElementById("summaryStatus").textContent = order.status;
document.getElementById("summaryPrice").textContent = `₹${order.priceInr}`;
const timelineEl = document.getElementById("summaryTimeline");
if (timelineEl) {
timelineEl.innerHTML = order.timeline
.map(e => `<div class="timeline-item">${e.label}<div class="time">${e.time}</div></div>`)
.join("");
}
// Optional: show locker + pickup code if you added elements in summary.html
const lockerEl = document.getElementById("summaryLocker");
const codeEl = document.getElementById("summaryCode");
if (lockerEl) lockerEl.textContent = order.locker || "Not assigned";
if (codeEl) codeEl.textContent = order.pickupCode || "N/A";
document.getElementById("btnNewOrder").addEventListener("click", () => {
localStorage.removeItem(STORAGE_KEY);
window.location.href = "index.html";
});
}