@extends('admin.layouts.app')

@section('title', '五子棋对局记录')

@section('content')
    <div class="space-y-6">

        {{-- 页头 --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 flex justify-between items-center">
            <div>
                <h2 class="text-lg font-bold text-gray-800">♟️ 五子棋历史记录</h2>
                <p class="text-xs text-gray-500 mt-1">查询全站所有的五子棋人机及 PvP 对局。</p>
            </div>
            <a href="{{ route('admin.game-configs.index') }}"
                class="px-4 py-2 bg-gray-100 text-gray-700 rounded-lg text-sm font-bold hover:bg-gray-200 transition">
                ⚙️ 游戏配置
            </a>
        </div>

        {{-- 统计卡片 --}}
        <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5">
                <div class="text-2xl font-bold text-indigo-600">{{ number_format($summary['total_games']) }}</div>
                <div class="text-xs text-gray-500 mt-1">历史对局数</div>
            </div>
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5">
                <div class="text-2xl font-bold text-emerald-600">{{ number_format($summary['completed']) }}</div>
                <div class="text-xs text-gray-500 mt-1">正常完赛</div>
            </div>
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5">
                <div class="text-2xl font-bold text-amber-600">{{ number_format($summary['pvp_count']) }}</div>
                <div class="text-xs text-gray-500 mt-1">PvP 人人对战数</div>
            </div>
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-5">
                <div class="text-2xl font-bold text-rose-600">{{ number_format($summary['today_games']) }}</div>
                <div class="text-xs text-gray-500 mt-1">今日新建对局</div>
            </div>
        </div>

        {{-- 期数列表 --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 border-b border-gray-100">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-bold text-gray-500 uppercase">赛事ID</th>
                        <th class="px-4 py-3 text-left text-xs font-bold text-gray-500 uppercase">创建时间</th>
                        <th class="px-4 py-3 text-center text-xs font-bold text-gray-500 uppercase">模式</th>
                        <th class="px-4 py-3 text-center text-xs font-bold text-gray-500 uppercase">黑方 (发起)</th>
                        <th class="px-4 py-3 text-center text-xs font-bold text-gray-500 uppercase">白方 (迎战)</th>
                        <th class="px-4 py-3 text-center text-xs font-bold text-gray-500 uppercase">状态</th>
                        <th class="px-4 py-3 text-center text-xs font-bold text-gray-500 uppercase">胜负</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @forelse ($games as $game)
                        <tr class="hover:bg-gray-50 transition">
                            <td class="px-4 py-3 text-gray-600 text-xs font-mono font-bold">#{{ $game->id }}</td>
                            <td class="px-4 py-3 text-xs text-gray-600">
                                {{ $game->created_at ? $game->created_at->format('m-d H:i') : '—' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                @if ($game->mode === 'pve')
                                    <span
                                        class="px-2 py-0.5 rounded-full text-xs font-bold bg-purple-100 text-purple-700">单机
                                        PvE（L{{ $game->ai_level }}）</span>
                                @else
                                    <span class="px-2 py-0.5 rounded-full text-xs font-bold bg-blue-100 text-blue-700">在线
                                        PvP</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center font-bold">
                                {{ $game->playerBlack?->username ?? '已注销/未知' }}
                            </td>
                            <td class="px-4 py-3 text-center font-bold">
                                @if ($game->mode === 'pve')
                                    <span class="text-xs text-gray-400">AI 机器人</span>
                                @else
                                    {{ $game->playerWhite?->username ?? '已注销/未加入' }}
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $s = match ($game->status) {
                                        'waiting' => ['label' => '等待中', 'bg' => 'bg-amber-100 text-amber-600'],
                                        'playing' => ['label' => '对弈中', 'bg' => 'bg-blue-100 text-blue-600'],
                                        'finished' => ['label' => '已结束', 'bg' => 'bg-emerald-100 text-emerald-700'],
                                        default => ['label' => $game->status, 'bg' => 'bg-gray-100 text-gray-600'],
                                    };
                                @endphp
                                <span
                                    class="px-2 py-0.5 rounded-full text-xs font-bold {{ $s['bg'] }}">{{ $s['label'] }}</span>
                            </td>
                            <td class="px-4 py-3 text-center">
                                @if ($game->status !== 'finished')
                                    <span class="text-xs text-gray-400">-</span>
                                @else
                                    @if ($game->winner === 1)
                                        <span class="text-xs font-bold text-gray-800 bg-gray-200 px-2 py-1 rounded">⚫
                                            黑胜</span>
                                    @elseif($game->winner === 2)
                                        <span class="text-xs font-bold bg-white border border-gray-300 px-2 py-1 rounded">⚪
                                            白胜</span>
                                    @else
                                        <span
                                            class="text-xs font-bold text-gray-500 bg-gray-100 px-2 py-1 rounded">平局撤销</span>
                                    @endif
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-4 py-10 text-center text-gray-400 text-sm">暂无记录</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>

            @if ($games->hasPages())
                <div class="px-4 py-3 border-t border-gray-100">
                    {{ $games->links() }}
                </div>
            @endif
        </div>
    </div>
@endsection
