@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 }}">查询所有已完成的赛马场次及下注明细。</p>
            </div>
            <a href="{{ route('admin.game-configs.index') }}" class="{{ $adminListSecondaryButtonClass }}">
                ⚙️ 游戏配置
            </a>
        </div>

        <div class="grid grid-cols-1 gap-4 md:grid-cols-3">
            <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_races']) }}</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['total_bets']) }}</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['total_pool']) }}</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 }}">统一表格层级，保留场次状态与胜者展示语义。</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-right">下注总笔</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-right">注池总额</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-right">操作</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                        @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 border-blue-200'],
                                    'running' => ['label' => '比赛中', 'color' => 'bg-amber-100 text-amber-700 border-amber-200'],
                                    'settled' => ['label' => '已结算', 'color' => 'bg-emerald-100 text-emerald-700 border-emerald-200'],
                                    'canceled' => ['label' => '已取消', 'color' => 'bg-red-100 text-red-600 border-red-200'],
                                    default => ['label' => $race->status, 'color' => 'bg-gray-100 text-gray-500 border-gray-200'],
                                };
                            @endphp
                            <tr class="{{ $adminListTableRowClass }}">
                                <td class="{{ $tableCellClass }} {{ $adminListSecondaryTextClass }} font-mono">#{{ $race->id }}</td>
                                <td class="{{ $tableCellClass }} {{ $adminListBodyTextClass }}">
                                    {{ $race->settled_at ? $race->settled_at->format('m-d H:i') : '—' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center {{ $adminListBodyTextClass }}">
                                    {{ collect($horses)->pluck('emoji')->implode('') ?: '—' }}
                                    <span class="{{ $adminListSecondaryTextClass }}">（{{ count($horses) }} 匹）</span>
                                </td>
                                <td class="{{ $tableCellClass }} text-center font-semibold text-indigo-700">
                                    @if ($winner)
                                        {{ $winner['emoji'] ?? '' }} {{ $winner['name'] ?? '' }}
                                    @else
                                        <span class="{{ $adminListSecondaryTextClass }}">—</span>
                                    @endif
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    <span class="{{ $badgeClass }} {{ $statusLabel['color'] }}">{{ $statusLabel['label'] }}</span>
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} font-semibold text-indigo-600">
                                    {{ number_format($race->total_bets ?? 0) }}
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} font-semibold text-amber-600">
                                    {{ number_format($race->total_pool ?? 0) }}
                                </td>
                                <td class="{{ $tableCellClass }} text-right">
                                    <a href="{{ route('admin.game-history.horse.race', $race->id) }}"
                                        class="{{ $adminListActionButtonClass }} bg-indigo-50 text-indigo-700 hover:bg-indigo-100">
                                        下注明细
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="{{ $adminListEmptyClass }}">暂无记录</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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