<?php
require_once __DIR__ . '/helpers.php';
requireLogin();
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ব্যালেন্স - Steadfast</title>
    <link rel="stylesheet" href="style.css">
</head>
<body class="standalone-page">
<div class="container">
    <div class="nav-wrap">
    <nav id="main-nav">
        <a href="index.php">হোম</a>
        <a href="order.php">অর্ডার</a>
        <a href="status.php">স্ট্যাটাস</a>
        <a href="balance.php" class="active">ব্যালেন্স</a>
        <a href="payments.php">পেমেন্ট</a>
        <a href="returns.php">রিটার্ন</a>
        <a href="return_create.php">রিটার্ন রিকোয়েস্ট</a>
        <a href="police.php">পুলিশ স্টেশন</a>
        <a href="settings.php">সেটিংস</a>
        <span class="nav-user"><?php echo htmlspecialchars($_SESSION['user_email']); ?></span>
        <a href="auth.php?action=logout" class="nav-logout">লগআউট</a>
        <button type="button" class="nav-toggle" id="nav-toggle" aria-label="মেনু">☰</button>
    </nav>
    </div>
    <script>(function(){ var t=document.getElementById('nav-toggle'); var n=document.getElementById('main-nav'); if(t&&n) t.addEventListener('click',function(){ n.classList.toggle('open'); }); })();</script>
    <h1>ব্যালেন্স</h1>
    <div id="balance-msg" class="msg" style="display:none;"></div>
    <button type="button" id="btn-balance">ব্যালেন্স দেখুন</button>
    <div id="balance-loading" class="loading-wrap" style="display:none;"><div class="loading-spinner"></div><p>লোড হচ্ছে...</p></div>
    <div id="balance-card" class="balance-card" style="display:none;">
        <span class="label">বর্তমান ব্যালেন্স</span>
        <p class="amount" id="balance-amount">০ BDT</p>
    </div>
    <div id="balance-result"></div>
</div>
<script>
document.getElementById('btn-balance').addEventListener('click', async function() {
    var msg = document.getElementById('balance-msg');
    var loading = document.getElementById('balance-loading');
    var card = document.getElementById('balance-card');
    var amountEl = document.getElementById('balance-amount');
    var resultDiv = document.getElementById('balance-result');
    msg.style.display = 'none';
    card.style.display = 'none';
    resultDiv.innerHTML = '';
    loading.style.display = 'block';
    try {
        var res = await fetch('api.php?action=balance');
        var data = await res.json();
        loading.style.display = 'none';
        if (data.message) {
            msg.style.display = 'block';
            msg.className = 'msg error';
            msg.textContent = data.message;
        } else if (data.current_balance !== undefined) {
            card.style.display = 'block';
            amountEl.textContent = data.current_balance + ' BDT';
            msg.style.display = 'block';
            msg.className = 'msg success';
            msg.textContent = 'সর্বশেষ আপডেট দেখানো হয়েছে।';
        }
        resultDiv.innerHTML = '<p class="toggle-raw" id="bal-raw-toggle">Raw JSON দেখুন</p><pre id="bal-raw" class="pre-wrap" style="display:none;">' + JSON.stringify(data, null, 2) + '</pre>';
        document.getElementById('bal-raw-toggle').onclick = function() {
            var pre = document.getElementById('bal-raw');
            pre.style.display = pre.style.display === 'none' ? 'block' : 'none';
            this.textContent = pre.style.display === 'none' ? 'Raw JSON দেখুন' : 'Raw JSON লুকান';
        };
    } catch (e) {
        loading.style.display = 'none';
        msg.style.display = 'block';
        msg.className = 'msg error';
        msg.textContent = 'নেটওয়ার্ক ত্রুটি। আবার চেষ্টা করুন।';
    }
});
</script>
</body>
</html>
