@extends('layouts.admin')

@section('content')
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h2>Student Registrations</h2>
        <a href="{{ route('admin.registrations.create') }}" class="btn btn-success">+ New Registration</a>
    </div>

    @if (session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif

    @if($registrations->isEmpty())
        <div class="alert alert-info">
            No registrations found.
        </div>
    @else
        <div class="table-responsive">
            <table class="table table-bordered table-striped align-middle">
                <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>Reg. Number</th>
                        <th>Full Name</th>
                        <th>Programme</th>
                        <th>National ID</th>
                        <th>DOB</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($registrations as $index => $registration)
                        <tr>
                            <td>{{ $index + 1 }}</td>
                            <td>{{ $registration->registration_number }}</td>
                            <td>
                                {{ $registration->user->first_name ?? $registration->first_name }}
                                {{ $registration->user->last_name ?? $registration->last_name }}
                            </td>
                            <td>{{ $registration->programme_code }}</td>
                            <td>{{ $registration->user->national_id ?? $registration->national_id }}</td>
                            <td>
                                {{ optional($registration->user)->dob ? \Carbon\Carbon::parse($registration->user->dob)->format('d M Y') : $registration->dob }}
                            </td>
                            <td>
                                <a href="{{ route('admin.registrations.show', $registration->id) }}" class="btn btn-sm btn-info">View</a>
                                <a href="{{ route('admin.registrations.edit', $registration->id) }}" class="btn btn-sm btn-primary">Edit</a>
                                <form action="{{ route('admin.registrations.destroy', $registration->id) }}" method="POST" style="display:inline-block;">
                                    @csrf
                                    @method('DELETE')
                                    <button onclick="return confirm('Are you sure you want to delete this?')" class="btn btn-sm btn-danger">Delete</button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    @endif
</div>
@endsection
