<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>داشبورد مدیریت | perspolis</title>
    <script src="https://my-perspolis.com/storage/app/public/3.4.17"></script>
    <style>
        @import url('https://my-perspolis.com/storage/app/public/css2.css');
        * { font-family: 'Vazirmatn', sans-serif; }
    </style>
</head>
<body class="bg-slate-50 font-sans flex h-screen overflow-hidden">

{{-- ─── Sidebar ──────────────────────────────────────────────────── --}}
<aside class="w-64 bg-slate-900 text-white flex flex-col h-screen flex-shrink-0">
    <div class="h-16 flex items-center justify-center border-b border-slate-800">
        <svg class="w-8 h-8 text-emerald-400 ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                  d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
        </svg>
        <h1 class="text-xl font-bold tracking-wider">EduGoal Admin</h1>
    </div>
    <nav class="flex-1 px-4 py-6 space-y-2 overflow-y-auto">
        <a href="{{ route('admin.dashboard') }}"
           class="flex items-center px-4 py-3 bg-emerald-600 text-white rounded-lg">
            <svg class="w-5 h-5 ml-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                      d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
            </svg>
            <span class="font-medium">داشبورد</span>
        </a>
        <a href="{{ route('admin.schools') }}"
           class="flex items-center px-4 py-3 text-slate-300 hover:bg-slate-800 hover:text-white rounded-lg transition">
            <svg class="w-5 h-5 ml-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                      d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/>
            </svg>
            <span class="font-medium">مدارس</span>
        </a>
        <a href="{{ route('admin.packs') }}"
           class="flex items-center px-4 py-3 text-slate-300 hover:bg-slate-800 hover:text-white rounded-lg transition">
            <svg class="w-5 h-5 ml-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                      d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
            </svg>
            <span class="font-medium">پک‌ها</span>
        </a>
        <a href="{{ route('admin.schedules') }}"
           class="flex items-center px-4 py-3 text-slate-300 hover:bg-slate-800 hover:text-white rounded-lg transition">
            <svg class="w-5 h-5 ml-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                      d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>
            </svg>
            <span class="font-medium">زمان‌بندی‌ها</span>
        </a>
        <a href="{{ route('admin.enrollments') }}"
           class="flex items-center px-4 py-3 text-slate-300 hover:bg-slate-800 hover:text-white rounded-lg transition">
            <svg class="w-5 h-5 ml-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                      d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"/>
            </svg>
            <span class="font-medium">ثبت‌نام‌ها</span>
        </a>
    </nav>
    <div class="p-4 border-t border-slate-800">
        <form method="POST" action="#">
            @csrf
            <button type="submit"
                    class="w-full text-right flex items-center gap-2 px-4 py-2 text-slate-400 hover:text-white transition text-sm">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                          d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/>
                </svg>
                خروج
            </button>
        </form>
    </div>
</aside>

{{-- ─── Main Content ─────────────────────────────────────────────── --}}
<div class="flex-1 flex flex-col h-screen overflow-hidden text-slate-800">

    {{-- Header --}}
    <header class="h-16 bg-white shadow-sm flex items-center justify-between px-8 z-10">
        <div>
            <h2 class="text-xl font-bold text-slate-800">داشبورد</h2>
            <p class="text-xs text-slate-400">خوش آمدید، {{ auth()->user()->name }}</p>
        </div>
        <div class="flex items-center gap-4">
            <a href="{{ route('admin.enrollments') }}"
               class="text-sm text-slate-500 hover:text-emerald-600 transition">
                مشاهده همه ثبت‌نام‌ها ←
            </a>
            <div class="w-10 h-10 rounded-full bg-emerald-500 flex items-center justify-center text-white font-bold text-sm">
                {{ strtoupper(substr(auth()->user()->name, 0, 1)) }}
            </div>
        </div>
    </header>

    {{-- Main Scrollable Area --}}
    <main class="flex-1 overflow-x-hidden overflow-y-auto bg-slate-50 p-8">

        {{-- ── Stats Cards ─────────────────────────────────────────── --}}
        <div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-5 mb-8">

            {{-- Total Active Enrollments --}}
            <div class="bg-white rounded-xl shadow-sm p-6 border border-slate-100">
                <div class="flex justify-between items-start">
                    <div>
                        <p class="text-xs font-medium text-slate-500 mb-1">ثبت‌نام‌های فعال</p>
                        <h3 class="text-3xl font-bold text-slate-800">{{ $stats['active_enrollments'] }}</h3>
                        <p class="text-xs text-slate-400 mt-1">از {{ $stats['total_enrollments'] }} کل ثبت‌نام</p>
                    </div>
                    <div class="p-3 bg-emerald-100 text-emerald-600 rounded-lg">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                  d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"/>
                        </svg>
                    </div>
                </div>
            </div>

            {{-- Total Schools --}}
            <div class="bg-white rounded-xl shadow-sm p-6 border border-slate-100">
                <div class="flex justify-between items-start">
                    <div>
                        <p class="text-xs font-medium text-slate-500 mb-1">مدارس تأییدشده</p>
                        <h3 class="text-3xl font-bold text-slate-800">{{ $stats['approved_schools'] }}</h3>
                        <p class="text-xs text-slate-400 mt-1">{{ $stats['pending_schools'] }} در انتظار تأیید</p>
                    </div>
                    <div class="p-3 bg-blue-100 text-blue-600 rounded-lg">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                  d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/>
                        </svg>
                    </div>
                </div>
            </div>

            {{-- Total Packs --}}
            <div class="bg-white rounded-xl shadow-sm p-6 border border-slate-100">
                <div class="flex justify-between items-start">
                    <div>
                        <p class="text-xs font-medium text-slate-500 mb-1">پک‌های فعال</p>
                        <h3 class="text-3xl font-bold text-slate-800">{{ $stats['total_packs'] }}</h3>
                        <p class="text-xs text-slate-400 mt-1">در {{ $stats['approved_schools'] }} مدرسه</p>
                    </div>
                    <div class="p-3 bg-violet-100 text-violet-600 rounded-lg">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                  d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
                        </svg>
                    </div>
                </div>
            </div>

            {{-- Pending Payments --}}
            <div class="bg-white rounded-xl shadow-sm p-6 border border-slate-100">
                <div class="flex justify-between items-start">
                    <div>
                        <p class="text-xs font-medium text-slate-500 mb-1">در انتظار پرداخت</p>
                        <h3 class="text-3xl font-bold text-slate-800">{{ $stats['pending_payment'] }}</h3>
                        <p class="text-xs text-slate-400 mt-1">نیاز به پیگیری</p>
                    </div>
                    <div class="p-3 bg-amber-100 text-amber-600 rounded-lg">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                  d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                    </div>
                </div>
            </div>

        </div>

        {{-- ── Bottom Grid: Recent Enrollments + Pack Summary ────────── --}}
        <div class="grid grid-cols-1 xl:grid-cols-3 gap-6">

            {{-- Recent Enrollments Table (spans 2 cols) --}}
            <div class="xl:col-span-2 bg-white rounded-xl shadow-sm border border-slate-100 overflow-hidden">
                <div class="px-6 py-4 border-b border-slate-100 flex justify-between items-center">
                    <h3 class="text-base font-semibold text-slate-800">آخرین ثبت‌نام‌ها</h3>
                    <a href="{{ route('admin.enrollments') }}"
                       class="text-sm text-emerald-600 hover:text-emerald-700 font-medium">مشاهده همه</a>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full text-right text-sm">
                        <thead>
                        <tr class="bg-slate-50 text-slate-500 text-xs border-b border-slate-100">
                            <th class="px-5 py-3 font-medium">بازیکن</th>
                            <th class="px-5 py-3 font-medium">تلفن</th>
                            <th class="px-5 py-3 font-medium">پک</th>
                            <th class="px-5 py-3 font-medium">وضعیت</th>
                            <th class="px-5 py-3 font-medium">تاریخ</th>
                        </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-50">
                        @forelse($recentEnrollments as $enrollment)
                            @php
                                $statusConfig = [
                                    'active'          => ['فعال',              'bg-emerald-100 text-emerald-700'],
                                    'pending_payment' => ['در انتظار پرداخت', 'bg-amber-100 text-amber-700'],
                                    'cancelled'       => ['لغو شده',           'bg-red-100 text-red-700'],
                                    'draft'           => ['پیش‌نویس',          'bg-slate-100 text-slate-600'],
                                ];
                                [$label, $cls] = $statusConfig[$enrollment->status] ?? [$enrollment->status, 'bg-slate-100 text-slate-600'];
                            @endphp
                            <tr class="hover:bg-slate-50 transition-colors">
                                <td class="px-5 py-3 font-medium text-slate-800">
                                    {{ $enrollment->user->name ?? '—' }}
                                </td>
                                <td class="px-5 py-3 text-slate-500 text-xs font-mono" dir="ltr">
                                    {{ $enrollment->user->phone ?? '—' }}
                                </td>
                                <td class="px-5 py-3 text-slate-600 text-xs">
                                    {{ $enrollment->pack->name ?? '—' }}
                                    <span class="block text-slate-400">
                                        {{ $enrollment->pack->school->name ?? '' }}
                                    </span>
                                </td>
                                <td class="px-5 py-3">
                                    <span class="px-2 py-0.5 rounded-full text-xs font-semibold {{ $cls }}">
                                        {{ $label }}
                                    </span>
                                </td>
                                <td class="px-5 py-3 text-slate-400 text-xs" dir="ltr">
                                    {{ $enrollment->created_at?->format('Y-m-d') }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="5" class="text-center py-10 text-slate-400 text-sm">
                                    هیچ ثبت‌نامی وجود ندارد.
                                </td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

            {{-- Pack Capacity Summary --}}
            <div class="bg-white rounded-xl shadow-sm border border-slate-100 overflow-hidden">
                <div class="px-6 py-4 border-b border-slate-100 flex justify-between items-center">
                    <h3 class="text-base font-semibold text-slate-800">ظرفیت پک‌ها</h3>
                    <a href="{{ route('admin.packs') }}"
                       class="text-sm text-emerald-600 hover:text-emerald-700 font-medium">همه</a>
                </div>
                <div class="divide-y divide-slate-50">
                    @forelse($packCapacities as $pack)
                        @php
                            $percent = $pack->capacity > 0
                                ? min(100, round(($pack->enrollments_count / $pack->capacity) * 100))
                                : 0;
                            $barColor = $percent >= 90 ? 'bg-red-500'
                                      : ($percent >= 70 ? 'bg-amber-400' : 'bg-emerald-500');
                        @endphp
                        <div class="px-5 py-4">
                            <div class="flex justify-between items-center mb-1">
                                <span class="text-sm font-medium text-slate-700 truncate max-w-[60%]">
                                    {{ $pack->name }}
                                </span>
                                <span class="text-xs text-slate-500 shrink-0">
                                    {{ $pack->enrollments_count }} / {{ $pack->capacity }}
                                </span>
                            </div>
                            <div class="w-full bg-slate-100 rounded-full h-2">
                                <div class="{{ $barColor }} h-2 rounded-full transition-all"
                                     style="width: {{ $percent }}%"></div>
                            </div>
                            <p class="text-xs text-slate-400 mt-1">{{ $pack->school->name ?? '—' }}</p>
                        </div>
                    @empty
                        <p class="text-center py-10 text-slate-400 text-sm">پکی یافت نشد.</p>
                    @endforelse
                </div>
            </div>

        </div>

    </main>
</div>

</body>
</html>
