@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">查询所有已完成的赛马场次及下注明细。</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-3 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_races']) }}</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['total_bets']) }}</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['total_pool']) }}</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-right text-xs font-bold text-gray-500 uppercase">下注总笔</th>
                        <th class="px-4 py-3 text-right text-xs font-bold text-gray-500 uppercase">注池总额</th>
                        <th class="px-4 py-3 text-right text-xs font-bold text-gray-500 uppercase">操作</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @forelse ($races as $race)
                        @php
                            $horses = $race->horses ?? [];
                            $winner = collect($horses)->firstWhere('id', $race->winner_horse_id);
                            $statusLabel = match ($race->status) {
                                'betting' => ['label' => '押注中', 'color' => 'bg-blue-100 text-blue-700'],
                                'running' => ['label' => '比赛中', 'color' => 'bg-amber-100 text-amber-700'],
                                'settled' => ['label' => '已结算', 'color' => 'bg-emerald-100 text-emerald-700'],
                                'canceled' => ['label' => '已取消', 'color' => 'bg-red-100 text-red-600'],
                                default => ['label' => $race->status, 'color' => 'bg-gray-100 text-gray-500'],
                            };
                        @endphp
                        <tr class="hover:bg-gray-50 transition">
                            <td class="px-4 py-3 text-gray-400 text-xs font-mono">#{{ $race->id }}</td>
                            <td class="px-4 py-3 text-xs text-gray-600">
                                {{ $race->settled_at ? $race->settled_at->format('m-d H:i') : '—' }}
                            </td>
                            <td class="px-4 py-3 text-center text-sm">
                                {{ collect($horses)->pluck('emoji')->implode('') ?: '—' }}
                                <span class="text-xs text-gray-400">（{{ count($horses) }} 匹）</span>
                            </td>
                            <td class="px-4 py-3 text-center font-bold text-indigo-700">
                                @if ($winner)
                                    {{ $winner['emoji'] ?? '' }} {{ $winner['name'] ?? '' }}
                                @else
                                    <span class="text-gray-400">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span class="px-2 py-0.5 rounded-full text-xs font-bold {{ $statusLabel['color'] }}">
                                    {{ $statusLabel['label'] }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-right font-mono text-xs text-indigo-600 font-bold">
                                {{ number_format($race->total_bets ?? 0) }}
                            </td>
                            <td class="px-4 py-3 text-right font-mono text-xs text-amber-600 font-bold">
                                {{ number_format($race->total_pool ?? 0) }}
                            </td>
                            <td class="px-4 py-3 text-right">
                                <a href="{{ route('admin.game-history.horse.race', $race->id) }}"
                                    class="px-3 py-1 bg-indigo-50 text-indigo-700 rounded text-xs font-bold hover:bg-indigo-100 transition">
                                    下注明细
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="px-4 py-10 text-center text-gray-400 text-sm">暂无记录</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>

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