@extends('layouts.admin')

@section('title', 'Edit Kelas')
@section('subtitle', 'Perbarui data kelas')

@section('content')

<div class="row g-3">
    <div class="col-12 col-xl-6">
        <div class="panel">
            <div class="panel-head">
                <h3>Edit Kelas — {{ $class->name }}</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.update', $class->id) }}" method="POST">
                    @csrf
                    @method('PUT')

                    <div class="mb-3">
                        <label class="form-label">Nama Kelas</label>
                        <input type="text" name="name" class="form-control" value="{{ old('name', $class->name) }}" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Tingkat</label>
                        <select name="grade_level" class="form-select" required>
                            @for ($i = 1; $i <= 6; $i++)
                                <option value="{{ $i }}" {{ old('grade_level', $class->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', $class->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', $class->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" value="{{ old('capacity', $class->capacity) }}">
                    </div>

                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-brand flex-fill">
                            <i class="fa-solid fa-floppy-disk me-1"></i> Simpan Perubahan
                        </button>
                        <a href="{{ route('classes.index') }}" class="btn btn-outline-secondary">Batal</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

@endsection
