@extends('admin.layouts.app')

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

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

    @php
        $tableCellClass = 'px-4 py-3';
        $badgeClass = $adminListBadgeBaseClass;
    @endphp

    <div class="{{ $adminListPageClass }}">
        <div class="{{ $adminListHeaderCardClass }} flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
            <div>
                <h2 class="{{ $adminListHeaderTitleClass }}">♟️ 五子棋历史记录</h2>
                <p class="{{ $adminListHeaderSubtitleClass }}">查询全站所有的五子棋人机及 PvP 对局。</p>
            </div>
            <a href="{{ route('admin.game-configs.index') }}" class="{{ $adminListSecondaryButtonClass }}">
                ⚙️ 游戏配置
            </a>
        </div>

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

        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }}">
                <div class="{{ $adminListSectionTitleClass }}">对局列表</div>
                <div class="{{ $adminListSectionDescClass }}">统一表格层次，保留 PvE/PvP、对局状态与胜负视觉语义。</div>
            </div>

            <div class="{{ $adminListTableWrapClass }}">
                <table class="{{ $adminListTableClass }}">
                    <thead>
                        <tr class="{{ $adminListTableHeadRowClass }}">
                            <th class="{{ $adminListTableHeadCellClass }}">赛事ID</th>
                            <th class="{{ $adminListTableHeadCellClass }}">创建时间</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">模式</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">黑方 (发起)</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">白方 (迎战)</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">状态</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">胜负</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                        @forelse ($games as $game)
                            <tr class="{{ $adminListTableRowClass }}">
                                <td class="{{ $tableCellClass }} {{ $adminListPrimaryTextClass }} font-mono">#{{ $game->id }}</td>
                                <td class="{{ $tableCellClass }} {{ $adminListBodyTextClass }}">
                                    {{ $game->created_at ? $game->created_at->format('m-d H:i') : '—' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    @if ($game->mode === 'pve')
                                        <span class="{{ $badgeClass }} border-purple-200 bg-purple-100 text-purple-700">单机 PvE（L{{ $game->ai_level }}）</span>
                                    @else
                                        <span class="{{ $badgeClass }} border-blue-200 bg-blue-100 text-blue-700">在线 PvP</span>
                                    @endif
                                </td>
                                <td class="{{ $tableCellClass }} text-center {{ $adminListPrimaryTextClass }}">
                                    {{ $game->playerBlack?->username ?? '已注销/未知' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center {{ $adminListPrimaryTextClass }}">
                                    @if ($game->mode === 'pve')
                                        <span class="{{ $adminListSecondaryTextClass }}">AI 机器人</span>
                                    @else
                                        {{ $game->playerWhite?->username ?? '已注销/未加入' }}
                                    @endif
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    @php
                                        $s = match ($game->status) {
                                            'waiting' => ['label' => '等待中', 'bg' => 'bg-amber-100 text-amber-600 border-amber-200'],
                                            'playing' => ['label' => '对弈中', 'bg' => 'bg-blue-100 text-blue-600 border-blue-200'],
                                            'finished' => ['label' => '已结束', 'bg' => 'bg-emerald-100 text-emerald-700 border-emerald-200'],
                                            default => ['label' => $game->status, 'bg' => 'bg-gray-100 text-gray-600 border-gray-200'],
                                        };
                                    @endphp
                                    <span class="{{ $badgeClass }} {{ $s['bg'] }}">{{ $s['label'] }}</span>
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    @if ($game->status !== 'finished')
                                        <span class="{{ $adminListSecondaryTextClass }}">-</span>
                                    @else
                                        @if ($game->winner === 1)
                                            <span class="{{ $badgeClass }} border-gray-300 bg-gray-200 text-gray-800">⚫ 黑胜</span>
                                        @elseif ($game->winner === 2)
                                            <span class="{{ $badgeClass }} border-gray-300 bg-white text-gray-700">⚪ 白胜</span>
                                        @else
                                            <span class="{{ $badgeClass }} border-gray-200 bg-gray-100 text-gray-500">平局撤销</span>
                                        @endif
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="{{ $adminListEmptyClass }}">暂无记录</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if ($games->hasPages())
                <div class="{{ $adminListPaginationClass }}">
                    {{ $games->links() }}
                </div>
            @endif
        </div>
    </div>
@endsection
