<?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">ব্যালেন্স</a>
        <a href="payments.php">পেমেন্ট</a>
        <a href="returns.php" class="active">রিটার্ন</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="returns-msg" class="msg" style="display:none;"></div>
    <button type="button" id="btn-returns">রিটার্ন লোড করুন</button>
    <div id="returns-loading" class="loading-wrap" style="display:none;"><div class="loading-spinner"></div><p>লোড হচ্ছে...</p></div>
    <div id="returns-result"></div>
</div>
<script>
document.getElementById('btn-returns').addEventListener('click', function() {
    var msg = document.getElementById('returns-msg');
    var loading = document.getElementById('returns-loading');
    var resultDiv = document.getElementById('returns-result');
    msg.style.display = 'none';
    resultDiv.innerHTML = '';
    loading.style.display = 'block';
    fetch('api.php?action=return_requests').then(function(r) { return r.json(); }).then(function(data) {
        loading.style.display = 'none';
        if (data.message) {
            msg.style.display = 'block';
            msg.className = 'msg error';
            msg.textContent = data.message;
            return;
        }
        var arr = Array.isArray(data) ? data : (data.data || []);
        if (arr.length > 0) {
            var table = '<table class="data-table"><thead><tr><th>#</th><th>আইডি</th><th>কনসাইনমেন্ট</th><th>স্ট্যাটাস</th><th>তারিখ</th></tr></thead><tbody>';
            arr.forEach(function(row, i) {
                table += '<tr><td>' + (i+1) + '</td><td>' + (row.id || '-') + '</td><td>' + (row.consignment_id || '-') + '</td><td>' + (row.status || '-') + '</td><td>' + (row.created_at || '-') + '</td></tr>';
            });
            table += '</tbody></table>';
            resultDiv.innerHTML = table + '<p class="toggle-raw" id="ret-raw-toggle">Raw JSON দেখুন</p><pre id="ret-raw" class="pre-wrap" style="display:none;"></pre>';
            document.getElementById('ret-raw').textContent = JSON.stringify(data, null, 2);
            document.getElementById('ret-raw-toggle').onclick = function() {
                var pre = document.getElementById('ret-raw');
                pre.style.display = pre.style.display === 'none' ? 'block' : 'none';
                this.textContent = pre.style.display === 'none' ? 'Raw JSON দেখুন' : 'Raw JSON লুকান';
            };
        } else {
            resultDiv.innerHTML = '<p class="msg info">কোন রিটার্ন রিকোয়েস্ট নেই।</p><pre class="pre-wrap">' + JSON.stringify(data, null, 2) + '</pre>';
        }
    }).catch(function() {
        loading.style.display = 'none';
        msg.style.display = 'block';
        msg.className = 'msg error';
        msg.textContent = 'নেটওয়ার্ক ত্রুটি।';
    });
});
</script>
</body>
</html>
