<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>WinGo - CV111</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
    body { background: #0a0a0a; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
    .gold-gradient { background: linear-gradient(135deg, #d4af37, #f0d264); }
    .header-bg { background: linear-gradient(160deg, #1a1005 0%, #0a0a0a 80%); }
    .card-bg { background: #141414; }
</style>
</head>
<body class="pb-8">

<header class="header-bg border-b border-gray-800 sticky top-0 z-50">
    <div class="max-w-2xl mx-auto px-4 h-14 flex items-center gap-3">
        <a href="/newhome.php" class="text-gray-300 text-xl"><i class="fa-solid fa-arrow-left"></i></a>
        <div class="text-white font-bold text-base">5D Lottery</div>
    </div>
</header>

<main class="max-w-2xl mx-auto px-4 py-5">

    <div class="rounded-xl gold-gradient p-5 mb-5 text-center">
        <div class="text-gray-800 text-xs font-bold">Issue Number</div>
        <div id="issueNumber" class="text-gray-900 text-xl font-black mt-1">...</div>
        <div id="countdown" class="text-gray-900 text-4xl font-black mt-2">--:--</div>
    </div>

    <div id="msgBox" class="hidden rounded-lg p-3 mb-4 text-xs font-bold text-center"></div>

    <div class="text-gray-400 text-xs font-bold mb-2 px-1">প্রথম Position-এর সংখ্যা (0-9)</div>
    <div class="grid grid-cols-5 gap-2 mb-5" id="numberGrid"></div>

    <div class="card-bg rounded-xl p-4 border border-gray-800">
        <label class="text-gray-400 text-xs mb-1 block">Selected: <span id="selectedNum" class="text-yellow-400 font-bold">কিছুই না</span></label>
        <label class="text-gray-400 text-xs mb-1 block mt-2">Amount</label>
        <input type="number" id="betAmount" placeholder="যেমন: 10" class="w-full bg-[#0a0a0a] border border-gray-700 rounded-lg px-4 py-3 text-white text-sm mb-3">
        <button onclick="placeBet()" id="betBtn" class="w-full gold-gradient text-gray-900 font-bold py-3 rounded-lg text-sm">Bet করো</button>
    </div>

</main>

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

let selectedNumber = null;
let currentIssue = null;

for (let i = 0; i <= 9; i++) {
    document.getElementById('numberGrid').innerHTML += `
        <button onclick="selectNumber(${i})" data-num="${i}" class="num-btn card-bg border border-gray-800 rounded-lg py-4 text-white font-black text-lg">${i}</button>
    `;
}

function selectNumber(n) {
    selectedNumber = n;
    document.getElementById('selectedNum').textContent = n;
    document.querySelectorAll('.num-btn').forEach(btn => {
        btn.className = Number(btn.dataset.num) === n
            ? 'num-btn gold-gradient rounded-lg py-4 text-gray-900 font-black text-lg'
            : 'num-btn card-bg border border-gray-800 rounded-lg py-4 text-white font-black text-lg';
    });
}

function md5(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);
}

let countdownInterval;
async function loadIssue() {
    const random = Math.random().toString(36).substring(2, 15);
    const language = 1, typeId = 5;
    const shonustr = `{"language":${language},"random":"${random}","typeId":${typeId}}`;
    const signature = md5(shonustr).toUpperCase();

    try {
        const res = await fetch('/deepanshu/api/webapi/GetGame5DIssue.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ language, random, signature, typeId, timestamp: Date.now() })
        });
        const data = await res.json();
        if (data.code === 0 && data.data) {
            currentIssue = data.data.issueNumber;
            document.getElementById('issueNumber').textContent = currentIssue;
            startCountdown(new Date(data.data.endTime).getTime());
        }
    } catch (e) {}
}

function startCountdown(endTime) {
    clearInterval(countdownInterval);
    countdownInterval = setInterval(() => {
        const diff = Math.max(0, Math.floor((endTime - Date.now()) / 1000));
        const m = Math.floor(diff / 60).toString().padStart(2, '0');
        const s = (diff % 60).toString().padStart(2, '0');
        document.getElementById('countdown').textContent = m + ':' + s;
        if (diff <= 0) {
            clearInterval(countdownInterval);
            setTimeout(loadIssue, 2000);
        }
    }, 1000);
}

async function placeBet() {
    const amount = document.getElementById('betAmount').value;
    const msgBox = document.getElementById('msgBox');
    const btn = document.getElementById('betBtn');

    if (selectedNumber === null) { alert('একটা সংখ্যা বেছে নাও।'); return; }
    if (!amount || amount <= 0) { alert('সঠিক Amount দাও।'); return; }

    btn.textContent = 'Bet হচ্ছে...';
    const random = Math.random().toString(36).substring(2, 15);
    const language = 1, betCount = 1, gameType = 1, typeId = 5, selectType = selectedNumber;
    const shonustr = `{"amount":${amount},"betCount":${betCount},"gameType":${gameType},"issuenumber":"${currentIssue}","language":${language},"random":"${random}","selectType":${selectType},"typeId":${typeId}}`;
    const signature = md5(shonustr).toUpperCase();

    try {
        const res = await fetch('/deepanshu/api/webapi/Game5DBetting.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ amount: Number(amount), betCount, gameType, issuenumber: currentIssue, language, random, selectType, signature, typeId, timestamp: Date.now() })
        });
        const data = await res.json();

        msgBox.classList.remove('hidden');
        if (data.code === 0) {
            msgBox.className = 'rounded-lg p-3 mb-4 text-xs font-bold text-center bg-green-500/10 border border-green-500/40 text-green-400';
            msgBox.textContent = '✅ Bet সফলভাবে হয়েছে!';
        } else {
            msgBox.className = 'rounded-lg p-3 mb-4 text-xs font-bold text-center bg-red-500/10 border border-red-500/40 text-red-400';
            msgBox.textContent = '❌ ' + (data.msg || 'Bet ব্যর্থ হয়েছে।');
        }
    } catch (e) {
        msgBox.classList.remove('hidden');
        msgBox.textContent = 'একটা সমস্যা হয়েছে।';
    }
    btn.textContent = 'Bet করো';
}

loadIssue();
</script>

</body>
</html>
