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

    @author   ChatRoom Laravel
    @version  1.0.0
--}}

@extends('admin.layouts.app')

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

@section('content')
    @php require resource_path('views/admin/partials/list-theme.php'); @endphp

    <div class="{{ $adminListPageClass }}">
        <div class="{{ $adminListHeaderCardClass }}">
            <div>
                <a href="{{ route('admin.appointments.index') }}" class="text-sm font-semibold text-indigo-600 hover:text-indigo-700">← 返回任命管理</a>
                <h2 class="mt-2 {{ $adminListHeaderTitleClass }}">
                    {{ $userPosition->position->icon }} {{ $userPosition->user->username }} · {{ $userPosition->position->name }}
                </h2>
                <p class="{{ $adminListHeaderSubtitleClass }}">
                    任命于 {{ $userPosition->appointed_at->format('Y-m-d') }}，任命人：{{ $userPosition->appointedBy?->username ?? '系统' }}
                </p>
            </div>
        </div>

    {{-- 统计摘要 --}}
    <div class="grid gap-4 md:grid-cols-3">
        <div class="{{ $adminListCardClass }} p-5 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="{{ $adminListCardClass }} p-5 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="{{ $adminListCardClass }} p-5 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="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }}">
                <h3 class="{{ $adminListSectionTitleClass }}">在职登录记录</h3>
                <p class="{{ $adminListSectionDescClass }}">记录该任职周期内的登录、退出、在线时长与所在房间信息。</p>
            </div>
            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }} whitespace-nowrap">
                    <thead>
                        <tr class="{{ $adminListTableHeadRowClass }}">
                            <th class="{{ $adminListTableHeadCellClass }}">登录时间</th>
                            <th class="{{ $adminListTableHeadCellClass }}">退出时间</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">在线时长</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">房间</th>
                            <th class="{{ $adminListTableHeadCellClass }}">IP 地址</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                        @forelse ($logs as $log)
                            <tr class="{{ $adminListTableRowClass }}">
                                <td class="px-4 py-3 {{ $adminListPrimaryTextClass }}">{{ $log->login_at->format('m-d H:i:s') }}</td>
                                <td class="px-4 py-3 {{ $adminListBodyTextClass }}">{{ $log->logout_at?->format('m-d H:i:s') ?? '在线中...' }}</td>
                                <td class="px-4 py-3 text-center">
                                    @if ($log->duration_seconds)
                                        <span class="inline-flex items-center rounded-full bg-blue-100 px-2 py-0.5 text-xs font-semibold text-blue-700">{{ $log->formatted_duration }}</span>
                                    @else
                                        <span class="{{ $adminListSecondaryTextClass }}">—</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-center {{ $adminListBodyTextClass }}">{{ $log->room_id ? "房间#{$log->room_id}" : '—' }}</td>
                                <td class="px-4 py-3 {{ $adminListNumericTextClass }} text-gray-400">{{ $log->ip_address }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="{{ $adminListEmptyClass }}">暂无登录记录</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if ($logs->hasPages())
                <div class="{{ $adminListPaginationClass }}">{{ $logs->links() }}</div>
            @endif
        </div>
    </div>
@endsection
