<?php
// modules/wallet/widgets/admin_users_status.php
if (basename($_SERVER['PHP_SELF']) == 'admin_users_status.php') { die('Access Denied'); }
?>

<div class="bg-white dark:bg-slate-800 rounded-3xl shadow-sm border border-gray-100 dark:border-slate-700 p-8 border-t-8 border-t-indigo-500 mb-8">
    <h2 class="text-xl font-bold mb-6 text-gray-800 dark:text-white">وضعیت مالی لحظه‌ای کاربران</h2>
    <div class="overflow-x-auto">
        <table class="w-full text-sm text-right text-gray-600 dark:text-gray-400">
            <thead class="bg-gray-100 dark:bg-slate-700/50 text-gray-700 dark:text-gray-300">
                <tr>
                    <th class="px-4 py-3 rounded-tr-xl">نام اپراتور</th>
                    <th class="px-4 py-3">موجودی کیف پول (تومان)</th>
                    <th class="px-4 py-3 text-rose-600 dark:text-rose-400">بدهی اعتباری (تومان)</th>
                    <th class="px-4 py-3 rounded-tl-xl text-fuchsia-600 dark:text-fuchsia-400">سقف اضطراری مجاز</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100 dark:divide-slate-700">
                <?php foreach($usersList as $u): 
                    $displayName = !empty($u['fullname']) ? htmlspecialchars($u['fullname']) : htmlspecialchars($u['username']);
                    $debt = (int)$u['credit_debt'];
                ?>
                    <tr class="hover:bg-gray-50 dark:hover:bg-slate-700/30 transition">
                        <td class="px-4 py-4 font-bold text-gray-800 dark:text-gray-200 flex items-center gap-2">
                            👤 <?php echo $displayName; ?>
                        </td>
                        <td class="px-4 py-4 font-black <?php echo $u['wallet_balance'] > 0 ? 'text-green-600 dark:text-emerald-400' : ($u['wallet_balance'] < 0 ? 'text-red-600 dark:text-rose-500' : 'text-gray-400'); ?>" dir="ltr">
                            <?php echo number_format($u['wallet_balance']); ?>
                        </td>
                        <td class="px-4 py-4 font-black <?php echo $debt > 0 ? 'text-rose-600 bg-rose-50 dark:bg-rose-900/30 dark:text-rose-400 rounded-lg inline-block my-2 px-3' : 'text-gray-300 dark:text-gray-600'; ?>" dir="ltr">
                            <?php echo $debt > 0 ? number_format($debt) : '-'; ?>
                        </td>
                        <td class="px-4 py-4 font-black text-fuchsia-600 dark:text-fuchsia-400" dir="ltr">
                            <?php 
                            if ($u['emergency_amount'] !== null) {
                                echo number_format($u['emergency_amount']) . ' <span class="text-[9px] text-fuchsia-400 dark:text-fuchsia-500 bg-fuchsia-100 dark:bg-fuchsia-900/30 px-1 py-0.5 rounded">اختصاصی</span>';
                            } else {
                                echo number_format($globalEmergency) . ' <span class="text-[9px] text-gray-400 bg-gray-100 dark:bg-slate-700 px-1 py-0.5 rounded">سراسری</span>';
                            }
                            ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>