<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<div class="row g-4">
    <div class="col-lg-7">
        <div class="stat-card">
            <div class="d-flex justify-content-between align-items-center mb-3">
                <h1 class="h5 mb-0">Active Sessions</h1>
                <form action="<?= base_url('account/sessions/revoke-others') ?>" method="post" onsubmit="return confirm('Sign out all other devices?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-sm btn-outline-danger">
                        <i class="fa-solid fa-power-off me-1"></i>Force Logout Other Devices
                    </button>
                </form>
            </div>

            <div class="table-responsive">
                <table class="table table-sm align-middle mb-0">
                    <thead>
                        <tr>
                            <th>Device / Browser</th>
                            <th>IP Address</th>
                            <th>Last Activity</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($activeSessions as $s): ?>
                            <tr>
                                <td><?= esc($s['user_agent'] ?? 'Unknown device') ?></td>
                                <td><?= esc($s['ip_address'] ?? '—') ?></td>
                                <td><?= esc($s['last_activity_at'] ?? '—') ?></td>
                                <td>
                                    <?php if (hash('sha256', $currentToken) === $s['session_token']): ?>
                                        <span class="badge text-bg-success">This device</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($activeSessions)): ?>
                            <tr><td colspan="4" class="text-muted text-center py-3">No active sessions.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="col-lg-5">
        <div class="stat-card">
            <h1 class="h5 mb-3">Recent Login History</h1>
            <div class="table-responsive">
                <table class="table table-sm align-middle mb-0">
                    <thead>
                        <tr>
                            <th>When</th>
                            <th>IP</th>
                            <th>Result</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($loginHistory as $h): ?>
                            <tr>
                                <td><?= esc($h['created_at']) ?></td>
                                <td><?= esc($h['ip_address']) ?></td>
                                <td>
                                    <?php if ($h['was_successful']): ?>
                                        <span class="badge text-bg-success">Success</span>
                                    <?php else: ?>
                                        <span class="badge text-bg-danger" title="<?= esc($h['failure_reason'] ?? '') ?>">Failed</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($loginHistory)): ?>
                            <tr><td colspan="3" class="text-muted text-center py-3">No login history yet.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
