@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-5 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_rounds']) }}</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_payout']) }}</div>
                <div class="text-xs text-gray-500 mt-1">累计派奖金币</div>
            </div>
            <div class="bg-white rounded-xl shadow-sm border border-red-100 p-5">
                <div class="text-2xl font-bold text-red-600">{{ number_format($summary['total_lost']) }}</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-sm font-bold text-gray-700 mb-2">结果分布</div>
                <div class="space-y-1">
                    @foreach (['big' => '大', 'small' => '小', 'triple' => '豹子', 'kill' => '收割'] as $key => $label)
                        <div class="flex 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="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-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 ($rounds as $round)
                        <tr class="hover:bg-gray-50 transition">
                            <td class="px-4 py-3 text-gray-400 text-xs font-mono">#{{ $round->id }}</td>
                            <td class="px-4 py-3 text-xs text-gray-600">
                                {{ $round->settled_at ? $round->settled_at->format('m-d H:i') : '—' }}
                            </td>
                            <td class="px-4 py-3 text-center text-lg">
                                @if ($round->dice1)
                                    {{ $round->dice1 }} {{ $round->dice2 }} {{ $round->dice3 }}
                                @else
                                    —
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center font-bold text-gray-700">
                                {{ $round->total_points ?? '—' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                @php
                                    $resultColors = [
                                        'big' => 'bg-red-100 text-red-700',
                                        'small' => 'bg-blue-100 text-blue-700',
                                        'triple' => 'bg-purple-100 text-purple-700',
                                        'kill' => 'bg-gray-100 text-gray-600',
                                    ];
                                    $label = match ($round->result) {
                                        'big' => '大',
                                        'small' => '小',
                                        'triple' => '豹子',
                                        'kill' => '收割',
                                        default => $round->result ?? '—',
                                    };
                                    $colorClass = $resultColors[$round->result] ?? 'bg-gray-100 text-gray-600';
                                @endphp
                                @if ($round->result)
                                    <span class="px-2 py-0.5 rounded-full text-xs font-bold {{ $colorClass }}">
                                        {{ $label }}
                                    </span>
                                @else
                                    <span class="text-gray-400 text-xs">未结算</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-center text-xs font-bold text-indigo-600">
                                {{ number_format($round->bet_count ?? 0) }}
                            </td>
                            <td class="px-4 py-3 text-right text-xs text-gray-500 font-mono">
                                <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="px-4 py-3 text-right font-mono text-xs text-amber-600 font-bold">
                                {{ number_format($round->total_payout ?? 0) }}
                            </td>
                            <td class="px-4 py-3 text-right">
                                <a href="{{ route('admin.game-history.baccarat.round', $round->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="9" class="px-4 py-10 text-center text-gray-400 text-sm">暂无记录</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>

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