@extends('layouts.admin')

@section('title', 'Data Kelas')
@section('subtitle', 'Kelola kelas, wali kelas, dan ruang')

@section('content')

<div class="row g-3">
    <div class="col-12 col-xl-4">
        <div class="panel">
            <div class="panel-head">
                <h3>Tambah Kelas</h3>
            </div>
            <div class="panel-body">
                @if ($errors->any())
                    <div class="alert alert-danger">
                        @foreach ($errors->all() as $error)
                            <div>{{ $error }}</div>
                        @endforeach
                    </div>
                @endif

                <form action="{{ route('classes.store') }}" method="POST">
                    @csrf
                    <div class="mb-3">
                        <label class="form-label">Nama Kelas</label>
                        <input type="text" name="name" class="form-control" placeholder="Contoh: 6A" value="{{ old('name') }}" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Tingkat</label>
                        <select name="grade_level" class="form-select" required>
                            <option value="">— Pilih tingkat —</option>
                            @for ($i = 1; $i <= 6; $i++)
                                <option value="{{ $i }}" {{ old('grade_level') == $i ? 'selected' : '' }}>Kelas {{ $i }}</option>
                            @endfor
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Wali Kelas</label>
                        <select name="homeroom_teacher_id" class="form-select">
                            <option value="">— Pilih guru (opsional) —</option>
                            @foreach ($teachers as $teacher)
                                <option value="{{ $teacher->id }}" {{ old('homeroom_teacher_id') == $teacher->id ? 'selected' : '' }}>{{ $teacher->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Ruang Kelas</label>
                        <select name="classroom_id" class="form-select">
                            <option value="">— Pilih ruang (opsional) —</option>
                            @foreach ($classrooms as $room)
                                <option value="{{ $room->id }}" {{ old('classroom_id') == $room->id ? 'selected' : '' }}>{{ $room->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Kapasitas</label>
                        <input type="number" name="capacity" class="form-control" placeholder="Opsional" value="{{ old('capacity') }}">
                    </div>
                    <button type="submit" class="btn btn-brand w-100">
                        <i class="fa-solid fa-plus me-1"></i> Simpan Kelas
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-12 col-xl-8">
        <div class="panel">
            <div class="panel-head">
                <h3>Daftar Kelas</h3>
                <span class="text-muted" style="font-size:.8rem;">{{ $classes->total() }} kelas</span>
            </div>
            <div class="table-responsive">
                <table class="table-modern">
                    <thead>
                        <tr>
                            <th>Kelas</th>
                            <th>Tingkat</th>
                            <th>Wali Kelas</th>
                            <th>Ruang</th>
                            <th>Jumlah Siswa</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($classes as $class)
                            <tr>
                                <td class="fw-semibold">{{ $class->name }}</td>
                                <td>Kelas {{ $class->grade_level }}</td>
                                <td>{{ $class->homeroomTeacher->name ?? '-' }}</td>
                                <td>{{ $class->classroom->name ?? '-' }}</td>
                                <td>
                                    {{ $class->students_count }}
                                    @if ($class->capacity)
                                        / {{ $class->capacity }}
                                    @endif
                                </td>
                                <td class="d-flex gap-1">
                                    <a href="{{ route('classes.edit', $class->id) }}" class="icon-btn" title="Edit">
                                        <i class="fa-solid fa-pen"></i>
                                    </a>
                                    <form action="{{ route('classes.destroy', $class->id) }}" method="POST" onsubmit="return confirm('Hapus kelas ini?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn-soft-danger" title="Hapus">
                                            <i class="fa-solid fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center text-muted py-4">Belum ada data kelas.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="p-3">
                {{ $classes->links() }}
            </div>
        </div>
    </div>
</div>

@endsection
