@csrf
<div class="row g-3">
    <div class="col-md-3"><label class="form-label">Type <span class="text-danger">*</span></label>
        <select name="type" class="form-select">
            @foreach(App\Models\LearningMaterial::TYPES as $val => $label)<option value="{{ $val }}" @selected(old('type',$material->type)===$val)>{{ $label }}</option>@endforeach
        </select></div>
    <div class="col-md-6"><label class="form-label">Title <span class="text-danger">*</span></label>
        <input type="text" name="title" value="{{ old('title', $material->title) }}" class="form-control @error('title') is-invalid @enderror" required>
        @error('title')<div class="invalid-feedback">{{ $message }}</div>@enderror</div>
    <div class="col-md-3"><label class="form-label">Due Date</label>
        <input type="date" name="due_date" value="{{ old('due_date', optional($material->due_date)->format('Y-m-d')) }}" class="form-control">
        <div class="form-text">For homework / assignments.</div></div>

    <div class="col-md-4"><label class="form-label">Class <span class="text-danger">*</span></label>
        <select name="class_id" id="class_id" class="form-select" required>
            <option value="">— Select class —</option>
            @foreach($classes as $c)<option value="{{ $c->id }}" @selected(old('class_id',$material->class_id)==$c->id)>{{ $c->name }}</option>@endforeach
        </select></div>
    <div class="col-md-4"><label class="form-label">Section (optional)</label>
        <select name="section_id" id="section_id" class="form-select"><option value="">All sections</option></select></div>
    <div class="col-md-4"><label class="form-label">Subject (optional)</label>
        <select name="subject_id" class="form-select">
            <option value="">—</option>
            @foreach($subjects as $s)<option value="{{ $s->id }}" @selected(old('subject_id',$material->subject_id)==$s->id)>{{ $s->name }}</option>@endforeach
        </select></div>

    <div class="col-12"><label class="form-label">Description / Instructions</label>
        <textarea name="description" rows="4" class="form-control">{{ old('description', $material->description) }}</textarea></div>
    <div class="col-md-6"><label class="form-label">Attachment</label>
        <input type="file" name="attachment" class="form-control @error('attachment') is-invalid @enderror">
        @error('attachment')<div class="invalid-feedback">{{ $message }}</div>@enderror
        @if($material->attachment)<div class="form-text">Current: {{ basename($material->attachment) }}</div>@endif</div>
    <div class="col-md-6 d-flex align-items-end"><div class="form-check"><input type="checkbox" name="is_published" value="1" id="is_published" class="form-check-input" @checked(old('is_published', $material->is_published ?? true))><label for="is_published" class="form-check-label">Published (visible to students/parents)</label></div></div>
</div>
<div class="mt-3 d-flex gap-2">
    <button class="btn btn-primary"><i class="bi bi-check-lg me-1"></i>{{ $submitLabel ?? 'Save' }}</button>
    <a href="{{ route('materials.index') }}" class="btn btn-light border">Cancel</a>
</div>

@push('scripts')
<script>
    const SECTIONS = @json($sections);
    const selectedSection = @json(old('section_id', $material->section_id));
    function loadSections(){
        const cid=document.getElementById('class_id').value, sel=document.getElementById('section_id');
        sel.innerHTML='<option value="">All sections</option>';
        SECTIONS.filter(s=>String(s.class_id)===String(cid)).forEach(s=>{
            const o=document.createElement('option'); o.value=s.id; o.textContent=s.name;
            if(String(s.id)===String(selectedSection)) o.selected=true; sel.appendChild(o);
        });
    }
    document.getElementById('class_id').addEventListener('change', loadSections);
    loadSections();
</script>
@endpush
