{{--
    文件功能：任职期间登录记录子页
    展示某个用户在职期间的每次登录时间、在线时长、IP 和房间

    @author   ChatRoom Laravel
    @version  1.0.0
--}}

@extends('admin.layouts.app')

@section('title', '在职登录日志 · ' . $userPosition->user->username)

@section('content')
    <div class="mb-6">
        <a href="{{ route('admin.appointments.index') }}" class="text-sm text-indigo-600 hover:underline">← 返回任命管理</a>
        <h2 class="text-lg font-bold text-gray-800 mt-2">
            {{ $userPosition->position->icon }} {{ $userPosition->user->username }} · {{ $userPosition->position->name }}
        </h2>
        <p class="text-sm text-gray-500 mt-1">
            任命于 {{ $userPosition->appointed_at->format('Y-m-d') }}，
            任命人：{{ $userPosition->appointedBy?->username ?? '系统' }}
        </p>
    </div>

    {{-- 统计摘要 --}}
    <div class="grid grid-cols-3 gap-4 mb-6">
        <div class="bg-white rounded-xl p-5 border shadow-sm text-center">
            <div class="text-2xl font-bold text-indigo-600">{{ $logs->total() }}</div>
            <div class="text-xs text-gray-500 mt-1">总登录次数</div>
        </div>
        <div class="bg-white rounded-xl p-5 border shadow-sm text-center">
            <div class="text-2xl font-bold text-green-600">
                {{ intdiv($totalSeconds, 3600) }}h {{ intdiv($totalSeconds % 3600, 60) }}m
            </div>
            <div class="text-xs text-gray-500 mt-1">累计在线时长（所有）</div>
        </div>
        <div class="bg-white rounded-xl p-5 border shadow-sm text-center">
            <div class="text-2xl font-bold text-orange-600">{{ $userPosition->total_rewarded_coins }}</div>
            <div class="text-xs text-gray-500 mt-1">在职期间发放金币</div>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border overflow-hidden">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 text-gray-600 text-xs">
                <tr>
                    <th class="px-4 py-3 text-left">登录时间</th>
                    <th class="px-4 py-3 text-left">退出时间</th>
                    <th class="px-4 py-3 text-center">在线时长</th>
                    <th class="px-4 py-3 text-center">房间</th>
                    <th class="px-4 py-3 text-left">IP 地址</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                @forelse ($logs as $log)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3 text-gray-700">{{ $log->login_at->format('m-d H:i:s') }}</td>
                        <td class="px-4 py-3 text-gray-500">{{ $log->logout_at?->format('m-d H:i:s') ?? '在线中...' }}</td>
                        <td class="px-4 py-3 text-center">
                            @if ($log->duration_seconds)
                                <span
                                    class="text-xs bg-blue-100 text-blue-700 px-2 py-0.5 rounded">{{ $log->formatted_duration }}</span>
                            @else
                                <span class="text-xs text-gray-400">—</span>
                            @endif
                        </td>
                        <td class="px-4 py-3 text-center text-gray-500">{{ $log->room_id ? "房间#{$log->room_id}" : '—' }}
                        </td>
                        <td class="px-4 py-3 text-gray-400 font-mono text-xs">{{ $log->ip_address }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5" class="px-4 py-12 text-center text-gray-400">暂无登录记录</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    <div class="mt-4">{{ $logs->links() }}</div>
@endsection
