<?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">রিটার্ন</a>
        <a href="return_create.php">রিটার্ন রিকোয়েস্ট</a>
        <a href="police.php" class="active">পুলিশ স্টেশন</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="police-msg" class="msg" style="display:none;"></div>
    <button type="button" id="btn-police">পুলিশ স্টেশন লোড করুন</button>
    <div id="police-loading" class="loading-wrap" style="display:none;"><div class="loading-spinner"></div><p>লোড হচ্ছে...</p></div>
    <div id="police-result"></div>
</div>
<script>
document.getElementById('btn-police').addEventListener('click', function() {
    var msg = document.getElementById('police-msg');
    var loading = document.getElementById('police-loading');
    var resultDiv = document.getElementById('police-result');
    msg.style.display = 'none';
    resultDiv.innerHTML = '';
    loading.style.display = 'block';
    fetch('api.php?action=police_stations').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></tr></thead><tbody>';
            arr.forEach(function(row, i) {
                var name = row.name || row.station_name || row.title || '-';
                var addr = row.address || row.area || row.details || JSON.stringify(row).slice(0, 80);
                table += '<tr><td>' + (i+1) + '</td><td>' + name + '</td><td>' + addr + '</td></tr>';
            });
            table += '</tbody></table>';
            resultDiv.innerHTML = table + '<p class="toggle-raw" id="pol-raw-toggle">Raw JSON দেখুন</p><pre id="pol-raw" class="pre-wrap" style="display:none;"></pre>';
            document.getElementById('pol-raw').textContent = JSON.stringify(data, null, 2);
            document.getElementById('pol-raw-toggle').onclick = function() {
                var pre = document.getElementById('pol-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>
