@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">Courses</h5>
                </div>
                <div class="col text-end">
                    @can('courses create')
                        <a href="{{ route('courses.create') }}" class="btn btn-primary">Add Course</a>
                    @endcan
                </div>
            </div>
            <hr/>
        </div>

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

            <div class="table-responsive">
                <table class="table" id="bookings-table">
                    <thead>
                        <tr>
                            <th>ID</th>
                          
                            <th>Title</th>
                            <th>Category</th>
                             <th>Teacher</th>
                            <th>Status</th>
                            <th>Created</th>
                            <th style="width:260px;">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($courses as $course)
                        <tr>
                            <td>{{ $course->id }}</td>
                            

                            
                                
                            <td>{{ $course->title }}</td>
                            <td>{{ $course->category?->name ?? 'N/A' }}</td>
                            <td>{{ $course->teacher?->name ?? 'N/A' }}</td>
                            <td>
                                <span class="badge bg-{{ $course->status === 'approved' ? 'success' : ($course->status === 'rejected' ? 'danger' : 'warning') }}">
                                    {{ ucfirst($course->status) }}
                                </span>
                            </td>
                            <td>{{ $course->created_at?->format('Y-m-d') }}</td>
                            <td>
                                @can('courses view')
                                    <a class="btn btn-sm btn-info" href="{{ route('courses.show', $course) }}">View</a>
                                @endcan

                                @can('courses update')
                                    <a class="btn btn-sm btn-warning" href="{{ route('courses.edit', $course) }}">Edit</a>
                                @endcan

                                @role('master admin')
                                    <form class="d-inline" method="POST" action="{{ route('courses.approve', $course) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="btn btn-sm btn-success" type="submit">Approve</button>
                                    </form>

                                    <form class="d-inline" method="POST" action="{{ route('courses.reject', $course) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="btn btn-sm btn-secondary" type="submit">Reject</button>
                                    </form>
                                @endrole

                                @can('courses delete')
                                    <form class="d-inline" method="POST" action="{{ route('courses.destroy', $course) }}"
                                          onsubmit="return confirm('Delete this course?');">
                                        @csrf
                                        @method('DELETE')
                                        <button class="btn btn-sm btn-danger" type="submit">Delete</button>
                                    </form>
                                @endcan


                                
                            </td>
                        </tr>
                        @endforeach
                    </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, 'asc']],
        columnDefs: [{ orderable: false, targets: -1 }]
    });
});
</script>
@endpush