{{--
    文件功能：后台成就记录页面
    提供固定成就目录、解锁统计与用户成就记录只读查询。
--}}
@extends('admin.layouts.app')

@section('title', '成就记录')

@section('content')
    <div class="space-y-6">
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div class="rounded-lg border border-slate-200 bg-white p-5 shadow-sm">
                <p class="text-sm text-slate-500">固定成就</p>
                <p class="mt-2 text-2xl font-bold text-slate-900">{{ number_format($summary['total_definitions']) }}</p>
            </div>
            <div class="rounded-lg border border-slate-200 bg-white p-5 shadow-sm">
                <p class="text-sm text-slate-500">解锁记录</p>
                <p class="mt-2 text-2xl font-bold text-amber-600">{{ number_format($summary['unlocked_records']) }}</p>
            </div>
            <div class="rounded-lg border border-slate-200 bg-white p-5 shadow-sm">
                <p class="text-sm text-slate-500">解锁用户</p>
                <p class="mt-2 text-2xl font-bold text-emerald-600">{{ number_format($summary['unlocked_users']) }}</p>
            </div>
        </div>

        <div class="grid grid-cols-1 xl:grid-cols-3 gap-6">
            <section class="xl:col-span-2 rounded-lg border border-slate-200 bg-white shadow-sm">
                <div class="border-b border-slate-200 p-4">
                    <form method="GET" class="flex flex-col md:flex-row gap-3">
                        <input type="text" name="username" value="{{ request('username') }}" placeholder="用户名"
                            class="w-full md:w-56 rounded-md border border-slate-300 px-3 py-2 text-sm focus:border-indigo-500 focus:outline-none">
                        <select name="achievement_key"
                            class="w-full md:w-64 rounded-md border border-slate-300 px-3 py-2 text-sm focus:border-indigo-500 focus:outline-none">
                            <option value="">全部成就</option>
                            @foreach ($definitions as $key => $definition)
                                <option value="{{ $key }}" @selected(request('achievement_key') === $key)>
                                    {{ $definition['icon'] }} {{ $definition['name'] }}
                                </option>
                            @endforeach
                        </select>
                        <button type="submit"
                            class="rounded-md bg-indigo-600 px-4 py-2 text-sm font-bold text-white hover:bg-indigo-700">筛选</button>
                        <a href="{{ route('admin.achievements.index') }}"
                            class="rounded-md border border-slate-300 px-4 py-2 text-sm font-bold text-slate-600 hover:bg-slate-50">重置</a>
                    </form>
                </div>

                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-slate-200 text-sm">
                        <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                            <tr>
                                <th class="px-4 py-3">用户</th>
                                <th class="px-4 py-3">成就</th>
                                <th class="px-4 py-3">进度</th>
                                <th class="px-4 py-3">解锁时间</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-100">
                            @forelse ($records as $record)
                                @php
                                    $definition = $definitions[$record->achievement_key] ?? null;
                                    $threshold = (int) data_get($record->metadata, 'threshold', $definition['threshold'] ?? 0);
                                @endphp
                                <tr>
                                    <td class="px-4 py-3 font-semibold text-slate-900">
                                        {{ $record->user?->username ?? '未知用户' }}
                                    </td>
                                    <td class="px-4 py-3">
                                        <div class="font-bold text-slate-800">
                                            {{ $definition['icon'] ?? '🏅' }} {{ $definition['name'] ?? $record->achievement_key }}
                                        </div>
                                        <div class="text-xs text-slate-500">{{ $definition['description'] ?? '' }}</div>
                                    </td>
                                    <td class="px-4 py-3 text-slate-600">
                                        {{ number_format($record->progress_value) }} / {{ number_format($threshold) }}
                                    </td>
                                    <td class="px-4 py-3 text-slate-500">
                                        {{ $record->achieved_at?->format('Y-m-d H:i') }}
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4" class="px-4 py-10 text-center text-slate-500">暂无解锁记录</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                <div class="border-t border-slate-200 p-4">
                    {{ $records->links() }}
                </div>
            </section>

            <section class="rounded-lg border border-slate-200 bg-white p-5 shadow-sm">
                <h2 class="text-base font-bold text-slate-900">热门成就</h2>
                <div class="mt-4 space-y-3">
                    @forelse ($topAchievements as $row)
                        @php $definition = $definitions[$row->achievement_key] ?? null; @endphp
                        <div class="flex items-center justify-between gap-3 rounded-md bg-slate-50 px-3 py-2">
                            <div class="min-w-0">
                                <p class="truncate font-semibold text-slate-800">
                                    {{ $definition['icon'] ?? '🏅' }} {{ $definition['name'] ?? $row->achievement_key }}
                                </p>
                                <p class="text-xs text-slate-500">{{ $definition['description'] ?? '' }}</p>
                            </div>
                            <span class="shrink-0 rounded bg-amber-100 px-2 py-1 text-xs font-bold text-amber-700">
                                {{ number_format($row->unlocked_count) }}
                            </span>
                        </div>
                    @empty
                        <p class="text-sm text-slate-500">暂无热门成就。</p>
                    @endforelse
                </div>
            </section>
        </div>
    </div>
@endsection
