@extends('layouts.admin')

@section('title', 'Admissions')
@section('heading', 'Admissions')

@section('content')

    <x-card class="mb-6">
        <form method="GET" action="{{ route('admin.admissions.index') }}" class="flex flex-wrap items-end gap-4">
            <div class="min-w-56">
                <x-select name="intake" label="Intake" placeholder="All intakes"
                          :selected="$selectedIntake"
                          :options="$intakes->pluck('name', 'id')->all()" />
            </div>
            <x-button type="submit">Filter</x-button>
        </form>
    </x-card>

    <x-card :padding="false">
        <div class="flex items-center justify-between gap-4 border-b border-slate-200 px-6 py-4">
            <p class="text-sm text-slate-600" role="status">
                <strong class="text-slate-900">{{ number_format($records->total()) }}</strong>
                {{ Str::plural('admission', $records->total()) }} issued
            </p>
        </div>

        @if ($records->isEmpty())
            <div class="px-6 py-12">
                <x-empty-state title="No admissions issued yet"
                               description="Admitted applicants will be listed here with their admission numbers." />
            </div>
        @else
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="border-b border-slate-200 bg-slate-50 text-left text-xs uppercase tracking-wider text-slate-500">
                        <tr>
                            <th scope="col" class="px-6 py-3 font-medium">Admission No.</th>
                            <th scope="col" class="px-6 py-3 font-medium">Applicant</th>
                            <th scope="col" class="px-6 py-3 font-medium">Programme</th>
                            <th scope="col" class="px-6 py-3 font-medium">Intake</th>
                            <th scope="col" class="px-6 py-3 font-medium">Admitted</th>
                            <th scope="col" class="px-6 py-3 font-medium">By</th>
                            <th scope="col" class="px-6 py-3 text-right font-medium">Actions</th>
                        </tr>
                    </thead>

                    <tbody class="divide-y divide-slate-100">
                        @foreach ($records as $record)
                            <tr class="transition hover:bg-slate-50">
                                <td class="whitespace-nowrap px-6 py-3 font-mono text-xs font-medium text-slate-900">
                                    {{ $record->admission_number }}
                                </td>
                                <td class="px-6 py-3 text-slate-800">
                                    {{ $record->application?->applicant?->fullName() }}
                                </td>
                                <td class="px-6 py-3 text-slate-600">{{ $record->programme?->name }}</td>
                                <td class="whitespace-nowrap px-6 py-3 text-xs text-slate-500">{{ $record->intake?->name }}</td>
                                <td class="whitespace-nowrap px-6 py-3 text-xs text-slate-500">
                                    {{ $record->admitted_at?->format('j M Y') }}
                                </td>
                                <td class="px-6 py-3 text-xs text-slate-500">{{ $record->admittedBy?->name }}</td>
                                <td class="whitespace-nowrap px-6 py-3 text-right">
                                    @permission('admissions.generate')
                                        <a href="{{ route('admin.admissions.letter', $record) }}"
                                           class="text-xs font-medium text-brand-700 hover:underline">Letter</a>
                                    @endpermission

                                    @permission('applications.view')
                                        <a href="{{ route('admin.applications.show', $record->application) }}"
                                           class="ml-3 text-xs font-medium text-slate-500 hover:underline">Open</a>
                                    @endpermission
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            <div class="border-t border-slate-200 px-6 py-4">{{ $records->links() }}</div>
        @endif
    </x-card>

@endsection
