{extends file="layout.tpl"}

{block name="content"}
<div class="container-fluid">
    <!-- Header -->
    <div class="mb-4">
        <h4 class="mb-1">Aktywne sesje</h4>
        <p class="text-muted mb-0">Monitorowanie aktywnych użytkowników</p>
    </div>

    <!-- Stats -->
    <div class="row g-4 mb-4">
        <div class="col-md-4">
            <div class="stat-card stat-card-success">
                <div class="stat-icon">
                    <i class="bi bi-circle-fill text-success"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$stats.active_now}</div>
                    <div class="stat-label">Aktywnych teraz</div>
                </div>
                <div class="stat-footer">
                    <small>Aktywność w ciągu ostatnich 10 minut</small>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card stat-card-primary">
                <div class="stat-icon">
                    <i class="bi bi-person-badge"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$stats.total_sessions}</div>
                    <div class="stat-label">Wszystkich sesji</div>
                </div>
                <div class="stat-footer">
                    <small>Niewygasłe sesje</small>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card stat-card-warning">
                <div class="stat-icon">
                    <i class="bi bi-clock-history"></i>
                </div>
                <div class="stat-content">
                    <div class="stat-value">{$stats.remember_me_sessions}</div>
                    <div class="stat-label">Zapamiętaj mnie</div>
                </div>
                <div class="stat-footer">
                    <small>Sesje z włączonym "zapamiętaj mnie"</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Active Now -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="card-title mb-0">
                <i class="bi bi-circle-fill text-success me-2" style="font-size: 0.6rem;"></i>
                Aktywni teraz <span class="badge bg-success ms-2">{$stats.active_now}</span>
            </h5>
            <small class="text-muted">Ostatnia aktywność &lt; 10 minut</small>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th>Użytkownik</th>
                            <th>IP</th>
                            <th>Ostatnia aktywność</th>
                            <th>Zapamiętaj</th>
                            <th>Zalogowany od</th>
                            <th class="text-end">Akcje</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $activeSessions as $session}
                        <tr>
                            <td>
                                <strong>{$session.username}</strong>
                                {if $session.role == 'admin'}
                                <span class="badge bg-warning text-dark ms-1">Admin</span>
                                {/if}
                            </td>
                            <td><code>{$session.ip}</code></td>
                            <td>
                                <span class="text-success">
                                    <i class="bi bi-circle-fill me-1" style="font-size: 0.5rem;"></i>
                                    {$session.last_activity_local|date_format:"%H:%M:%S"}
                                </span>
                            </td>
                            <td>
                                {if $session.remember_me}
                                <span class="badge bg-info"><i class="bi bi-check"></i> Tak</span>
                                {else}
                                <span class="badge bg-secondary">Nie</span>
                                {/if}
                            </td>
                            <td>{$session.created_at_local|date_format:"%d.%m.%Y %H:%M"}</td>
                            <td class="text-end">
                                <button type="button" 
                                        class="btn btn-sm btn-outline-danger" 
                                        class="terminate-btn" 
                                        
                                        data-session-id="{$session.id}"
                                        data-username="{$session.username}"
                                        title="Zakończ sesję">
                                    <i class="bi bi-x-circle"></i>
                                </button>
                            </td>
                        </tr>
                        {foreachelse}
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">
                                <i class="bi bi-moon-stars me-2"></i>Brak aktywnych użytkowników
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- All Sessions -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">
                <i class="bi bi-list-ul me-2"></i>
                Wszystkie sesje <span class="badge bg-secondary ms-2">{$stats.total_sessions}</span>
            </h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th>Użytkownik</th>
                            <th>IP</th>
                            <th>Ostatnia aktywność</th>
                            <th>Status</th>
                            <th>Zapamiętaj</th>
                            <th>Przeglądarka</th>
                            <th class="text-end">Akcje</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $allSessions as $session}
                        {assign var="isActive" value=false}
                        {foreach $activeSessions as $active}
                            {if $active.id == $session.id}
                                {assign var="isActive" value=true}
                            {/if}
                        {/foreach}
                        <tr class="{if $isActive}table-success{/if}">
                            <td>
                                <strong>{$session.username}</strong>
                                {if $session.role == 'admin'}
                                <span class="badge bg-warning text-dark ms-1">Admin</span>
                                {/if}
                            </td>
                            <td><code>{$session.ip}</code></td>
                            <td>
                                {if $isActive}
                                <span class="text-success">
                                    <i class="bi bi-circle-fill me-1" style="font-size: 0.5rem;"></i>
                                    {$session.last_activity_local|date_format:"%H:%M:%S"}
                                </span>
                                {else}
                                <span class="text-muted">{$session.last_activity_local|date_format:"%d.%m %H:%M"}</span>
                                {/if}
                            </td>
                            <td>
                                {if $isActive}
                                <span class="badge bg-success">Online</span>
                                {else}
                                <span class="badge bg-secondary">Offline</span>
                                {/if}
                            </td>
                            <td>
                                {if $session.remember_me}
                                <span class="badge bg-info"><i class="bi bi-check"></i></span>
                                {else}
                                <span class="badge bg-secondary">-</span>
                                {/if}
                            </td>
                            <td>
                                <small class="text-muted" title="{$session.user_agent}">
                                    {$session.user_agent|truncate:30}
                                </small>
                            </td>
                            <td class="text-end">
                                <button type="button" 
                                        class="btn btn-sm btn-outline-danger" 
                                        class="terminate-btn" 
                                        
                                        data-session-id="{$session.id}"
                                        data-username="{$session.username}"
                                        title="Zakończ sesję">
                                    <i class="bi bi-x-circle"></i>
                                </button>
                            </td>
                        </tr>
                        {foreachelse}
                        <tr>
                            <td colspan="7" class="text-center py-4 text-muted">
                                Brak aktywnych sesji
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Hidden form -->
<form id="terminateForm" method="post" action="{$baseUrl}/admin/sessions/terminate" style="display:none;">
    <input type="hidden" name="csrf_token" value="{$csrfToken}">
    <input type="hidden" name="session_id" id="terminateSessionId">
</form>
{/block}

{block name="scripts"}
{literal}
<script>
(function() {
    var swalConfig = {
        background: '#1a1d21',
        color: '#e1e1e1',
        confirmButtonColor: '#0d6efd',
        cancelButtonColor: '#6c757d'
    };

    // Terminate session
    document.querySelectorAll('.terminate-btn').forEach(function(btn) {
        btn.addEventListener('click', function() {
            var sessionId = this.dataset.sessionId;
            var username = this.dataset.username;

            Swal.fire({
                title: 'Zakończ sesję',
                html: 'Czy na pewno chcesz zakończyć sesję użytkownika <strong>"' + username + '"</strong>?<br><br><small class="text-muted">Użytkownik zostanie natychmiast wylogowany.</small>',
                icon: 'question',
                showCancelButton: true,
                confirmButtonText: 'Zakończ sesję',
                cancelButtonText: 'Anuluj',
                confirmButtonColor: '#dc3545',
                background: swalConfig.background,
                color: swalConfig.color
            }).then(function(result) {
                if (result.isConfirmed) {
                    document.getElementById('terminateSessionId').value = sessionId;
                    document.getElementById('terminateForm').submit();
                }
            });
        });
    });

    // Auto-refresh co 30 sekund
    setTimeout(function() {
        location.reload();
    }, 30000);
})();
</script>
{/literal}
{/block}
