@extends('layouts.admin')

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

@section('content')
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4>Course Management</h4>
        <a href="{{ route('admin.courses.create') }}" class="btn btn-success btn-sm">Add New Course</a>
    </div>

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

    @php
        // Grouping courses by programme + mode
        $groupedCourses = $courses->groupBy(function($course) {
            return $course->programme_description . ' - ' . $course->attendance_mode;
        });
    @endphp

    @if($groupedCourses->isEmpty())
        <div class="alert alert-warning">No courses found.</div>
    @else
        @foreach($groupedCourses as $programme => $courseGroup)
            <div class="card mb-4">
                <div class="card-header bg-dark text-white">
                    {{ $programme }}
                </div>
                <div class="card-body">
                    <table class="table table-sm table-bordered">
                        <thead class="table-light">
                            <tr>
                                <th>#</th>
                                <th>Course Code</th>
                                <th>Course Description</th>
                                <th>Credits</th>
                                <th>Level</th>
                                <th>Semester</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($courseGroup as $index => $course)
                                <tr>
                                    <td>{{ $index + 1 }}</td>
                                    <td>{{ $course->course_code }}</td>
                                    <td>{{ $course->course_description }}</td>
                                    <td>{{ $course->credits }}</td>
                                    <td>{{ $course->level }}</td>
                                    <td>{{ $course->semester }}</td>
                                    <td>
                                        <a href="{{ route('admin.courses.edit', $course->id) }}" class="btn btn-warning btn-sm">Edit</a>
                                        <form action="{{ route('admin.courses.destroy', $course->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                                        </form>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        @endforeach
    @endif
</div>
@endsection
