<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>Deposit - 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&display=swap">
<style>
  *{box-sizing:border-box;}
  :root{
    --bg:#0D0B14; --card:#171420; --field:#181523; --border:#2C2740;
    --coral:#9B5DE5; --main-grad:linear-gradient(90deg,#4361EE 0%,#7B2FF7 100%);
    --text:#FFFFFF; --muted:#8C89A0; --green:#18B660; --red:#FB5B5B;
  }
  body{margin:0; min-height:100vh; font-family:'Poppins',-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif; color:var(--text); background:var(--bg); padding-bottom:40px;}
  .top{display:flex;align-items:center;gap:14px;padding:18px;background:var(--bg);}
  .top a{color:var(--text);text-decoration:none;font-size:24px;}
  .top h1{font-size:20px;font-weight:700;margin:0;flex:1;text-align:center;padding-right:24px;}
  .wrap{max-width:480px;margin:0 auto;padding:16px;}
  .stat{background:var(--card);border-radius:18px;padding:20px 14px 18px;text-align:center;position:relative;overflow:hidden;margin-bottom:18px;}
  .stat .lab{font-size:14px;font-weight:600;color:var(--text);margin-bottom:10px;}
  .stat .icon-wallet{position:absolute;left:12px;bottom:10px;font-size:32px;opacity:.18;}
  .stat .num{font-family:Georgia,'Times New Roman',serif;font-weight:700;font-size:26px;color:var(--coral);}

  .section-title{font-size:15px;font-weight:700;margin:6px 0 12px;}
  .section-title .req{color:var(--coral);}
  .methods{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-bottom:20px;}
  .method{position:relative;display:flex;align-items:center;gap:12px;background:var(--card);border:1px solid var(--border);border-radius:16px;padding:14px;cursor:pointer;font-weight:600;font-size:14.5px;}
  .method .ic{width:38px;height:38px;border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:800;color:#fff;flex-shrink:0;}
  .method.bkash .ic{background:#E2136E;}
  .method.nagad .ic{background:#EC1D24;}
  .method.active{border-color:var(--coral);background:linear-gradient(120deg,rgba(155,93,229,.22),rgba(155,93,229,.04));box-shadow:0 0 0 1px rgba(155,93,229,.35);}
  .method .check{position:absolute;top:10px;right:10px;width:18px;height:18px;border-radius:50%;background:var(--coral);color:#fff;display:none;align-items:center;justify-content:center;font-size:11px;font-weight:800;}
  .method.active .check{display:flex;}
  .method .m-tag{font-size:11px;color:var(--muted);margin-top:2px;}

  .channel-list{margin-bottom:18px;}
  .channel-item{background:var(--card);border:1px solid var(--border);border-radius:14px;padding:14px;margin-bottom:10px;cursor:pointer;}
  .channel-item.active{border-color:var(--coral);background:linear-gradient(120deg,rgba(155,93,229,.16),rgba(155,93,229,.03));}
  .channel-item .name{font-weight:700;font-size:14px;}
  .channel-item .limit{font-size:11.5px;color:var(--muted);margin-top:3px;}

  .amounts{display:grid;grid-template-columns:repeat(3,1fr);gap:10px;margin-bottom:20px;}
  .amount-chip{background:var(--field);border:1px solid var(--border);border-radius:12px;padding:12px 0;text-align:center;font-weight:700;font-size:14px;cursor:pointer;}
  .amount-chip.active{border-color:var(--coral);color:var(--coral);background:rgba(155,93,229,.1);}

  .btn{width:100%;padding:16px;border-radius:30px;border:none;font-weight:700;font-size:16px;cursor:pointer;margin-top:6px;background:var(--main-grad);color:#fff;}
  .msg{padding:13px 16px;border-radius:12px;font-size:13.5px;margin-bottom:14px;}
  .msg-ok{background:rgba(24,182,96,.14);border:1px solid var(--green);color:var(--green);}
  .msg-err{background:rgba(251,91,91,.14);border:1px solid var(--red);color:#FFAFAF;}
  .center{text-align:center;color:var(--muted);padding:30px 0;}
</style>
</head>
<body>
  <div class="top"><a href="/newhome.php">←</a><h1>Deposit</h1></div>
  <div class="wrap">
    <div id="msgBox"></div>

    <div class="stat">
      <div class="lab">Cash Balance</div>
      <div class="icon-wallet">💰</div>
      <div class="num" id="cashBalance">৳0</div>
    </div>

    <p class="section-title">Payment Method <span class="req">*</span></p>
    <div class="methods">
      <div class="method bkash active" id="dmtd-bKash" onclick="selectMethod('1')"><div class="check">✓</div><div class="ic">bK</div><div><div>bKash</div><div class="m-tag">Instant</div></div></div>
      <div class="method nagad" id="dmtd-Nagad" onclick="selectMethod('2')"><div class="check">✓</div><div class="ic">N</div><div><div>Nagad</div><div class="m-tag">Instant</div></div></div>
    </div>

    <p class="section-title">Select Channel</p>
    <div class="channel-list" id="channelList">
      <div class="center">লোড হচ্ছে...</div>
    </div>

    <p class="section-title">Deposit Amount</p>
    <div class="amounts" id="amountsList"></div>

    <button class="btn" onclick="proceedDeposit()">Continue করো →</button>
  </div>

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

let currentPayId = '2';
let currentChannel = null;
let currentAmount = null;
let allChannels = [];

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);
}

function showMsg(text, ok){
    document.getElementById('msgBox').innerHTML = '<div class="msg ' + (ok ? 'msg-ok' : 'msg-err') + '">' + text + '</div>';
}

async function loadBalance() {
    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('cashBalance').textContent = '৳' + Number(data.data.amount).toLocaleString('en-IN', {maximumFractionDigits: 2});
        }
    } catch (e) {}
}

async function loadChannels(payid) {
    document.getElementById('channelList').innerHTML = '<div class="center">লোড হচ্ছে...</div>';
    const random = Math.random().toString(36).substring(2, 15);
    const shonustr = `{"language":1,"payTypeId":0,"payid":${payid},"random":"${random}"}`;
    const signature = md5(shonustr).toUpperCase();
    try {
        const res = await fetch('/deepanshu/api/webapi/GetRechargeTypes.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ language: 1, payTypeId: 0, payid: Number(payid), random, signature, timestamp: Date.now() })
        });
        const data = await res.json();
        allChannels = (data.data && Object.values(data.data.rechargetypelist || {})) || [];
        renderChannels();
    } catch (e) {
        document.getElementById('channelList').innerHTML = '<div class="center">Load ব্যর্থ হয়েছে।</div>';
    }
}

function renderChannels() {
    const container = document.getElementById('channelList');
    container.innerHTML = '';
    if (allChannels.length === 0) { container.innerHTML = '<div class="center">কোনো Channel নেই।</div>'; return; }
    allChannels.forEach((ch, i) => {
        const div = document.createElement('div');
        div.className = 'channel-item' + (i === 0 ? ' active' : '');
        div.innerHTML = `<div class="name">${ch.payName}</div><div class="limit">Limit: ৳${ch.miniPrice} - ৳${ch.maxPrice}</div>`;
        div.onclick = () => selectChannel(ch, div);
        container.appendChild(div);
    });
    selectChannel(allChannels[0], container.children[0]);
}

function selectChannel(channel, el) {
    document.querySelectorAll('.channel-item').forEach(e => e.classList.remove('active'));
    el.classList.add('active');
    currentChannel = channel;
    renderAmounts(channel);
}

function renderAmounts(channel) {
    const container = document.getElementById('amountsList');
    container.innerHTML = '';
    const list = channel.quickConfigList || [{rechargeAmount: channel.miniPrice}];
    list.forEach((q, i) => {
        const div = document.createElement('div');
        div.className = 'amount-chip' + (i === 0 ? ' active' : '');
        div.textContent = '৳' + q.rechargeAmount;
        div.onclick = () => selectAmount(q.rechargeAmount, div);
        container.appendChild(div);
    });
    if (list[0]) currentAmount = list[0].rechargeAmount;
}

function selectAmount(amount, el) {
    document.querySelectorAll('.amount-chip').forEach(e => e.classList.remove('active'));
    el.classList.add('active');
    currentAmount = amount;
}

function selectMethod(payid) {
    currentPayId = payid;
    document.getElementById('dmtd-bKash').classList.toggle('active', payid === '1');
    document.getElementById('dmtd-Nagad').classList.toggle('active', payid === '2');
    loadChannels(payid);
}

function proceedDeposit() {
    if (!currentChannel || !currentAmount) { showMsg('একটা Channel/Amount বেছে নাও।', false); return; }
    let uid = 0;
    try { uid = JSON.parse(atob(token.split('.')[1])).id || 0; } catch (e) {}
    const targetUrl = currentPayId === '2' ? '/pay/nagad_deposit.php' : '/pay/bkash_deposit.php';
    window.location.href = targetUrl + '?amount=' + currentAmount + '&uid=' + uid;
}

loadBalance();
loadChannels(currentPayId);
</script>
</body>
</html>
