@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;
        $gradeAll = [
            'jackpot' => ['label' => '上上签', 'color' => 'text-amber-600'],
            'good' => ['label' => '上签', 'color' => 'text-emerald-600'],
            'normal' => ['label' => '中签', 'color' => 'text-gray-500'],
            'bad' => ['label' => '下签', 'color' => 'text-orange-500'],
            'curse' => ['label' => '大凶签', 'color' => 'text-red-600'],
        ];
    @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_times']) }}</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-500">{{ number_format($summary['grade_dist']['jackpot'] ?? 0) }}</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['grade_dist']['curse'] ?? 0) }}</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 ($gradeAll as $key => $meta)
                        <div class="flex items-center justify-between text-xs">
                            <span class="{{ $meta['color'] }}">{{ $meta['label'] }}</span>
                            <span class="font-bold text-gray-700">{{ $summary['grade_dist'][$key] ?? 0 }}</span>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>

        <div class="{{ $adminListFilterCardClass }}">
            <div class="{{ $adminListFilterInnerClass }}">
                <form method="GET" class="{{ $adminListFilterFormClass }}">
                    <div>
                        <label class="{{ $adminListFilterLabelClass }}">签文等级</label>
                        <select name="grade" class="min-w-[130px] {{ $adminListFilterInputClass }}">
                            <option value="">全部等级</option>
                            @foreach ($gradeAll as $key => $meta)
                                <option value="{{ $key }}" {{ request('grade') === $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.fortune') }}" 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 }} w-1/3">签文内容</th>
                            <th class="{{ $adminListTableHeadCellClass }}">加成/减益</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-center">是否免费</th>
                            <th class="{{ $adminListTableHeadCellClass }} text-right">消耗金币</th>
                        </tr>
                    </thead>
                    <tbody class="{{ $adminListTableBodyClass }}">
                        @forelse ($logs as $log)
                            @php
                                $gradeInfo = match ($log->grade) {
                                    'jackpot' => ['label' => '✨ 上上签', 'color' => 'bg-amber-100 text-amber-700 border-amber-200'],
                                    'good' => ['label' => '🌸 上签', 'color' => 'bg-emerald-100 text-emerald-700 border-emerald-200'],
                                    'normal' => ['label' => '📜 中签', 'color' => 'bg-gray-100 text-gray-600 border-gray-200'],
                                    'bad' => ['label' => '😞 下签', 'color' => 'bg-orange-100 text-orange-700 border-orange-200'],
                                    'curse' => ['label' => '💀 大凶签', 'color' => 'bg-red-100 text-red-700 border-red-200'],
                                    default => ['label' => $log->grade, 'color' => 'bg-gray-100 text-gray-500 border-gray-200'],
                                };
                            @endphp
                            <tr class="{{ $adminListTableRowClass }} {{ in_array($log->grade, ['jackpot']) ? 'bg-amber-50/50' : (in_array($log->grade, ['curse']) ? 'bg-red-50/30' : '') }}">
                                <td class="{{ $tableCellClass }} {{ $adminListSecondaryTextClass }}">
                                    {{ $log->created_at->format('m-d H:i') }}
                                </td>
                                <td class="{{ $tableCellClass }} {{ $adminListPrimaryTextClass }}">
                                    {{ $log->user?->username ?? '已注销' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    <span class="{{ $badgeClass }} {{ $gradeInfo['color'] }}">{{ $gradeInfo['label'] }}</span>
                                </td>
                                <td class="{{ $tableCellClass }} text-xs leading-relaxed text-gray-600">
                                    {{ \Illuminate\Support\Str::limit($log->text, 50) }}
                                </td>
                                <td class="{{ $tableCellClass }} text-xs text-gray-500">
                                    {{ $log->buff_desc ?? '—' }}
                                </td>
                                <td class="{{ $tableCellClass }} text-center">
                                    @if ($log->is_free)
                                        <span class="{{ $badgeClass }} border-emerald-200 bg-emerald-100 text-emerald-700">免费</span>
                                    @else
                                        <span class="{{ $badgeClass }} border-amber-200 bg-amber-100 text-amber-700">付费</span>
                                    @endif
                                </td>
                                <td class="{{ $tableCellClass }} text-right {{ $adminListNumericTextClass }} {{ $log->cost > 0 ? 'text-red-500' : 'text-gray-400' }}">
                                    {{ $log->cost > 0 ? '-' . number_format($log->cost) : '0' }}
                                </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
