💳 SỐ DƯ KHẢ DỤNG
₫0
📥
₫0
Tổng nạp
🛒
₫0
Tổng chi tiêu
📅
0
Giao dịch

📋 Lịch sử giao dịch

document.addEventListener('DOMContentLoaded', () => { currentUser = Auth.requireAuth(); if (!currentUser) return; renderSidebar(); renderWallet(); renderTransactions(); }); function renderSidebar() { const initials = currentUser.name.split(' ').map(w => w[0]).join('').substring(0, 2).toUpperCase(); document.getElementById('sidebarUserCard').innerHTML = ` 💳 Thần Pay `; document.getElementById('navUserInfo').innerHTML = `${currentUser.name}`; } function renderWallet() { currentUser = DB.Users.findById(currentUser.id); document.getElementById('walletAmount').textContent = formatMoney(currentUser.walletBalance || 0); const txs = DB.Transactions.byUser(currentUser.id); const totalTopup = txs.filter(t => t.type === 'topup').reduce((a, t) => a + t.amount, 0); const totalPaid = Math.abs(txs.filter(t => t.type === 'payment').reduce((a, t) => a + t.amount, 0)); document.getElementById('totalTopup').textContent = formatMoney(totalTopup); document.getElementById('totalPaid').textContent = formatMoney(totalPaid); document.getElementById('txCount').textContent = txs.length; } function renderTransactions() { const txs = DB.Transactions.byUser(currentUser.id).reverse(); const container = document.getElementById('transactionList'); if (!txs.length) { container.innerHTML = `
💳

Chưa có giao dịch nào

Nạp tiền để bắt đầu mua sắm!

`; return; } const typeConfig = { topup: { icon: '📥', cls: 'topup', sign: '+' }, payment: { icon: '🛒', cls: 'payment', sign: '' }, income: { icon: '💰', cls: 'income', sign: '+' }, withdraw: { icon: '🏦', cls: 'withdraw', sign: '' }, refund: { icon: '↩️', cls: 'refund', sign: '+' }, }; container.innerHTML = txs.map(t => { const cfg = typeConfig[t.type] || { icon: '💲', cls: 'topup', sign: '' }; const isPositive = t.amount > 0; return `
${cfg.icon}
${t.description}
${formatDateTime(t.createdAt)} · Số dư sau: ${formatMoney(t.balanceAfter)}
${isPositive ? '+' : ''}${formatMoney(t.amount)}
`; }).join(''); } function openTopup() { document.getElementById('topupModal').classList.add('show'); } function openWithdraw() { document.getElementById('withdrawModal').classList.add('show'); } function closeModal(id) { document.getElementById(id).classList.remove('show'); } let selectedAmount = 0; function selectAmount(amount) { selectedAmount = amount; document.getElementById('topupAmount').value = amount; document.querySelectorAll('.amount-preset').forEach(b => { b.classList.toggle('active', parseInt(b.textContent.replace(/[₫KM]/g, '')) * (b.textContent.includes('K') ? 1000 : b.textContent.includes('M') ? 1000000 : 1) === amount); }); } function doTopup() { const amount = parseInt(document.getElementById('topupAmount').value) || selectedAmount; if (!amount || amount < 10000) { showToast('⚠️ Vui lòng nhập số tiền hợp lệ (tối thiểu ₫10,000)!'); return; } const result=Auth.topupWallet(amount); if (result.ok) { closeModal('topupModal'); renderWallet(); renderTransactions(); renderSidebar(); showToast(`✅ Nạp ${formatMoney(amount)} thành công! Số dư: ${formatMoney(result.newBalance)}`); } } function doWithdraw() { const amount=parseInt(document.getElementById('withdrawAmount').value); if (!amount || amount < 50000) { showToast('⚠️ Số tiền rút tối thiểu ₫50,000!'); return; } if (amount> (currentUser.walletBalance || 0)) { showToast('⚠️ Số dư không đủ!'); return; } closeModal('withdrawModal'); showToast(`✅ Yêu cầu rút ${formatMoney(amount)} đã được gửi! Xử lý trong 1-3 ngày.`); } function formatMoney(n) { return '₫' + (n || 0).toLocaleString('vi-VN'); } function formatDateTime(d) { if (!d) return ''; const date = new Date(d); return `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`; } function showToast(msg) { const t = document.createElement('div'); t.className = 'toast'; t.textContent = msg; document.body.appendChild(t); setTimeout(() => t.remove(), 3000); } // Close modal on backdrop click document.querySelectorAll('.modal-backdrop').forEach(m => { m.addEventListener('click', e => { if (e.target === m) m.classList.remove('show'); }); });