@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-4">
            <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_dropped']) }}</div>
                <div class="mt-1 text-xs text-gray-500">历史总投放数</div>
            </div>
            <div class="rounded-xl border border-emerald-100 bg-emerald-50 p-5 shadow-sm">
                <div class="text-2xl font-bold text-emerald-600">{{ number_format($summary['total_claimed']) }}</div>
                <div class="mt-1 text-xs text-emerald-500">已被领取</div>
            </div>
            <div class="rounded-xl border border-gray-200 bg-gray-50 p-5 shadow-sm">
                <div class="text-2xl font-bold text-gray-500">{{ number_format($summary['total_expired']) }}</div>
                <div class="mt-1 text-xs text-gray-400">已过期/未领取</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">
                    <div class="flex items-center justify-between text-xs">
                        <span class="text-gray-500">📦 普通箱</span>
                        <span class="font-bold">{{ $summary['type_dist']['normal'] ?? 0 }}</span>
                    </div>
                    <div class="flex items-center justify-between text-xs">
                        <span class="text-gray-500">💎 稀有箱</span>
                        <span class="font-bold text-purple-600">{{ $summary['type_dist']['rare'] ?? 0 }}</span>
                    </div>
                    <div class="flex items-center justify-between text-xs">
                        <span class="text-gray-500">☠️ 黑化箱</span>
                        <span class="font-bold text-red-600">{{ $summary['type_dist']['trap'] ?? 0 }}</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="{{ $adminListFilterCardClass }}">
            <div class="{{ $adminListFilterInnerClass }}">
                <form method="GET" class="{{ $adminListFilterFormClass }}">
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">箱子类型</label>
                        <select name="box_type" class="min-w-[130px] {{ $adminListFilterInputClass }}">
                            <option value="">全部类型</option>
                            <option value="normal" {{ request('box_type') === 'normal' ? 'selected' : '' }}>📦 普通箱</option>
                            <option value="rare" {{ request('box_type') === 'rare' ? 'selected' : '' }}>💎 稀有箱</option>
                            <option value="trap" {{ request('box_type') === 'trap' ? 'selected' : '' }}>☠️ 黑化箱</option>
                        </select>
                    </div>
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">状态</label>
                        <select name="status" class="min-w-[120px] {{ $adminListFilterInputClass }}">
                            <option value="">全部状态</option>
                            <option value="open" {{ request('status') === 'open' ? 'selected' : '' }}>等待领取</option>
                            <option value="claimed" {{ request('status') === 'claimed' ? 'selected' : '' }}>已领取</option>
                            <option value="expired" {{ request('status') === 'expired' ? 'selected' : '' }}>已过期</option>
                        </select>
                    </div>
                    <button type="submit" class="{{ $adminListPrimaryButtonClass }}">筛选</button>
                    <a href="{{ route('admin.game-history.mystery-box') }}" 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 }} text-center">类型</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">奖励范围</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">暗号</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">状态</th>
                            <th class="{{ $adminListTableHeadCellClass }}">领取者</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-right">实际奖励</th>
                            <th class="{{ $adminListTableHeadCellClass }}">领取时间</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                        @forelse ($boxes as $box)
                            @php
                                $typeInfo = match ($box->box_type) {
                                    'normal' => ['label' => '📦 普通箱', 'color' => 'bg-emerald-100 text-emerald-700 border-emerald-200'],
                                    'rare' => ['label' => '💎 稀有箱', 'color' => 'bg-purple-100 text-purple-700 border-purple-200'],
                                    'trap' => ['label' => '☠️ 黑化箱', 'color' => 'bg-red-100 text-red-700 border-red-200'],
                                    default => ['label' => '📦 未知', 'color' => 'bg-gray-100 text-gray-600 border-gray-200'],
                                };
                                $statusInfo = match ($box->status) {
                                    'open' => ['label' => '等待领取', 'color' => 'bg-blue-100 text-blue-700 border-blue-200'],
                                    'claimed' => ['label' => '✅ 已领取', 'color' => 'bg-emerald-100 text-emerald-700 border-emerald-200'],
                                    'expired' => ['label' => '⏰ 已过期', 'color' => 'bg-gray-100 text-gray-500 border-gray-200'],
                                    default => ['label' => $box->status, 'color' => 'bg-gray-100 text-gray-500 border-gray-200'],
                                };
                                $claim = $box->claim;
                            @endphp
                            <tr class="{{ $adminListTableRowClass }}">
                                <td class="{{ $tableCellClass }} {{ $adminListSecondaryTextClass }}">
                                    {{ $box->created_at->format('m-d H:i:s') }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    <span class="{{ $badgeClass }} {{ $typeInfo['color'] }}">{{ $typeInfo['label'] }}</span>
                                </td>
                                <td class="{{ $tableCellClass }} text-center {{ $adminListNumericTextClass }} text-gray-600">
                                    {{ number_format($box->reward_min) }} ~ {{ number_format($box->reward_max) }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    <code class="rounded bg-gray-100 px-2 py-0.5 text-xs font-bold text-indigo-700">
                                        {{ $box->passcode ?? '—' }}
                                    </code>
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    <span class="{{ $badgeClass }} {{ $statusInfo['color'] }}">{{ $statusInfo['label'] }}</span>
                                </td>
                                <td class="{{ $tableCellClass }} {{ $adminListPrimaryTextClass }}">
                                    {{ $claim?->user?->username ?? '—' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }}">
                                    @if ($claim)
                                        <span class="{{ $claim->reward_amount > 0 ? 'font-semibold text-emerald-600' : 'font-semibold text-red-500' }}">
                                            {{ $claim->reward_amount > 0 ? '+' : '' }}{{ number_format($claim->reward_amount) }}
                                        </span>
                                    @else
                                        <span class="{{ $adminListSecondaryTextClass }}">—</span>
                                    @endif
                                </td>
                                <td class="{{ $tableCellClass }} {{ $adminListSecondaryTextClass }}">
                                    {{ $claim?->created_at?->format('m-d H:i:s') ?? '—' }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="{{ $adminListEmptyClass }}">暂无记录</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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