<?php
/**
 * newbonuspopup.php
 * Login করার পরপরই (Homepage Load হলে), এই Popup-এ
 * User-এর Claim-করার-যোগ্য Bonus দেখানো হয়।
 * newhome.php-তে <?php include 'newbonuspopup.php'; ?> দিয়ে বসানো যায়,
 * অথবা আলাদা <script> দিয়ে Include করা যায়।
 */
?>
<div id="bonusPopupOverlay" style="display:none; position:fixed; inset:0; background:rgba(0,0,0,.75); z-index:9999; align-items:center; justify-content:center; padding:20px;">
    <div style="background:#102620; border:1px solid rgba(255,213,79,.3); border-radius:16px; max-width:360px; width:100%; padding:24px; text-align:center; position:relative;">
        <button onclick="closeBonusPopup()" style="position:absolute; top:12px; right:14px; background:none; border:none; color:#888; font-size:20px; cursor:pointer;">✕</button>
        <i class="fa-solid fa-gift" style="font-size:36px; color:#ffd54f; margin-bottom:10px;"></i>
        <div style="color:#fff; font-weight:900; font-size:18px; margin-bottom:6px;">তোমার জন্য Bonus আছে!</div>
        <div style="color:#9fb8ae; font-size:12.5px; margin-bottom:16px;">Deposit করে, নিচের Bonus-গুলো Claim করো</div>
        <div id="bonusPopupList" style="max-height:280px; overflow-y:auto; text-align:left;"></div>
    </div>
</div>

<script>
(function() {
    const popupToken = localStorage.getItem('token');
    if (!popupToken) return; // Login না থাকলে কিছু দেখাবে না

    function popupMd5(str) {
        function rotateLeft(n, s) { return (n << s) | (n >>> (32 - s)); }
        function toHex(n) { let s = '', v; for (let i = 0; i < 4; i++) { v = (n >>> (i * 8)) & 255; s += ('0' + v.toString(16)).slice(-2); } return s; }
        function utf8Encode(str) { return unescape(encodeURIComponent(str)); }
        str = utf8Encode(str);
        const msgLen = str.length;
        const numBlocks = (((msgLen + 8) >>> 6) + 1) * 16;
        const M = new Array(numBlocks).fill(0);
        for (let i = 0; i < msgLen; i++) M[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
        M[msgLen >> 2] |= 0x80 << ((msgLen % 4) * 8);
        M[numBlocks - 2] = msgLen * 8;
        let a = 1732584193, b = -271733879, c = -1732584194, d = 271733878;
        const S = [7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21];
        const K = [];
        for (let i = 0; i < 64; i++) K[i] = (Math.floor(Math.abs(Math.sin(i + 1)) * 4294967296)) | 0;
        for (let i = 0; i < numBlocks; i += 16) {
            let A = a, B = b, C = c, D = d;
            for (let j = 0; j < 64; j++) {
                let F, g;
                if (j < 16) { F = (B & C) | (~B & D); g = j; }
                else if (j < 32) { F = (D & B) | (~D & C); g = (5 * j + 1) % 16; }
                else if (j < 48) { F = B ^ C ^ D; g = (3 * j + 5) % 16; }
                else { F = C ^ (B | ~D); g = (7 * j) % 16; }
                F = (F + A + K[j] + (M[i + g] | 0)) | 0;
                A = D; D = C; C = B;
                B = (B + rotateLeft(F, S[j])) | 0;
            }
            a = (a + A) | 0; b = (b + B) | 0; c = (c + C) | 0; d = (d + D) | 0;
        }
        return toHex(a) + toHex(b) + toHex(c) + toHex(d);
    }

    async function loadPopupBonusList() {
        const random = Math.random().toString(36).substring(2, 15);
        const shonustr = `{"getAll":true,"language":1,"random":"${random}"}`;
        const signature = popupMd5(shonustr).toUpperCase();

        try {
            const res = await fetch('/deepanshu/api/webapi/GetFirstRechargeList.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + popupToken },
                body: JSON.stringify({ getAll: true, language: 1, random, signature, timestamp: Date.now() })
            });
            const data = await res.json();
            if (data.code !== 0 || !data.data) return;

            // ✅ শুধু "Claim করার যোগ্য" আর "এখনো Claim হয়নি" — সেগুলোই দেখানো
            const claimable = data.data.filter(item => item.canReceive && !item.isFinshed);

            if (claimable.length === 0) return; // Claim করার কিছু না থাকলে Popup-ই দেখাবে না

            const list = document.getElementById('bonusPopupList');
            list.innerHTML = claimable.map(item => `
                <div style="background:#0b1d1a; border:1px solid rgba(0,230,118,.2); border-radius:10px; padding:12px 14px; margin-bottom:8px; display:flex; align-items:center; justify-content:space-between;">
                    <div>
                        <div style="color:#fff; font-weight:700; font-size:13px;">৳${item.rechargeAmount.toLocaleString()} Deposit Bonus</div>
                        <div style="color:#00e676; font-weight:800; font-size:15px;">৳${item.rewardAmount} পাবে</div>
                    </div>
                    <button onclick="claimPopupBonus(${item.id}, this)" style="background:linear-gradient(135deg,#ffd54f,#ffb300); color:#1a1400; font-weight:800; padding:8px 14px; border-radius:8px; border:none; font-size:12px; cursor:pointer;">Claim</button>
                </div>
            `).join('');

            document.getElementById('bonusPopupOverlay').style.display = 'flex';
        } catch (e) {}
    }

    window.claimPopupBonus = async function(taskId, btn) {
        btn.textContent = '...';
        const random = Math.random().toString(36).substring(2, 15);
        const shonustr = `{"language":1,"random":"${random}","taskId":${taskId}}`;
        const signature = popupMd5(shonustr).toUpperCase();

        try {
            const res = await fetch('/deepanshu/api/webapi/ReceiveFirstRechargeReward.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + popupToken },
                body: JSON.stringify({ language: 1, random, signature, taskId, timestamp: Date.now() })
            });
            const data = await res.json();
            if (data.code === 0) {
                btn.textContent = '✓ পেয়েছো!';
                btn.style.background = '#00e676';
                btn.disabled = true;
            } else {
                btn.textContent = 'ব্যর্থ';
            }
        } catch (e) {
            btn.textContent = 'ব্যর্থ';
        }
    };

    window.closeBonusPopup = function() {
        document.getElementById('bonusPopupOverlay').style.display = 'none';
    };

    setTimeout(loadPopupBonusList, 800); // Page Load হওয়ার একটু পর দেখানো
})();
</script>
