@extends('layouts.sadmin')
@section('title', 'Online Users')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="page-title mb-0">Online Users</h1>
        <p class="text-muted mb-0" style="font-size:13px">
            Users active in the last 30 minutes — {{ now()->format('H:i:s') }}
            <a href="{{ route('sadmin.online-users') }}" class="ms-2 text-primary" style="font-size:12px">
                <i class="bi bi-arrow-clockwise"></i> Refresh
            </a>
        </p>
    </div>
    <span class="badge fs-6 px-3 py-2" style="background:#d1e7dd;color:#0a3622">
        <i class="bi bi-circle-fill me-1" style="font-size:8px"></i>
        {{ $sessions->count() }} Online
    </span>
</div>

{{-- By Company summary --}}
@if($byCompany->isNotEmpty())
<div class="row g-3 mb-4">
    @foreach($byCompany as $companyId => $companySessions)
        @php $companyName = $companySessions->first()->company?->name ?? 'Unknown'; @endphp
        <div class="col-12 col-md-6 col-xl-4">
            <div class="card">
                <div class="card-body py-3">
                    <div class="d-flex align-items-center justify-content-between">
                        <div>
                            <div style="font-size:13px;font-weight:600">{{ $companyName }}</div>
                            <div class="text-muted" style="font-size:11.5px">
                                <code>{{ $companySessions->first()->company?->company_code }}</code>
                            </div>
                        </div>
                        <span class="badge rounded-pill" style="background:#d1e7dd;color:#0a3622;font-size:14px;padding:6px 14px">
                            {{ $companySessions->count() }}
                        </span>
                    </div>
                </div>
            </div>
        </div>
    @endforeach
</div>
@endif

{{-- Full Sessions Table --}}
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span><i class="bi bi-people me-2"></i>Active Sessions</span>
        <form action="{{ route('sadmin.force-logout') }}" method="POST" id="forceLogoutAllForm">
            @csrf
            <input type="hidden" name="session_id" value="ALL">
            <button type="button" class="btn btn-danger btn-sm btn-confirm"
                data-form="#forceLogoutAllForm"
                data-title="Force Logout All?"
                data-message="This will terminate ALL active user sessions immediately."
                data-icon="warning"
                data-color="#E74C3C"
                data-confirm="Force Logout All">
                <i class="bi bi-power me-1"></i>Logout All
            </button>
        </form>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0" id="sessionsTable">
                <thead>
                    <tr>
                        <th>User</th>
                        <th>Company</th>
                        <th>IP Address</th>
                        <th>Logged In</th>
                        <th>Last Active</th>
                        <th>Idle</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($sessions as $session)
                    @php
                        $lastActive = \Carbon\Carbon::createFromTimestamp($session->last_activity);
                        $idleMinutes = $lastActive->diffInMinutes(now());
                    @endphp
                    <tr>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <img src="{{ $session->user?->avatar_url }}"
                                     style="width:30px;height:30px;border-radius:50%;object-fit:cover" alt="">
                                <div>
                                    <div style="font-size:13px;font-weight:600">{{ $session->user?->name ?? 'Unknown' }}</div>
                                    <div class="text-muted" style="font-size:11.5px">
                                        <code>{{ $session->user?->username }}</code>
                                        @if($session->user?->is_admin)
                                            <span class="badge bg-primary ms-1" style="font-size:9px">Admin</span>
                                        @endif
                                    </div>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div style="font-size:13px">{{ $session->company?->name }}</div>
                            <code style="font-size:11px">{{ $session->company?->company_code }}</code>
                        </td>
                        <td><code style="font-size:12px">{{ $session->ip_address }}</code></td>
                        <td>
                            <span class="text-muted" style="font-size:12px">
                                {{ $session->logged_in_at ? \Carbon\Carbon::parse($session->logged_in_at)->format('d M, H:i') : '—' }}
                            </span>
                        </td>
                        <td>
                            <span style="font-size:12px">{{ $lastActive->format('H:i:s') }}</span>
                        </td>
                        <td>
                            @if($idleMinutes < 5)
                                <span class="badge" style="background:#d1e7dd;color:#0a3622;font-size:11px">Active</span>
                            @elseif($idleMinutes < 15)
                                <span class="badge bg-warning text-dark" style="font-size:11px">{{ $idleMinutes }}m idle</span>
                            @else
                                <span class="badge bg-secondary" style="font-size:11px">{{ $idleMinutes }}m idle</span>
                            @endif
                        </td>
                        <td>
                            <form action="{{ route('sadmin.force-logout') }}" method="POST" id="logout-{{ $session->id }}">
                                @csrf
                                <input type="hidden" name="session_id" value="{{ $session->id }}">
                                <button type="button" class="btn btn-outline-danger btn-sm btn-confirm"
                                    data-form="#logout-{{ $session->id }}"
                                    data-title="Force Logout?"
                                    data-message="This will immediately terminate {{ $session->user?->name }}'s session."
                                    data-icon="warning"
                                    data-color="#E74C3C"
                                    data-confirm="Force Logout"
                                    title="Force Logout">
                                    <i class="bi bi-power"></i>
                                </button>
                            </form>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="text-center py-5">
                            <i class="bi bi-people display-6 d-block text-muted mb-2"></i>
                            <span class="text-muted">No users are currently online</span>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
// Auto-refresh every 30 seconds
setTimeout(() => location.reload(), 30000);
</script>
@endpush
