{{--
    文件功能：我的成就页面
    按分类展示当前用户的固定成就解锁状态与进度。
--}}
@extends('layouts.app')

@section('title', '我的成就 - 飘落流星')

@section('nav-icon', '🏅')
@section('nav-title', '我的成就')

@section('content')
    <main class="p-4 sm:p-6 lg:p-8">
        <div class="max-w-7xl mx-auto flex flex-col gap-6">
            <section class="bg-white border border-gray-200 rounded-lg shadow-sm p-5">
                <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
                    <div>
                        <h2 class="text-xl font-bold text-gray-900">{{ $user->username }} 的成就档案</h2>
                        <p class="text-sm text-gray-500 mt-1">已解锁 {{ $unlocked_count }} / {{ $total_count }} 项</p>
                    </div>
                    <div class="w-full sm:w-64 bg-gray-100 rounded-full h-3 overflow-hidden">
                        <div class="h-3 bg-amber-500"
                            style="width: {{ $total_count > 0 ? min(100, floor($unlocked_count / $total_count * 100)) : 0 }}%">
                        </div>
                    </div>
                </div>
            </section>

            <nav class="bg-white border border-gray-200 rounded-lg shadow-sm p-1 flex flex-col sm:flex-row gap-1"
                aria-label="成就筛选">
                @foreach ($achievement_tabs as $tabKey => $tab)
                    <a href="{{ $tab['url'] }}"
                        class="flex-1 inline-flex items-center justify-center gap-2 rounded-md px-4 py-2.5 text-sm font-semibold {{ $active_tab === $tabKey ? 'bg-gray-900 text-white shadow-sm' : 'text-gray-600 hover:bg-gray-100 hover:text-gray-900' }}"
                        aria-current="{{ $active_tab === $tabKey ? 'page' : 'false' }}">
                        <span>{{ $tab['label'] }}</span>
                        <span
                            class="text-xs px-2 py-0.5 rounded-full {{ $active_tab === $tabKey ? 'bg-white/15 text-white' : 'bg-gray-100 text-gray-500' }}">
                            {{ $tab['count'] }}
                        </span>
                    </a>
                @endforeach
            </nav>

            @foreach ($categories as $categoryKey => $categoryLabel)
                @php
                    $items = $achievements->where('category', $categoryKey)->values();
                @endphp

                @if ($items->isEmpty())
                    @continue
                @endif

                <section class="flex flex-col gap-3">
                    <div class="flex items-center justify-between">
                        <h3 class="text-lg font-bold text-gray-800">{{ $categoryLabel }}成就</h3>
                        <span class="text-xs text-gray-500">
                            {{ $items->where('unlocked', true)->count() }} / {{ $items->count() }}
                        </span>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
                        @foreach ($items as $achievement)
                            <article
                                class="bg-white border {{ $achievement['unlocked'] ? 'border-amber-200' : 'border-gray-200' }} rounded-lg p-4 shadow-sm flex gap-3">
                                <div
                                    class="w-11 h-11 rounded-lg flex items-center justify-center text-2xl shrink-0 {{ $achievement['unlocked'] ? 'bg-amber-100' : 'bg-gray-100 grayscale' }}">
                                    {{ $achievement['icon'] }}
                                </div>
                                <div class="min-w-0 flex-1">
                                    <div class="flex items-center justify-between gap-3">
                                        <h4 class="font-bold text-gray-900 truncate">{{ $achievement['name'] }}</h4>
                                        <span
                                            class="text-xs px-2 py-0.5 rounded {{ $achievement['unlocked'] ? 'bg-emerald-100 text-emerald-700' : 'bg-gray-100 text-gray-500' }}">
                                            {{ $achievement['unlocked'] ? '已解锁' : '进行中' }}
                                        </span>
                                    </div>
                                    <p class="text-sm text-gray-500 mt-1">{{ $achievement['description'] }}</p>
                                    <div class="mt-3 flex items-center gap-3">
                                        <div class="flex-1 h-2 bg-gray-100 rounded-full overflow-hidden">
                                            <div class="h-2 {{ $achievement['unlocked'] ? 'bg-emerald-500' : 'bg-indigo-500' }}"
                                                style="width: {{ $achievement['progress_percent'] }}%"></div>
                                        </div>
                                        <span class="text-xs text-gray-500 whitespace-nowrap">
                                            {{ number_format($achievement['progress_value']) }} /
                                            {{ number_format($achievement['threshold']) }}
                                        </span>
                                    </div>
                                    @if ($achievement['achieved_at'])
                                        <p class="text-xs text-amber-700 mt-2">
                                            解锁于 {{ $achievement['achieved_at']->format('Y-m-d H:i') }}
                                        </p>
                                    @endif
                                </div>
                            </article>
                        @endforeach
                    </div>
                </section>
            @endforeach

            @if ($achievements->isEmpty())
                <section class="bg-white border border-gray-200 rounded-lg shadow-sm p-8 text-center">
                    <h3 class="text-base font-bold text-gray-800">暂无对应成就</h3>
                    <p class="text-sm text-gray-500 mt-2">切换其他筛选查看成就列表。</p>
                </section>
            @endif
        </div>
    </main>
@endsection
