@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
    <div class="card">

        <div class="card-header">
            <div class="row align-items-center">
                <div class="col">
                    <h5 class="mb-0">Course Enrollments</h5>
                    <small class="text-muted">
                        @if(auth()->user()->hasRole('teacher'))
                            Enrolled Students (Only Name + Student ID)
                        @elseif(auth()->user()->hasRole('student'))
                            My Courses
                        @else
                            All Enrollments
                        @endif
                    </small>
                </div>

                <div class="col-auto">
                    @can('course_enrollments create')
                        <a href="{{ route('course-enrollments.create') }}" class="btn btn-primary btn-sm">
                            + New Enrollment
                        </a>
                    @endcan
                </div>
            </div>
            <hr class="mb-0" />
        </div>

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

            <div class="table-responsive">
                <table @if($enrollments->count() > 0) id="bookings-table" @endif class="table table-bordered align-middle text-center">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>Course</th>
                            <th>Category</th>
                            <th>Teacher</th>
                            <th>Student</th>
                            <th>Course Period</th>
                            <th>Enrolled At</th>
                            <th class="text-center">Action</th>
                        </tr>
                    </thead>

                    <tbody>
                        @forelse($enrollments as $enrollment)
                            <tr>
                                <td>{{ $enrollment->id }}</td>
                                <td>
                                    <div class="fw-semibold">{{ $enrollment->course->title ?? '-' }}</div>
                                </td>
                                <td>
                                    <div class="text-muted small">{{ $enrollment->course->category->name ?? '-' }}</div>
                                </td>
                                <td>{{ $enrollment->course->teacher->name ?? '-' }}</td>
                                <td>
                                    <div class="fw-semibold">{{ $enrollment->student->name ?? '-' }}</div>
                                </td>
                                <td>
                                    <div>{{ $enrollment->course->start_date?->format('Y-m-d') ?? 'N/A' }}</div>
                                    <div class="text-muted small">
                                        to {{ $enrollment->course->expire_date?->format('Y-m-d') ?? 'N/A' }}
                                    </div>
                                </td>
                                <td>{{ optional($enrollment->enrolled_at)->format('Y-m-d H:i') }}</td>
                                <td class="text-center">
                                    @can('course_enrollments update')
                                        <a href="{{ route('course-enrollments.edit', $enrollment->id) }}"
                                           class="btn btn-primary btn-sm">
                                            Edit
                                        </a>
                                    @endcan

                                    @can('course_enrollments delete')
                                        <form action="{{ route('course-enrollments.destroy', $enrollment->id) }}"
                                              method="POST"
                                              onsubmit="return confirm('Delete this enrollment?')"
                                              style="display:inline-block;">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-danger btn-sm">
                                                Delete
                                            </button>
                                        </form>
                                    @endcan
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center text-muted">No enrollments found.</td>
                            </tr>
                        @endforelse
                    </tbody>

                </table>
            </div>

        </div>
    </div>
</div>
@endsection

@push('script')
<script>
$(document).ready(function () {
    $('#bookings-table').DataTable({
        responsive: false,
        scrollX: true,
        pageLength: 10,
        lengthMenu: [5, 10, 25, 50, 100],
        order: [[0, 'desc']],
        columnDefs: [
            { orderable: false, targets: -1 }
        ]
    });
});
</script>
@endpush
