@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">Assignments</h5>
                </div>

                <div class="col text-end">
                    @can('assignments create')
                        <a href="{{ route('assignments.create') }}" class="btn btn-primary">
                            Add Assignment
                        </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 @if($assignments->count() > 0) id="bookings-table" @endif
               class="table table-bordered align-middle text-center">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Course</th>
                            <th>Title</th>
                            <th>Due Date</th>
                            <th>Created By</th>
                            <th>Created At</th>
                            <th style="width:220px;">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($assignments as $a)
                            <tr>
                                <td>{{ $a->id }}</td>
                                <td>{{ $a->course?->title ?? 'N/A' }}</td>
                                <td>{{ $a->title }}</td>
                                <td>{{ $a->due_date ? $a->due_date->format('Y-m-d H:i') : 'N/A' }}</td>
                                <td>{{ $a->creator?->name ?? 'N/A' }}</td>
                                <td>{{ $a->created_at?->format('Y-m-d') }}</td>
                                <td>
                                    @can('assignments view')
                                        <a class="btn btn-sm btn-info"
                                           href="{{ route('assignments.show', $a) }}">
                                            View
                                        </a>
                                    @endcan

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

                                    @can('assignments delete')
                                        <form class="d-inline"
                                              method="POST"
                                              action="{{ route('assignments.destroy', $a) }}"
                                              onsubmit="return confirm('Delete this assignment?');">
                                            @csrf
                                            @method('DELETE')
                                            <button class="btn btn-sm btn-danger">Delete</button>
                                        </form>
                                    @endcan
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center">No assignments found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <a href="{{ route('courses.index') }}" class="btn btn-secondary mt-3">Back to Courses</a>
        </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