-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
689 lines (566 loc) · 25.1 KB
/
Copy pathscript.js
File metadata and controls
689 lines (566 loc) · 25.1 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
// ============================================
// LOCKET VIP SYSTEM - PROFESSIONAL VERSION
// VERSION: Optimized Frontend (FIXED UX)
// ============================================
// --- CONFIG & CONSTANTS ---
const WORKER_URL = "https://locket-vip.hungnguyen-junn.workers.dev";
const QR_BANK_ID = "MB";
const QR_ACCOUNT_NO = "09999999900";
const QR_ACCOUNT_NAME = "NGUYEN VAN HUNG";
const POLL_INTERVAL_MS = 5000; // 5 giây
let DYNAMIC_CONFIG = {
ACTIVATION_COST: 20000,
REFERRAL_REWARD: 5000,
};
let currentUsername = localStorage.getItem('locket_user') || null;
let pollTimer = null; // Đổi tên biến interval thành timer để dễ hiểu hơn
let configLoaded = false;
// =====================
// UTILITIES
// =====================
function formatMoney(amount) {
return new Intl.NumberFormat('vi-VN', {
style: 'currency',
currency: 'VND'
}).format(amount || 0);
}
function cleanUsername(username) {
if (!username) return '';
let cleaned = username.trim();
if (cleaned.startsWith('@')) {
cleaned = cleaned.substring(1);
}
// Regex: Chỉ giữ lại chữ cái, số, gạch dưới (_) và dấu chấm (.)
cleaned = cleaned.replace(/[^a-zA-Z0-9_.]/g, '');
return cleaned;
}
function showView(view) {
['login-view', 'main-card', 'dashboard-view'].forEach(id => {
const el = document.getElementById(id);
if (el) el.classList.add('hidden');
});
const targetEl = document.getElementById(view);
if (targetEl) targetEl.classList.remove('hidden');
}
function showMessage(el, msg, type) {
if (!el) return;
const icons = {
success: '✅',
warning: '⚠️',
error: '❌',
info: 'ℹ️'
};
el.innerHTML = `<div style="display:flex;align-items:flex-start;gap:12px;">
<span style="font-size:20px;flex-shrink:0;">${icons[type]}</span>
<div>${msg}</div>
</div>`;
el.className = `alert alert-${type}`;
el.classList.remove('hidden');
}
function hideMessage(el) {
if (el) el.classList.add('hidden');
}
function clearPolling() {
if (pollTimer) clearInterval(pollTimer);
pollTimer = null;
console.log("⏸️ Polling dừng.");
}
function showLoadingScreen(show = true) {
const loadingEl = document.getElementById('global-loading');
if (loadingEl) {
loadingEl.classList.toggle('hidden', !show);
}
}
function copyToClipboard(elementId, successMessage = "✅ Đã sao chép!") {
const el = document.getElementById(elementId);
if (!el) return;
const textToCopy = el.textContent || el.value;
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(textToCopy)
.then(() => alert(successMessage))
.catch(() => fallbackCopy(textToCopy, successMessage));
} else {
fallbackCopy(textToCopy, successMessage);
}
}
function fallbackCopy(textToCopy, successMessage) {
const tempInput = document.createElement('textarea');
tempInput.value = textToCopy;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
alert(successMessage);
}
// =====================
// CONFIG & DISPLAY
// =====================
async function fetchConfig() {
if (configLoaded) return;
let configData = null;
try {
// Ưu tiên /user/config vì nó nhẹ nhất
const res = await fetch(`${WORKER_URL}/user/config`);
const data = await res.json();
if (data.ok && data.config) configData = data.config;
} catch (err) {
console.warn("⚠️ Không thể tải config từ /user/config:", err);
}
if (configData) {
DYNAMIC_CONFIG.ACTIVATION_COST = parseInt(configData.ACTIVATION_COST) || DYNAMIC_CONFIG.ACTIVATION_COST;
DYNAMIC_CONFIG.REFERRAL_REWARD = parseInt(configData.REFERRAL_REWARD) || DYNAMIC_CONFIG.REFERRAL_REWARD;
console.log("✅ Config loaded:", DYNAMIC_CONFIG);
configLoaded = true;
}
updateCostDisplay();
}
function updateCostDisplay() {
const cost = formatMoney(DYNAMIC_CONFIG.ACTIVATION_COST);
const reward = formatMoney(DYNAMIC_CONFIG.REFERRAL_REWARD);
// Main card hint
const hint = document.getElementById('referrer-reward');
if (hint) {
hint.innerHTML = `💡 Người giới thiệu nhận <strong>${reward}</strong>`;
}
// Start Button
const startBtn = document.getElementById('start-btn');
if (startBtn) {
const content = `Bắt đầu nâng cấp <small style="opacity:0.8;font-size:14px;">(${cost})</small>`;
const span = startBtn.querySelector('span:last-child');
if (span) span.innerHTML = content;
}
// Reactivate Buttons
const reactivateContent = `Nâng cấp lại`;
[document.getElementById('reactivate-btn'), document.getElementById('reactivate-btn-dashboard')].forEach(btn => {
if (btn) {
const span = btn.querySelector('span:last-child');
if (span) span.innerHTML = reactivateContent;
}
});
// Dashboard
const friendCost = document.getElementById('friend-activation-cost');
if (friendCost) friendCost.textContent = cost;
const referralReward = document.getElementById('referral-reward-amount');
if (referralReward) referralReward.textContent = reward;
}
// =====================
// INIT & AUTH
// =====================
async function initApp() {
showLoadingScreen(true);
await fetchConfig();
const urlParams = new URLSearchParams(window.location.search);
const referrerFromURL = cleanUsername(urlParams.get('referrer') || '');
if (currentUsername) {
await handleUser(currentUsername, referrerFromURL);
} else {
showView('login-view');
const userUsernameEl = document.getElementById('user-username');
if (userUsernameEl) userUsernameEl.value = '';
}
updateCostDisplay();
showLoadingScreen(false);
}
async function loginUser() {
const username = cleanUsername(document.getElementById('user-username')?.value);
if (!username) {
alert("⚠️ Vui lòng nhập username!");
return;
}
localStorage.setItem('locket_user', username);
currentUsername = username;
const urlParams = new URLSearchParams(window.location.search);
const referrerFromURL = cleanUsername(urlParams.get('referrer') || '');
await fetchConfig(); // Tải lại config nếu chưa load
await handleUser(username, referrerFromURL);
}
function logout() {
localStorage.removeItem('locket_user');
clearPolling();
currentUsername = null;
showView('login-view');
const userUsernameEl = document.getElementById('user-username');
if (userUsernameEl) userUsernameEl.value = '';
fetchConfig().then(() => updateCostDisplay());
}
// =====================
// USER HANDLER (Luồng chính)
// =====================
function hideUpgradeElements() {
['qr-container', 'install-notice', 'download-link', 'reactivate-btn'].forEach(id => {
const el = document.getElementById(id);
if (el) el.classList.add('hidden');
});
const referrerContainer = document.getElementById('referrer-container');
if (referrerContainer) referrerContainer.classList.remove('hidden');
const startBtn = document.getElementById('start-btn');
if (startBtn) {
startBtn.classList.remove('hidden');
startBtn.disabled = false;
}
hideMessage(document.getElementById('activation-message'));
updateCostDisplay();
}
async function handleUser(username, prefillReferrer = '') {
clearPolling();
currentUsername = username;
const messageEl = document.getElementById('activation-message');
showLoadingScreen(true);
// FIX: Xóa tham số referrer khỏi URL sau khi đã đọc
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('referrer')) {
urlParams.delete('referrer');
const newUrl = `${window.location.pathname}${urlParams.toString() ? '?' + urlParams.toString() : ''}${window.location.hash}`;
window.history.replaceState({}, document.title, newUrl);
}
if (!configLoaded) await fetchConfig();
// Cập nhật giá trị input
const usernameInput = document.getElementById('username');
if (usernameInput) {
usernameInput.value = username;
usernameInput.readOnly = false;
}
const referrerInput = document.getElementById('referrer');
if (prefillReferrer && referrerInput) {
referrerInput.value = prefillReferrer;
}
try {
const res = await fetch(`${WORKER_URL}/user/check?username=${encodeURIComponent(username)}`);
const data = await res.json();
if (data.config) {
DYNAMIC_CONFIG.ACTIVATION_COST = parseInt(data.config.ACTIVATION_COST) || DYNAMIC_CONFIG.ACTIVATION_COST;
DYNAMIC_CONFIG.REFERRAL_REWARD = parseInt(data.config.REFERRAL_REWARD) || DYNAMIC_CONFIG.REFERRAL_REWARD;
updateCostDisplay();
}
const referralLink = document.getElementById('referral-link-display');
if (referralLink) {
referralLink.value = `${window.location.origin}${window.location.pathname}?referrer=${encodeURIComponent(username)}`;
}
if (data.ok && (data.user.status === "ACTIVATED" || data.user.status === "GOLD")) {
// Trường hợp 1: Đã kích hoạt (ACTIVATED/GOLD)
showView('dashboard-view');
hideMessage(messageEl);
await loadUserData(username, data.user);
} else if (data.user?.status === "TXN_USED" || data.user?.status === "RC_FAILED" || data.user?.status === "PENDING") {
// Trường hợp 2: Đã thanh toán, bị lỗi kích hoạt, hoặc đang PENDING (cần thanh toán/kích hoạt lại)
// Gọi startUpgrade với cờ isRecheck để kích hoạt luồng QR/Reactivate mà không cần POST lại
await startUpgrade(true);
} else {
// Trường hợp 3: Chưa đăng ký/Chưa có record (UNREGISTERED)
showView('main-card');
hideUpgradeElements();
hideMessage(messageEl);
}
} catch (err) {
console.error("❌ Lỗi API khi check user:", err);
alert("Lỗi kết nối! Vui lòng thử lại.");
showView('login-view');
} finally {
updateCostDisplay();
showLoadingScreen(false);
}
}
// =====================
// UPGRADE & REACTIVATE
// =====================
async function startUpgrade(isRecheck = false) {
const username = cleanUsername(document.getElementById('username')?.value);
const referrer = cleanUsername(document.getElementById('referrer')?.value || '');
const messageEl = document.getElementById('activation-message');
if (!username) {
showMessage(messageEl, "Vui lòng nhập username!", "error");
return;
}
localStorage.setItem('locket_user', username);
currentUsername = username;
await fetchConfig();
const cost = DYNAMIC_CONFIG.ACTIVATION_COST;
const startBtn = document.getElementById('start-btn');
if (!isRecheck) {
if (startBtn) {
startBtn.disabled = true;
startBtn.innerHTML = '<div class="spinner"></div><span>Đang xử lý...</span>';
}
}
const referrerContainer = document.getElementById('referrer-container');
try {
const endpoint = isRecheck ? `${WORKER_URL}/user/check?username=${encodeURIComponent(username)}` : WORKER_URL;
const fetchOptions = isRecheck ? { method: "GET" } : {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, referrer })
};
const res = await fetch(endpoint, fetchOptions);
const data = await res.json();
clearPolling();
if (data.ok && (data.flow === 'ACTIVATED_SUCCESS' || data.user?.status === 'ACTIVATED' || data.user?.status === 'GOLD')) {
// Trường hợp 1: Kích hoạt thành công
showMessage(
messageEl,
`<strong>Nâng cấp Gold thành công! 🎉</strong><br>${data.referral_info || ''}`,
"success"
);
document.getElementById('install-notice')?.classList.remove('hidden');
document.getElementById('download-link')?.classList.remove('hidden');
if (startBtn) startBtn.classList.add('hidden');
document.getElementById('reactivate-btn')?.classList.remove('hidden');
if (referrerContainer) referrerContainer.classList.add('hidden');
setTimeout(() => handleUser(username), 3000);
} else if (data.flow === 'PAYMENT_REQUIRED') {
// Trường hợp 2: Yêu cầu thanh toán (Hiển thị QR)
const substr = data.localIdCode;
const qrImage = document.getElementById('qr-image');
if (qrImage) {
qrImage.src = `https://vietqr.co/api/generate/${QR_BANK_ID}/${QR_ACCOUNT_NO}/${QR_ACCOUNT_NAME}/${cost}/${encodeURIComponent(substr)}?isMask=0&logo=1&style=2&bg=61`;
}
document.getElementById('qr-container')?.classList.remove('hidden');
if (startBtn) startBtn.classList.add('hidden');
if (referrerContainer) referrerContainer.classList.add('hidden');
// Bắt đầu polling để kiểm tra giao dịch
startPolling(username, false);
} else if (data.flow === 'ALREADY_ACTIVATED_PANEL') {
// Trường hợp 3: Đã Premium
showMessage(
messageEl,
"<strong>Bạn đã là Premium!</strong><br>Đang chuyển sang Dashboard...",
"success"
);
setTimeout(() => handleUser(username), 2000);
} else if (data.user?.status === "TXN_USED" || data.user?.status === "RC_FAILED" || data.user?.status === "PENDING") {
// Trường hợp 4: Đã thanh toán nhưng chưa kích hoạt thành công, hoặc đang PENDING (không tìm thấy QR code trong data.flow=PAYMENT_REQUIRED)
// Hiển thị nút reactivate/thông báo
showView('main-card');
hideUpgradeElements();
startPolling(username, true); // Bắt đầu polling để check chuyển trạng thái
}
else {
// Trường hợp 5: Lỗi khác
showMessage(messageEl, data.error || data.details || "Có lỗi xảy ra!", "error");
if (startBtn) {
startBtn.classList.remove('hidden');
startBtn.disabled = false;
updateCostDisplay();
}
}
} catch (err) {
showMessage(messageEl, `Lỗi kết nối: ${err.message}`, "error");
} finally {
if (startBtn && !isRecheck) {
startBtn.disabled = false;
updateCostDisplay();
}
}
}
async function reactivate() {
const username = currentUsername;
const referrer = cleanUsername(document.getElementById('referrer')?.value || '');
const isDashboard = !document.getElementById('dashboard-view')?.classList.contains('hidden');
const messageEl = isDashboard
? document.getElementById('dashboard-activation-message')
: document.getElementById('activation-message');
if (!messageEl || !username) return;
const btns = [
document.getElementById('reactivate-btn'),
document.getElementById('reactivate-btn-dashboard')
];
btns.forEach(btn => { if (btn) btn.disabled = true; });
showMessage(messageEl, "Đang nâng cấp lại...", "warning");
try {
// Gửi POST request tới main flow, backend sẽ cố gắng kích hoạt lại RevenueCat
const res = await fetch(WORKER_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, referrer })
});
const data = await res.json();
clearPolling();
if (data.ok || data.flow === 'ALREADY_ACTIVATED_PANEL') {
showMessage(messageEl, "<strong>Nâng cấp Gold thành công! 🎉</strong>", "success");
if (isDashboard) {
await loadUserData(username);
} else {
setTimeout(() => handleUser(username), 2000);
}
} else {
showMessage(messageEl, data.error || data.details || "Nâng cấp thất bại!", "error");
}
} catch (err) {
showMessage(messageEl, `Lỗi kết nối: ${err.message}`, "error");
} finally {
btns.forEach(btn => { if (btn) btn.disabled = false; });
updateCostDisplay();
}
}
// =====================
// DASHBOARD
// =====================
async function loadUserData(username, initialData = null) {
let user = initialData;
const welcomeMsg = document.getElementById('welcome-message');
if (welcomeMsg) welcomeMsg.textContent = `Xin chào, ${username}!`;
const balanceEl = document.getElementById('current-balance');
if (balanceEl) balanceEl.textContent = '...';
try {
if (!user) {
const res = await fetch(`${WORKER_URL}/user/check?username=${encodeURIComponent(username)}`);
const data = await res.json();
if (data.ok) {
user = data.user;
if (data.config) {
DYNAMIC_CONFIG.ACTIVATION_COST = parseInt(data.config.ACTIVATION_COST) || DYNAMIC_CONFIG.ACTIVATION_COST;
DYNAMIC_CONFIG.REFERRAL_REWARD = parseInt(data.config.REFERRAL_REWARD) || DYNAMIC_CONFIG.REFERRAL_REWARD;
}
}
}
if (user) {
if (balanceEl) balanceEl.textContent = formatMoney(user.balance);
const statusEl = document.getElementById('user-status');
if (statusEl) {
const statusText = user.status === 'GOLD' ? '👑 GOLD' :
user.status === 'ACTIVATED' ? '✨ GOLD' : '❌ Chưa kích hoạt';
statusEl.textContent = statusText;
}
const referralCount = document.getElementById('referral-count');
if (referralCount) {
referralCount.innerHTML = `💎 ${user.referralCount || 0} lượt giới thiệu`;
}
const reactivateBtn = document.getElementById('reactivate-btn-dashboard');
const downloadBtn = document.getElementById('download-link-dashboard');
if (user.status === 'ACTIVATED' || user.status === 'GOLD') {
reactivateBtn?.classList.remove('hidden');
downloadBtn?.classList.remove('hidden');
} else {
reactivateBtn?.classList.add('hidden');
downloadBtn?.classList.add('hidden');
}
}
updateCostDisplay();
} catch (err) {
console.error("❌ Lỗi load user data:", err);
if (balanceEl) balanceEl.textContent = 'Lỗi';
const statusEl = document.getElementById('user-status');
if (statusEl) statusEl.textContent = 'Lỗi kết nối';
}
}
function copyReferralLink() {
copyToClipboard('referral-link-display', "✅ Đã sao chép link giới thiệu!");
}
async function activateFriend() {
const referred_username = cleanUsername(document.getElementById('friend-username')?.value || '');
const messageEl = document.getElementById('dashboard-activation-message');
const cost = DYNAMIC_CONFIG.ACTIVATION_COST;
if (!referred_username) {
showMessage(messageEl, "Vui lòng nhập Username bạn bè.", "error");
return;
}
showMessage(messageEl, `Đang dùng ${formatMoney(cost)} để kích hoạt...`, "warning");
try {
const res = await fetch(`${WORKER_URL}/user/referral-activate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
referrer_username: currentUsername,
referred_username
})
});
const data = await res.json();
if (data.ok) {
showMessage(
messageEl,
`<strong>Thành công!</strong><br>Đã dùng ${formatMoney(cost)} để kích hoạt Gold cho <strong>${referred_username}</strong>`,
"success"
);
const friendInput = document.getElementById('friend-username');
if (friendInput) friendInput.value = '';
await loadUserData(currentUsername);
} else {
showMessage(messageEl, data.error || "Không thành công", "error");
}
} catch (err) {
showMessage(messageEl, `Lỗi kết nối: ${err.message}`, "error");
}
}
// =====================
// POLLING
// =====================
function startPolling(username, isReactivationCheck = false) {
console.log(`🔄 Bắt đầu polling cho: ${username}, Mode: ${isReactivationCheck ? 'Reactivate Check' : 'Payment Check'}`);
clearPolling();
const qrStatus = document.querySelector('.qr-status');
const messageEl = document.getElementById('activation-message');
let dotCount = 0;
pollTimer = setInterval(async () => {
try {
const res = await fetch(`${WORKER_URL}/user/check?username=${encodeURIComponent(username)}`);
const data = await res.json();
const referrerContainer = document.getElementById('referrer-container');
if (data.ok && (data.user?.status === 'ACTIVATED' || data.user?.status === 'GOLD')) {
// THÀNH CÔNG: Chuyển thẳng tới dashboard
clearPolling();
if (qrStatus) {
qrStatus.innerHTML = '<span>✅</span><span>Thanh toán đã xác nhận! Đang hoàn tất...</span>';
}
document.getElementById('qr-container')?.classList.add('hidden');
showMessage(
messageEl,
`<strong>Thanh toán thành công! Đã lên Gold 🎉</strong><br>${data.referral_info || ''}`,
"success"
);
document.getElementById('install-notice')?.classList.remove('hidden');
document.getElementById('download-link')?.classList.remove('hidden');
document.getElementById('reactivate-btn')?.classList.remove('hidden');
if (referrerContainer) referrerContainer.classList.remove('hidden');
setTimeout(() => handleUser(username), 3000);
return;
} else if (data.user?.status === 'TXN_USED' || data.user?.status === 'RC_FAILED') {
// GIAO DỊCH ĐÃ VÀO, nhưng kích hoạt chưa xong/bị lỗi
clearPolling();
if (!isReactivationCheck) {
// Nếu đang ở luồng Payment Check (chưa bấm start/reactivate)
if (qrStatus) {
qrStatus.innerHTML = '<span>⚠️</span><span>Đã nhận thanh toán. Vui lòng bấm "Nâng cấp lại".</span>';
}
document.getElementById('qr-container')?.classList.add('hidden');
document.getElementById('install-notice')?.classList.remove('hidden');
document.getElementById('download-link')?.classList.remove('hidden');
document.getElementById('reactivate-btn')?.classList.remove('hidden');
showMessage(
messageEl,
"<strong>Thanh toán thành công.</strong><br>Vui lòng nhấn 'Nâng cấp lại' để hoàn tất quá trình kích hoạt Premium.",
"warning"
);
if (referrerContainer) referrerContainer.classList.remove('hidden');
}
return; // Dừng polling
} else {
// ĐANG CHỜ THANH TOÁN (Chỉ áp dụng cho luồng Payment Check)
if (!isReactivationCheck) {
dotCount = (dotCount + 1) % 4;
if (qrStatus) {
qrStatus.innerHTML = `<span>⏳</span><span>Đang chờ thanh toán${'.'.repeat(dotCount)}</span>`;
}
}
}
} catch (err) {
console.error("Polling error:", err);
dotCount = (dotCount + 1) % 4;
if (!isReactivationCheck && qrStatus) {
qrStatus.innerHTML = `<span>❌</span><span>Lỗi kết nối (Tự động thử lại)${'.'.repeat(dotCount)}</span>`;
}
}
}, POLL_INTERVAL_MS);
}
// =====================
// BINDINGS & START
// =====================
window.loginUser = loginUser;
window.logout = logout;
window.startUpgrade = startUpgrade;
window.reactivate = reactivate;
window.activateFriend = activateFriend;
window.copyReferralLink = copyReferralLink;
window.showLoadingScreen = showLoadingScreen;
window.copyToClipboard = copyToClipboard;
window.onload = () => {
initApp();
};