@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>

                            {{-- @if(!auth()->user()->hasRole('student')) --}}
                                <th>Teacher</th>
                            {{-- @endif --}}

                            <th>Student</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>
                                    {{-- @if(!auth()->user()->hasRole('student')) --}}
                                        
                                    {{-- @endif --}}
                                </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>
                                    {{-- <div class="text-muted small">Student ID: {{ $enrollment->student->id ?? '-' }}</div> --}}

                                    {{-- ✅ Only Master Admin can see email --}}
                                   
                                        {{-- <div class="text-muted small">
                                            {{ $enrollment->student->email ?? '-' }}
                                        </div> --}}
                                   
                                </td>

                                <td>
                                    {{ optional($enrollment->enrolled_at)->format('Y-m-d H:i') }}
                                </td>

                                <td class="text-center">

                                    {{-- Edit Button --}}
                                    @can('course_enrollments update')
                                        <a href="{{ route('course-enrollments.edit', $enrollment->id) }}"
                                        class="btn btn-primary btn-sm">
                                            Edit
                                        </a>
                                    @endcan

                                    {{-- Delete Button --}}
                                    @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="10" 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