<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>Commission - CV111</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700;800;900&display=swap">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
    *{box-sizing:border-box; margin:0; padding:0;}
    :root{
        --bg:#0A0714; --card:#181129; --border:rgba(255,255,255,.07); --border-hi:rgba(155,93,229,.35);
        --gold:#F5C542; --purple:#9B5DE5;
        --grad-main:linear-gradient(135deg,#6C3FC5 0%,#9B5DE5 50%,#B57EF0 100%);
        --grad-gold:linear-gradient(135deg,#F5C542,#E0A926);
        --text:#FFFFFF; --muted:#9691B0; --green:#22D67A; --red:#FF5C7A;
    }
    body{
        min-height:100vh; font-family:'Poppins',-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif; color:var(--text);
        background:radial-gradient(circle at 20% 0%, rgba(155,93,229,.15), transparent 45%),
                   radial-gradient(circle at 90% 100%, rgba(245,197,66,.1), transparent 40%), var(--bg);
        padding-bottom:34px;
    }
    .top{ display:flex; align-items:center; gap:14px; padding:18px 16px; position:sticky; top:0; z-index:40;
        background:rgba(10,7,20,.9); backdrop-filter:blur(16px); border-bottom:1px solid var(--border-hi); }
    .top a{ width:38px; height:38px; border-radius:12px; background:var(--card); border:1px solid var(--border);
        display:flex; align-items:center; justify-content:center; color:var(--gold); text-decoration:none; font-size:15px; }
    .top h1{ font-size:17px; font-weight:800; }

    main{ max-width:520px; margin:0 auto; padding:0 16px; }

    .hero-card{ background:linear-gradient(160deg,#1D1533 0%,#120D24 100%); border:1px solid var(--border-hi);
        border-radius:22px; padding:26px 22px; text-align:center; margin:16px 0; box-shadow:0 24px 60px -15px rgba(108,63,197,.4); }
    .hero-card .lab{ font-size:11px; color:var(--muted); font-weight:700; text-transform:uppercase; letter-spacing:.6px; }
    .hero-card .amount{ font-size:34px; font-weight:900; color:var(--gold); margin:8px 0 18px; }

    .claim-btn{ width:100%; padding:15px; border-radius:14px; border:none; font-weight:800; font-size:14.5px; cursor:pointer;
        background:var(--grad-main); color:#fff; box-shadow:0 14px 30px -10px rgba(108,63,197,.6); }
    .claim-btn:disabled{ opacity:.5; }

    .rate-grid{ display:grid; grid-template-columns:1fr 1fr; gap:10px; margin-bottom:16px; }
    .rate-card{ background:var(--card); border:1px solid var(--border); border-radius:14px; padding:14px; text-align:center; }
    .rate-card .rl{ font-size:10px; color:var(--muted); margin-bottom:6px; }
    .rate-card .rv{ font-size:18px; font-weight:900; color:#fff; }

    .stat-grid{ display:grid; grid-template-columns:repeat(3,1fr); gap:10px; margin:16px 0; }
    .stat-card{ background:var(--card); border:1px solid var(--border); border-radius:14px; padding:12px 8px; text-align:center; }
    .stat-card .sl{ font-size:9px; color:var(--muted); text-transform:uppercase; margin-bottom:5px; }
    .stat-card .sv{ font-size:14px; font-weight:800; color:#fff; }

    .section-title{ font-size:13px; font-weight:800; color:var(--muted); text-transform:uppercase; letter-spacing:.5px; margin:20px 0 10px; }

    .history-item{ background:var(--card); border:1px solid var(--border); border-radius:14px; padding:14px; margin-bottom:8px;
        display:flex; align-items:center; justify-content:space-between; }
    .history-item .hleft{ display:flex; align-items:center; gap:10px; }
    .history-item .hicon{ width:36px; height:36px; border-radius:10px; background:rgba(34,214,122,.15);
        display:flex; align-items:center; justify-content:center; }
    .history-item .hicon i{ color:var(--green); font-size:14px; }
    .history-item .hdate{ font-size:10px; color:var(--muted); margin-top:2px; }
    .history-item .hamount{ font-size:14px; font-weight:800; color:var(--green); }

    .empty-state{ text-align:center; padding:40px 20px; color:var(--muted); font-size:12.5px; }

    .msg{ padding:12px 14px; border-radius:12px; font-size:12px; font-weight:600; margin-bottom:14px; text-align:center; }
    .msg-ok{ background:rgba(34,214,122,.1); border:1px solid rgba(34,214,122,.3); color:var(--green); }
    .msg-err{ background:rgba(255,92,122,.1); border:1px solid rgba(255,92,122,.3); color:var(--red); }
    .hidden{ display:none !important; }
</style>
</head>
<body>
<?php $pt = __DIR__ . '/page_transition.php'; if (file_exists($pt)) { include $pt; } ?>

<div class="top">
    <a href="/newprofile.php"><i class="fa-solid fa-arrow-left"></i></a>
    <h1>💰 Commission</h1>
</div>

<main>
    <div id="msgBox" class="hidden"></div>

    <div class="hero-card">
        <div class="lab">Claim করার মতো Commission</div>
        <div class="amount" id="availableAmount">৳0.00</div>
        <button class="claim-btn" id="claimBtn" onclick="claimCommission()">Claim করো</button>
    </div>

    <div class="rate-grid">
        <div class="rate-card">
            <div class="rl">Deposit-Commission</div>
            <div class="rv" id="depositRate">2%</div>
        </div>
        <div class="rate-card">
            <div class="rl">Betting-Commission</div>
            <div class="rv" id="bettingRate">1%</div>
        </div>
    </div>

    <div class="stat-grid">
        <div class="stat-card">
            <div class="sl">Downline</div>
            <div class="sv" id="downlineCount">0</div>
        </div>
        <div class="stat-card">
            <div class="sl">মোট Deposit</div>
            <div class="sv" id="totalDeposit">৳0</div>
        </div>
        <div class="stat-card">
            <div class="sl">মোট Bet</div>
            <div class="sv" id="totalBet">৳0</div>
        </div>
    </div>

    <!-- ✅ নতুন যোগ — সহজ, Example-সহ বর্ণনা -->
    <div style="background:rgba(155,93,229,.08); border:1px solid rgba(155,93,229,.25); border-radius:14px; padding:16px; margin-bottom:16px;">
        <div style="display:flex; align-items:center; gap:8px; margin-bottom:10px;">
            <i class="fa-solid fa-circle-info" style="color:#9B5DE5; font-size:14px;"></i>
            <span style="color:#fff; font-weight:800; font-size:13px;">Commission কীভাবে কাজ করে?</span>
        </div>
        <div style="color:#9691B0; font-size:11.5px; line-height:1.8;">
            তুমি তোমার Referral-Code (Invite-Link) দিয়ে যাদের
            আনবে, তারাই তোমার <b style="color:#F5C542;">"Downline"</b>।
            তারা যা Deposit বা Game খেলবে, তার একটা অংশ
            তুমি Commission হিসেবে পাবে।
            <br><br>
            <b style="color:#fff;">উদাহরণ ১ (Deposit থেকে):</b><br>
            তোমার এক বন্ধু (Downline) যদি ৳1000 Deposit করে,
            তুমি পাবে তার <b style="color:#F5C542;">2%</b> =
            <b style="color:#22D67A;">৳20</b>
            <br><br>
            <b style="color:#fff;">উদাহরণ ২ (Bet থেকে):</b><br>
            সে যদি Game খেলে মোট ৳500 Bet করে, তুমি পাবে
            তার <b style="color:#F5C542;">1%</b> =
            <b style="color:#22D67A;">৳5</b>
            <br><br>
            এভাবে তোমার প্রতিটা Downline-এর Deposit ও
            Betting-Activity থেকে, স্বয়ংক্রিয়ভাবে Commission
            জমা হতে থাকে — উপরের <b style="color:#F5C542;">"Claim করো"</b>
            Button চেপে, সেই টাকা তোমার Balance-এ নিয়ে নাও।
        </div>
    </div>

    <div class="section-title">Commission-History</div>
    <div id="historyList"></div>
</main>

<script>
const token = localStorage.getItem('token');
if (!token) window.location.href = '/newlogin.php';

function showMsg(text, ok) {
    const msgBox = document.getElementById('msgBox');
    msgBox.classList.remove('hidden');
    msgBox.className = 'msg ' + (ok ? 'msg-ok' : 'msg-err');
    msgBox.textContent = text;
}

async function loadSummary() {
    try {
        const res = await fetch('/commission_api.php?action=summary', {
            headers: { 'Authorization': 'Bearer ' + token }
        });
        const data = await res.json();

        if (data.code !== 0) {
            showMsg(data.msg, false);
            return;
        }

        document.getElementById('availableAmount').textContent = '৳' + data.available_to_claim.toLocaleString('en-IN', {minimumFractionDigits: 2});
        document.getElementById('depositRate').textContent = data.deposit_rate + '%';
        document.getElementById('bettingRate').textContent = data.betting_rate + '%';
        document.getElementById('downlineCount').textContent = data.downline_count;
        document.getElementById('totalDeposit').textContent = '৳' + data.total_deposit_amount.toLocaleString('en-IN', {maximumFractionDigits: 0});
        document.getElementById('totalBet').textContent = '৳' + data.total_betting_amount.toLocaleString('en-IN', {maximumFractionDigits: 0});

        document.getElementById('claimBtn').disabled = data.available_to_claim <= 0;
    } catch (e) {
        showMsg('একটা সমস্যা হয়েছে: ' + e.message, false);
    }
}

async function loadHistory() {
    const list = document.getElementById('historyList');
    try {
        const res = await fetch('/commission_api.php?action=history', {
            headers: { 'Authorization': 'Bearer ' + token }
        });
        const data = await res.json();

        if (!data.history || data.history.length === 0) {
            list.innerHTML = '<div class="empty-state">এখনো কোনো Commission Claim করা হয়নি।</div>';
            return;
        }

        list.innerHTML = '';
        data.history.forEach(h => {
            const div = document.createElement('div');
            div.className = 'history-item';
            div.innerHTML = `
                <div class="hleft">
                    <div class="hicon"><i class="fa-solid fa-check"></i></div>
                    <div>
                        <div style="font-size:12.5px; font-weight:700;">Commission Claim</div>
                        <div class="hdate">${h.paid_at}</div>
                    </div>
                </div>
                <div class="hamount">+৳${h.commission_amount}</div>
            `;
            list.appendChild(div);
        });
    } catch (e) {
        list.innerHTML = '<div class="empty-state">History লোড করা যায়নি।</div>';
    }
}

async function claimCommission() {
    const btn = document.getElementById('claimBtn');
    btn.disabled = true;
    btn.textContent = 'Claim হচ্ছে...';

    try {
        const res = await fetch('/commission_api.php?action=claim', {
            method: 'POST',
            headers: { 'Authorization': 'Bearer ' + token }
        });
        const data = await res.json();

        if (data.code === 0) {
            showMsg(data.msg, true);
            loadSummary();
            loadHistory();
        } else {
            showMsg(data.msg, false);
        }
    } catch (e) {
        showMsg('একটা সমস্যা হয়েছে: ' + e.message, false);
    }

    btn.textContent = 'Claim করো';
    btn.disabled = false;
}

loadSummary();
loadHistory();
</script>

</body>
</html>
