<?php require_once __DIR__ . '/../header.php'; ?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="h3 mb-0">Member Management</h2>
    </div>

    <div class="card border-0 shadow-sm rounded-4">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">User</th>
                            <th>Status</th>
                            <th>IP Addresses</th>
                            <th>Balance</th>
                            <th>Joined</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($users as $user): ?>
                        <tr>
                            <td class="ps-4">
                                <div class="d-flex align-items-center">
                                    <div class="avatar-sm bg-primary bg-opacity-10 text-primary rounded-circle d-flex align-items-center justify-content-center fw-bold me-3" style="width: 40px; height: 40px;">
                                        <?= strtoupper(substr($user['username'], 0, 1)) ?>
                                    </div>
                                    <div>
                                        <div class="fw-bold"><?= htmlspecialchars($user['username']) ?></div>
                                        <small class="text-muted"><?= $user['role'] ?></small>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <?php if ($user['is_banned']): ?>
                                    <span class="badge bg-danger">Banned</span>
                                <?php else: ?>
                                    <span class="badge bg-success">Active</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="small">
                                    <strong>Reg IP:</strong> <?= $user['registration_ip'] ?: 'N/A' ?> 
                                    <a href="https://ip-api.com/#<?= $user['registration_ip'] ?>" target="_blank" class="text-decoration-none ms-1"><i class="bi bi-geo-alt"></i></a>
                                    <br>
                                    <strong>Last IP:</strong> <?= $user['last_ip'] ?: 'N/A' ?>
                                    <a href="https://ip-api.com/#<?= $user['last_ip'] ?>" target="_blank" class="text-decoration-none ms-1"><i class="bi bi-geo-alt"></i></a>
                                </div>
                            </td>
                            <td>Rp <?= number_format($user['balance'], 0, ',', '.') ?></td>
                            <td><?= date('d M Y', strtotime($user['created_at'])) ?></td>
                            <td class="text-end pe-4">
                                <?php if ($user['role'] !== 'admin'): ?>
                                <form action="/admin/users/toggle-ban/<?= $user['id'] ?>" method="POST" class="d-inline" onsubmit="return confirm('Are you sure?')">
                                    <button type="submit" class="btn btn-sm <?= $user['is_banned'] ? 'btn-outline-success' : 'btn-outline-danger' ?>">
                                        <?= $user['is_banned'] ? 'Unban' : 'Ban' ?>
                                    </button>
                                </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../footer.php'; ?>
