// ✅ Header Balance Loader — Login থাকলে Real ব্যালেন্স দেখায়,
// না থাকলে Login/Register বাটন দেখায়
async function loadBalance() {
    const loggedOut = document.getElementById('loggedOutButtons');
    const loggedIn = document.getElementById('loggedInBadge');
    if (!loggedOut || !loggedIn) return;

    if (!token) {
        loggedOut.classList.remove('hidden');
        loggedOut.classList.add('flex');
        loggedIn.classList.add('hidden');
        loggedIn.classList.remove('flex');
        return;
    }
    loggedOut.classList.add('hidden');
    loggedOut.classList.remove('flex');
    loggedIn.classList.remove('hidden');
    loggedIn.classList.add('flex');

    const random = Math.random().toString(36).substring(2, 15);
    const shonustr = `{"language":1,"random":"${random}"}`;
    const signature = md5(shonustr).toUpperCase();
    try {
        const res = await fetch('/deepanshu/api/webapi/GetBalance.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ language: 1, random, signature, timestamp: Date.now() })
        });
        const data = await res.json();
        if (data.code === 0 && data.data) {
            document.getElementById('balanceBadge').textContent = '৳' + Number(data.data.amount).toLocaleString('en-IN', {maximumFractionDigits: 0});
        } else {
            document.getElementById('balanceBadge').textContent = '৳ --';
        }
    } catch (e) {
        document.getElementById('balanceBadge').textContent = '৳ --';
    }
}

document.addEventListener('DOMContentLoaded', loadBalance);
