@extends('layouts.admin')

@section('title', 'Manage Deferments')

@section('content')
<div class="container py-4">
    <h3 class="mb-4">Deferment Requests</h3>

    <a href="{{ route('admin.deferments.create') }}" class="btn btn-primary mb-3">Add New Deferment</a>

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

    <table class="table table-bordered table-striped">
        <thead class="table-dark">
            <tr>
                <th>#</th>
                <th>Registration Number</th>
                <th>Reason</th>
                <th>Status</th>
                <th>Attachment</th>
                <th>Submitted On</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($deferments as $deferment)
                <tr>
                    <td>{{ $loop->iteration }}</td>
                    <td>{{ $deferment->registration_number ?? 'N/A' }}</td>
                    <td>{{ $deferment->reason }}</td>
                    <td>{{ ucfirst($deferment->status) }}</td>
                    <td>
                        @if($deferment->attachment)
                            <a href="{{ asset('storage/' . $deferment->attachment) }}" target="_blank">View</a>
                        @else
                            N/A
                        @endif
                    </td>
                    <td>{{ $deferment->created_at ? $deferment->created_at->format('d M Y') : 'N/A' }}</td>
                    <td>
                        <a href="{{ route('admin.deferments.edit', $deferment->id) }}" class="btn btn-sm btn-warning">Edit</a>
                        <form action="{{ route('admin.deferments.destroy', $deferment->id) }}" method="POST" class="d-inline">
                            @csrf @method('DELETE')
                            <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('Delete this deferment?')">Delete</button>
                        </form>
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="7" class="text-center text-muted">No deferment requests found.</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>
@endsection
