@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_spins']) }}</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-red-500">{{ number_format($summary['total_cost']) }}</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-gray-100 p-5">
                <div class="text-2xl font-bold text-amber-600">{{ number_format($summary['net_income']) }}</div>
                <div class="text-xs text-gray-500 mt-1">庄家净收（消耗-派奖）</div>
            </div>
            <div class="bg-yellow-50 rounded-xl shadow-sm border border-yellow-100 p-5">
                <div class="text-2xl font-bold text-yellow-600">{{ number_format($summary['result_dist']['jackpot'] ?? 0) }}
                </div>
                <div class="text-xs text-yellow-500 mt-1">🎉 三7大奖次数</div>
            </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-3">结果类型分布</div>
            <div class="flex flex-wrap gap-3">
                @php
                    $resultLabels = [
                        'jackpot' => ['label' => '🎉 三个7', 'color' => 'bg-yellow-100 text-yellow-700'],
                        'triple_gem' => ['label' => '💎 三钻', 'color' => 'bg-blue-100 text-blue-700'],
                        'triple' => ['label' => '✨ 三同', 'color' => 'bg-green-100 text-green-700'],
                        'pair' => ['label' => '🎁 两同', 'color' => 'bg-indigo-100 text-indigo-700'],
                        'curse' => ['label' => '☠️ 三骷髅', 'color' => 'bg-red-100 text-red-700'],
                        'miss' => ['label' => '😔 未中奖', 'color' => 'bg-gray-100 text-gray-600'],
                    ];
                @endphp
                @foreach ($resultLabels as $key => $meta)
                    <div class="px-4 py-2 rounded-lg {{ $meta['color'] }}">
                        <div class="text-sm font-bold">{{ $meta['label'] }}</div>
                        <div class="text-xs mt-0.5">{{ number_format($summary['result_dist'][$key] ?? 0) }} 次</div>
                    </div>
                @endforeach
            </div>
        </div>

        {{-- 筛选条件 --}}
        <form method="GET"
            class="bg-white rounded-xl shadow-sm border border-gray-100 p-4 flex flex-wrap items-end gap-4">
            <div>
                <label class="block text-xs font-bold text-gray-600 mb-1">结果类型</label>
                <select name="result_type"
                    class="border border-gray-300 rounded-lg px-3 py-2 text-sm focus:border-indigo-400 min-w-[140px]">
                    <option value="">全部结果</option>
                    @foreach ($resultLabels as $key => $meta)
                        <option value="{{ $key }}" {{ request('result_type') === $key ? 'selected' : '' }}>
                            {{ $meta['label'] }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-xs font-bold text-gray-600 mb-1">玩家名称</label>
                <input type="text" name="username" value="{{ request('username') }}" placeholder="模糊搜索..."
                    class="border border-gray-300 rounded-lg px-3 py-2 text-sm focus:border-indigo-400 w-40">
            </div>
            <div class="flex gap-2">
                <button type="submit"
                    class="px-5 py-2 bg-indigo-600 text-white rounded-lg font-bold hover:bg-indigo-700 transition text-sm">
                    🔍 筛选
                </button>
                <a href="{{ route('admin.game-history.slot') }}"
                    class="px-4 py-2 bg-gray-100 text-gray-700 rounded-lg font-bold hover:bg-gray-200 transition text-sm">
                    重置
                </a>
            </div>
        </form>

        {{-- 记录列表 --}}
        <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">时间</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-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 ($logs as $log)
                        @php
                            $symbols = \App\Models\SlotMachineLog::symbols();
                            $r1Emoji = $symbols[$log->reel1]['emoji'] ?? $log->reel1;
                            $r2Emoji = $symbols[$log->reel2]['emoji'] ?? $log->reel2;
                            $r3Emoji = $symbols[$log->reel3]['emoji'] ?? $log->reel3;
                            $net = $log->payout - $log->cost;
                            $rowBg = match ($log->result_type) {
                                'jackpot' => 'bg-yellow-50',
                                'triple_gem' => 'bg-blue-50',
                                'curse' => 'bg-red-50',
                                default => '',
                            };
                        @endphp
                        <tr class="hover:bg-gray-50 transition {{ $rowBg }}">
                            <td class="px-4 py-3 text-xs text-gray-400">
                                {{ $log->created_at->format('m-d H:i:s') }}
                            </td>
                            <td class="px-4 py-3 font-medium text-gray-800">
                                {{ $log->user?->username ?? '已注销' }}
                            </td>
                            <td class="px-4 py-3 text-center text-lg tracking-widest">
                                {{ $r1Emoji }} {{ $r2Emoji }} {{ $r3Emoji }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span
                                    class="px-2 py-0.5 rounded-full text-xs font-bold {{ $resultLabels[$log->result_type]['color'] ?? 'bg-gray-100 text-gray-600' }}">
                                    {{ $resultLabels[$log->result_type]['label'] ?? $log->result_type }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-right font-mono text-xs text-red-500">
                                -{{ number_format($log->cost) }}
                            </td>
                            <td
                                class="px-4 py-3 text-right font-mono text-xs {{ $log->payout > 0 ? 'text-emerald-600 font-bold' : 'text-gray-400' }}">
                                {{ $log->payout > 0 ? '+' . number_format($log->payout) : '0' }}
                            </td>
                            <td
                                class="px-4 py-3 text-right font-mono text-xs {{ $net >= 0 ? 'text-emerald-600' : 'text-red-500' }} font-bold">
                                {{ $net >= 0 ? '+' : '' }}{{ number_format($net) }}
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-4 py-10 text-center text-gray-400 text-sm">暂无记录</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>

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