<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>Safe / Vault - CV111</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
    body { background: #0b1d1a; color: #d8d3c8; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 0; }
    .header-bg { background: linear-gradient(160deg, #133a2e 0%, #0b1d1a 80%); padding: 18px 16px; border-bottom: 1px solid rgba(0,230,118,.15); position: sticky; top: 0; display: flex; align-items: center; gap: 12px; }
    .header-bg a { color: #ffd54f; text-decoration: none; font-size: 20px; }
    .header-bg span { color: #fff; font-weight: 800; font-size: 17px; }
    main { max-width: 600px; margin: 0 auto; padding: 20px 16px 50px; }
    .safe-card { background: linear-gradient(135deg, #133a2e, #0b1d1a); border: 1px solid rgba(0,230,118,.3); border-radius: 18px; padding: 24px; text-align: center; margin-bottom: 20px; }
    .safe-card i { font-size: 32px; color: #ffd54f; margin-bottom: 10px; }
    .safe-amount { font-size: 30px; font-weight: 900; color: #fff; margin: 8px 0; }
    .card { background: #102620; border: 1px solid rgba(0,230,118,.15); border-radius: 14px; padding: 18px; margin-bottom: 14px; }
    label { display: block; color: #9fb8ae; font-size: 12.5px; margin-bottom: 6px; }
    input { width: 100%; padding: 12px; border-radius: 10px; border: 1px solid rgba(0,230,118,.2); background: #0b1d1a; color: #fff; font-size: 14px; margin-bottom: 12px; }
    button { width: 100%; padding: 13px; border-radius: 10px; border: none; font-weight: 800; font-size: 14px; cursor: pointer; }
    .btn-in { background: linear-gradient(135deg, #00c853, #00e676); color: #06251c; }
    .btn-out { background: rgba(255,213,79,.15); border: 1px solid rgba(255,213,79,.3); color: #ffd54f; }
    .msg-box { border-radius: 10px; padding: 12px; margin-bottom: 14px; font-size: 12.5px; font-weight: 700; text-align: center; display: none; }
</style>
</head>
<body>
<div class="header-bg"><a href="/newprofile.php"><i class="fa-solid fa-arrow-left"></i></a><span>Safe / Vault</span></div>
<main>
    <div class="safe-card">
        <i class="fa-solid fa-vault"></i>
        <div style="color:#9fb8ae; font-size:12px; font-weight:700;">SAFE BALANCE</div>
        <div class="safe-amount">৳ <span id="safeAmount">...</span></div>
        <p style="color:#9fb8ae; font-size:12px;">এখানে রাখা টাকা দিয়ে সরাসরি Bet করা যায় না — এটা Impulse-Bet থেকে রক্ষা করে।</p>
    </div>

    <div id="msgBox" class="msg-box"></div>

    <div class="card">
        <h3 style="color:#fff; font-size:14px; margin:0 0 12px;">Main থেকে Safe-এ পাঠাও</h3>
        <label>Amount</label>
        <input type="number" id="inAmount" placeholder="যত টাকা Safe-এ রাখবে">
        <button class="btn-in" onclick="safeIn()">Safe-এ রাখো</button>
    </div>

    <div class="card">
        <h3 style="color:#fff; font-size:14px; margin:0 0 12px;">Safe থেকে Main-এ ফেরত আনো</h3>
        <label>Amount</label>
        <input type="number" id="outAmount" placeholder="যত টাকা ফেরত আনবে">
        <button class="btn-out" onclick="safeOut()">ফেরত আনো</button>
    </div>
</main>

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

async function loadSafeInfo() {
    try {
        const res = await fetch('/deepanshu/api/webapi/GetSafeInfo.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ language: 1, random: 'x', timestamp: Date.now() })
        });
        const data = await res.json();
        if (data.code === 0 && data.data) {
            document.getElementById('safeAmount').textContent = Number(data.data.amount || data.data.safeAmount || 0).toLocaleString('en-IN', {minimumFractionDigits: 2});
        }
    } catch (e) {
        document.getElementById('safeAmount').textContent = '--';
    }
}

function showMsg(text, ok) {
    const box = document.getElementById('msgBox');
    box.textContent = text;
    box.className = 'msg-box';
    box.style.display = 'block';
    box.style.background = ok ? 'rgba(0,230,118,.1)' : 'rgba(232,50,60,.1)';
    box.style.color = ok ? '#00e676' : '#e8323c';
}

async function safeIn() {
    const amount = document.getElementById('inAmount').value;
    if (!amount || amount <= 0) { showMsg('সঠিক Amount দাও।', false); return; }
    try {
        const res = await fetch('/deepanshu/api/webapi/SetSafeInto.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ amount: Number(amount) })
        });
        const data = await res.json();
        if (data.code === 0) {
            showMsg('✅ Safe-এ যোগ হয়েছে!', true);
            document.getElementById('inAmount').value = '';
            loadSafeInfo();
        } else {
            showMsg('❌ ' + (data.msg || 'ব্যর্থ হয়েছে।'), false);
        }
    } catch (e) { showMsg('একটা সমস্যা হয়েছে।', false); }
}

async function safeOut() {
    const amount = document.getElementById('outAmount').value;
    if (!amount || amount <= 0) { showMsg('সঠিক Amount দাও।', false); return; }
    try {
        const res = await fetch('/deepanshu/api/webapi/SetSafeBack.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
            body: JSON.stringify({ amount: Number(amount) })
        });
        const data = await res.json();
        if (data.code === 0) {
            showMsg('✅ ফেরত এসেছে!', true);
            document.getElementById('outAmount').value = '';
            loadSafeInfo();
        } else {
            showMsg('❌ ' + (data.msg || 'ব্যর্থ হয়েছে।'), false);
        }
    } catch (e) { showMsg('একটা সমস্যা হয়েছে।', false); }
}

loadSafeInfo();
</script>
</body>
</html>
