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

        <div class="{{ $adminListCardClass }}">
            <div class="{{ $adminListSectionHeadClass }}">
                <div class="{{ $adminListSectionTitleClass }}">结果类型分布</div>
                <div class="{{ $adminListSectionDescClass }}">保留中奖结果配色，仅统一卡片容器与排版节奏。</div>
            </div>
            <div class="flex flex-wrap gap-3 p-6">
                @foreach ($resultLabels as $key => $meta)
                    @php
                        $distributionBorderClass = match ($key) {
                            'jackpot' => 'border-yellow-200',
                            'triple_gem' => 'border-blue-200',
                            'triple' => 'border-green-200',
                            'pair' => 'border-indigo-200',
                            'curse' => 'border-red-200',
                            default => 'border-gray-200',
                        };
                    @endphp
                    <div class="rounded-lg border px-4 py-2 {{ $distributionBorderClass }} {{ $meta['color'] }}">
                        <div class="text-sm font-bold">{{ $meta['label'] }}</div>
                        <div class="mt-0.5 text-xs">{{ number_format($summary['result_dist'][$key] ?? 0) }} 次</div>
                    </div>
                @endforeach
            </div>
        </div>

        <div class="{{ $adminListFilterCardClass }}">
            <div class="{{ $adminListFilterInnerClass }}">
                <form method="GET" class="{{ $adminListFilterFormClass }}">
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">结果类型</label>
                        <select name="result_type" class="min-w-[140px] {{ $adminListFilterInputClass }}">
                            <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="{{ $adminListFilterLabelClass }}">玩家名称</label>
                        <input type="text" name="username" value="{{ request('username') }}" placeholder="模糊搜索..."
                            class="w-40 {{ $adminListFilterInputClass }}">
                    </div>
                    <button type="submit" class="{{ $adminListPrimaryButtonClass }}">筛选</button>
                    <a href="{{ route('admin.game-history.slot') }}" class="{{ $adminListSecondaryButtonClass }}">重置</a>
                </form>
            </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 }}">时间</th>
                            <th class="{{ $adminListTableHeadCellClass }}">玩家</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 ($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="{{ $adminListTableRowClass }} {{ $rowBg }}">
                                <td class="{{ $tableCellClass }} {{ $adminListSecondaryTextClass }}">
                                    {{ $log->created_at->format('m-d H:i:s') }}
                                </td>
                                <td class="{{ $tableCellClass }} {{ $adminListPrimaryTextClass }}">
                                    {{ $log->user?->username ?? '已注销' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center text-lg tracking-widest">
                                    {{ $r1Emoji }} {{ $r2Emoji }} {{ $r3Emoji }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    <span class="{{ $badgeClass }} {{ $resultLabels[$log->result_type]['color'] ?? 'bg-gray-100 text-gray-600 border-gray-200' }}">
                                        {{ $resultLabels[$log->result_type]['label'] ?? $log->result_type }}
                                    </span>
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} text-red-500">
                                    -{{ number_format($log->cost) }}
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} {{ $log->payout > 0 ? 'font-semibold text-emerald-600' : 'text-gray-400' }}">
                                    {{ $log->payout > 0 ? '+' . number_format($log->payout) : '0' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} font-semibold {{ $net >= 0 ? 'text-emerald-600' : 'text-red-500' }}">
                                    {{ $net >= 0 ? '+' : '' }}{{ number_format($net) }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="{{ $adminListEmptyClass }}">暂无记录</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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