@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-4 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_times']) }}</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-500">{{ number_format($summary['grade_dist']['jackpot'] ?? 0) }}
                </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['grade_dist']['curse'] ?? 0) }}</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">
                    @php
                        $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
                    @foreach ($gradeAll as $key => $meta)
                        <div class="flex 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>

        {{-- 筛选 --}}
        <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="grade"
                    class="border border-gray-300 rounded-lg px-3 py-2 text-sm focus:border-indigo-400 min-w-[130px]">
                    <option value="">全部等级</option>
                    @foreach ($gradeAll as $key => $meta)
                        <option value="{{ $key }}" {{ request('grade') === $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.fortune') }}"
                    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-left text-xs font-bold text-gray-500 uppercase w-1/3">签文内容</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-right text-xs font-bold text-gray-500 uppercase">消耗金币</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @forelse ($logs as $log)
                        @php
                            $gradeInfo = match ($log->grade) {
                                'jackpot' => ['label' => '✨ 上上签', 'color' => 'bg-amber-100 text-amber-700'],
                                'good' => ['label' => '🌸 上签', 'color' => 'bg-emerald-100 text-emerald-700'],
                                'normal' => ['label' => '📜 中签', 'color' => 'bg-gray-100 text-gray-600'],
                                'bad' => ['label' => '😞 下签', 'color' => 'bg-orange-100 text-orange-700'],
                                'curse' => ['label' => '💀 大凶签', 'color' => 'bg-red-100 text-red-700'],
                                default => ['label' => $log->grade, 'color' => 'bg-gray-100 text-gray-500'],
                            };
                        @endphp
                        <tr
                            class="hover:bg-gray-50 transition {{ in_array($log->grade, ['jackpot']) ? 'bg-amber-50/50' : (in_array($log->grade, ['curse']) ? 'bg-red-50/30' : '') }}">
                            <td class="px-4 py-3 text-xs text-gray-400">
                                {{ $log->created_at->format('m-d H:i') }}
                            </td>
                            <td class="px-4 py-3 font-medium text-gray-800">
                                {{ $log->user?->username ?? '已注销' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                <span class="px-2 py-0.5 rounded-full text-xs font-bold {{ $gradeInfo['color'] }}">
                                    {{ $gradeInfo['label'] }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-600 leading-relaxed">
                                {{ \Illuminate\Support\Str::limit($log->text, 50) }}
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-500">
                                {{ $log->buff_desc ?? '—' }}
                            </td>
                            <td class="px-4 py-3 text-center">
                                @if ($log->is_free)
                                    <span
                                        class="px-2 py-0.5 rounded-full text-xs font-bold bg-emerald-100 text-emerald-700">免费</span>
                                @else
                                    <span
                                        class="px-2 py-0.5 rounded-full text-xs font-bold bg-amber-100 text-amber-700">付费</span>
                                @endif
                            </td>
                            <td
                                class="px-4 py-3 text-right font-mono text-xs {{ $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="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
