@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-2 xl:grid-cols-5">
            <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_rounds']) }}</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_payout']) }}</div>
                <div class="mt-1 text-xs text-gray-500">累计派奖金币</div>
            </div>
            <div class="rounded-xl border border-red-100 bg-white p-5 shadow-sm">
                <div class="text-2xl font-bold text-red-600">{{ number_format($summary['total_lost']) }}</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="mb-2 text-sm font-bold text-gray-700">结果分布</div>
                <div class="space-y-1">
                    @foreach (['big' => '大', 'small' => '小', 'triple' => '豹子', 'kill' => '收割'] as $key => $label)
                        <div class="flex items-center justify-between text-xs">
                            <span class="text-gray-500">{{ $label }}</span>
                            <span class="font-bold text-gray-700">{{ $summary['result_dist'][$key] ?? 0 }} 局</span>
                        </div>
                    @endforeach
                </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-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 ($rounds as $round)
                            <tr class="{{ $adminListTableRowClass }}">
                                <td class="{{ $tableCellClass }} {{ $adminListSecondaryTextClass }} font-mono">#{{ $round->id }}</td>
                                <td class="{{ $tableCellClass }} {{ $adminListBodyTextClass }}">
                                    {{ $round->settled_at ? $round->settled_at->format('m-d H:i') : '—' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center text-lg">
                                    @if ($round->dice1)
                                        {{ $round->dice1 }} {{ $round->dice2 }} {{ $round->dice3 }}
                                    @else
                                        —
                                    @endif
                                </td>
                                <td class="{{ $tableCellClass }} text-center {{ $adminListPrimaryTextClass }}">
                                    {{ $round->total_points ?? '—' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    @php
                                        $resultColors = [
                                            'big' => 'bg-red-100 text-red-700 border-red-200',
                                            'small' => 'bg-blue-100 text-blue-700 border-blue-200',
                                            'triple' => 'bg-purple-100 text-purple-700 border-purple-200',
                                            'kill' => 'bg-gray-100 text-gray-600 border-gray-200',
                                        ];
                                        $label = match ($round->result) {
                                            'big' => '大',
                                            'small' => '小',
                                            'triple' => '豹子',
                                            'kill' => '收割',
                                            default => $round->result ?? '—',
                                        };
                                        $colorClass = $resultColors[$round->result] ?? 'bg-gray-100 text-gray-600 border-gray-200';
                                    @endphp
                                    @if ($round->result)
                                        <span class="{{ $badgeClass }} {{ $colorClass }}">{{ $label }}</span>
                                    @else
                                        <span class="{{ $adminListSecondaryTextClass }}">未结算</span>
                                    @endif
                                </td>
                                <td class="{{ $tableCellClass }} text-center text-sm font-semibold text-indigo-600">
                                    {{ number_format($round->bet_count ?? 0) }}
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} text-gray-500">
                                    <span class="text-red-500">{{ number_format($round->total_bet_big ?? 0) }}</span> /
                                    <span class="text-blue-500">{{ number_format($round->total_bet_small ?? 0) }}</span> /
                                    <span class="text-purple-500">{{ number_format($round->total_bet_triple ?? 0) }}</span>
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} font-semibold text-amber-600">
                                    {{ number_format($round->total_payout ?? 0) }}
                                </td>
                                <td class="{{ $tableCellClass }} text-right">
                                    <a href="{{ route('admin.game-history.baccarat.round', $round->id) }}"
                                        class="{{ $adminListActionButtonClass }} bg-indigo-50 text-indigo-700 hover:bg-indigo-100">
                                        下注明细
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="9" class="{{ $adminListEmptyClass }}">暂无记录</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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