<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>Betting History - 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%);
        --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,.12), transparent 45%),
                   radial-gradient(circle at 90% 90%, rgba(245,197,66,.06), 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; flex-shrink:0; }
    .top h1{ font-size:17px; font-weight:800; flex:1; }
    .top .refresh-btn{ cursor:pointer; }

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

    .summary-strip{ display:grid; grid-template-columns:repeat(3,1fr); gap:10px; margin:16px 0; }
    .summary-card{ background:linear-gradient(160deg,#1D1533,#120D24); border:1px solid var(--border-hi); border-radius:16px; padding:14px 10px; text-align:center; }
    .summary-card .lab{ font-size:9.5px; color:var(--muted); font-weight:700; text-transform:uppercase; letter-spacing:.5px; margin-bottom:6px; }
    .summary-card .val{ font-size:16px; font-weight:900; }
    .val.green{ color:var(--green); } .val.red{ color:var(--red); } .val.gold{ color:var(--gold); }

    .session-card{ background:var(--card); border:1px solid var(--border); border-radius:18px; padding:16px; margin-bottom:12px; animation:fadeIn .3s ease both; }
    @keyframes fadeIn{ from{opacity:0; transform:translateY(6px);} to{opacity:1; transform:translateY(0);} }

    .session-header{ display:flex; align-items:center; justify-content:space-between; margin-bottom:12px; }
    .game-info{ display:flex; align-items:center; gap:10px; }
    .game-icon{ width:38px; height:38px; border-radius:11px; background:var(--grad-main); display:flex; align-items:center; justify-content:center; flex-shrink:0; }
    .game-icon i{ color:#fff; font-size:15px; }
    .game-name{ font-size:13.5px; font-weight:800; color:#fff; max-width:170px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
    .game-date{ font-size:10px; color:var(--muted); margin-top:2px; }

    .pl-badge{ font-size:14px; font-weight:900; padding:5px 12px; border-radius:20px; }
    .pl-positive{ background:rgba(34,214,122,.15); color:var(--green); }
    .pl-negative{ background:rgba(255,92,122,.15); color:var(--red); }

    .detail-grid{ display:grid; grid-template-columns:repeat(4,1fr); gap:8px; padding-top:12px; border-top:1px solid var(--border); }
    .detail-item{ text-align:center; }
    .detail-item .dl{ font-size:9px; color:var(--muted); text-transform:uppercase; letter-spacing:.3px; margin-bottom:4px; }
    .detail-item .dv{ font-size:12px; font-weight:700; color:#fff; }

    .empty-state{ text-align:center; padding:70px 20px; color:var(--muted); }
    .empty-state i{ font-size:36px; margin-bottom:14px; display:block; color:rgba(245,197,66,.3); }
    .empty-state p{ font-size:13px; }

    .skeleton{ background:linear-gradient(90deg,#181129 25%,#241D3D 50%,#181129 75%); background-size:200% 100%; animation:shimmer 1.4s ease-in-out infinite; border-radius:18px; height:120px; margin-bottom:12px; }
    @keyframes shimmer{ 0%{background-position:200% 0;} 100%{background-position:-200% 0;} }
</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>Betting History</h1>
    <a href="#" class="refresh-btn" onclick="loadHistory(); return false;"><i class="fa-solid fa-rotate"></i></a>
</div>

<main>
    <div class="summary-strip">
        <div class="summary-card">
            <div class="lab">মোট Session</div>
            <div class="val gold" id="sumCount">0</div>
        </div>
        <div class="summary-card">
            <div class="lab">মোট Bet</div>
            <div class="val" id="sumBet">৳0</div>
        </div>
        <div class="summary-card">
            <div class="lab">Net Profit/Loss</div>
            <div class="val" id="sumProfit">৳0</div>
        </div>
    </div>

    <div id="historyList"></div>
</main>

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

function showSkeleton() {
    document.getElementById('historyList').innerHTML = Array(5).fill('<div class="skeleton"></div>').join('');
}

function formatDateTime(isoStr) {
    const d = new Date(isoStr.replace(' ', 'T'));
    const options = { day: '2-digit', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit' };
    return d.toLocaleString('bn-BD', options);
}

async function loadHistory() {
    showSkeleton();
    const list = document.getElementById('historyList');

    try {
        const res = await fetch('/get_detailed_history.php', {
            headers: { 'Authorization': 'Bearer ' + token }
        });
        const data = await res.json();

        if (!data.sessions || data.sessions.length === 0) {
            list.innerHTML = `<div class="empty-state"><i class="fa-solid fa-dice"></i><p>এখনো কোনো Betting-History নেই।<br>একটা Game খেলে দেখো!</p></div>`;
            document.getElementById('sumCount').textContent = '0';
            document.getElementById('sumBet').textContent = '৳0';
            document.getElementById('sumProfit').textContent = '৳0';
            return;
        }

        let totalBet = 0, totalProfit = 0;
        list.innerHTML = '';

        data.sessions.forEach(s => {
            const isProfit = s.total_profit_loss >= 0;
            totalBet += s.total_bet;
            totalProfit += s.total_profit_loss;

            const div = document.createElement('div');
            div.className = 'session-card';
            div.innerHTML = `
                <div class="session-header">
                    <div class="game-info">
                        <div class="game-icon"><i class="fa-solid fa-gamepad"></i></div>
                        <div>
                            <div class="game-name">${s.game_name}</div>
                            <div class="game-date">${formatDateTime(s.start_time)}</div>
                        </div>
                    </div>
                    <div class="pl-badge ${isProfit ? 'pl-positive' : 'pl-negative'}">
                        ${isProfit ? '+' : ''}৳${s.total_profit_loss}
                    </div>
                </div>
                <div class="detail-grid">
                    <div class="detail-item">
                        <div class="dl">সময়কাল</div>
                        <div class="dv">${s.duration}</div>
                    </div>
                    <div class="detail-item">
                        <div class="dl">Round</div>
                        <div class="dv">${s.round_count}</div>
                    </div>
                    <div class="detail-item">
                        <div class="dl">মোট Bet</div>
                        <div class="dv">৳${s.total_bet}</div>
                    </div>
                    <div class="detail-item">
                        <div class="dl">মোট Win</div>
                        <div class="dv">৳${s.total_win}</div>
                    </div>
                </div>
            `;
            list.appendChild(div);
        });

        document.getElementById('sumCount').textContent = data.sessions.length;
        document.getElementById('sumBet').textContent = '৳' + totalBet.toLocaleString('en-IN', {maximumFractionDigits: 0});
        const profitEl = document.getElementById('sumProfit');
        profitEl.textContent = (totalProfit >= 0 ? '+' : '') + '৳' + totalProfit.toLocaleString('en-IN', {maximumFractionDigits: 0});
        profitEl.className = 'val ' + (totalProfit >= 0 ? 'green' : 'red');

    } catch (e) {
        list.innerHTML = `<div class="empty-state"><i class="fa-solid fa-triangle-exclamation"></i><p>একটা সমস্যা হয়েছে: ${e.message}</p></div>`;
    }
}

loadHistory();
</script>

</body>
</html>
