'use client' import { useState, useEffect } from 'react' import { teacherMaxHoursDayApi, teacherMaxHoursWeekApi, teachersApi, } from '@/lib/stundenplan/api' import type { TeacherMaxHoursDay, TeacherMaxHoursWeek, TimetableTeacher, } from '@/app/stundenplan/types' import { useConstraintCrud, ConstraintShell, useShellStyles } from './_shell' function useTeachers() { const [teachers, setTeachers] = useState([]) useEffect(() => { teachersApi.list().then(setTeachers).catch(() => setTeachers([])) }, []) return teachers } function tLabel(teachers: TimetableTeacher[], id: string): string { const t = teachers.find(x => x.id === id) return t ? `${t.last_name}, ${t.first_name}` : id.slice(0, 8) + '…' } // ---------- Max Hours / Day ---------- type DayForm = Omit const initialDay: DayForm = { teacher_id: '', max_hours: 6, is_hard: false, weight: 50, active: true, note: '' } export function TeacherMaxHoursDayEditor() { const styles = useShellStyles() const teachers = useTeachers() const crud = useConstraintCrud(teacherMaxHoursDayApi, initialDay) return (
crud.setForm({ ...crud.form, max_hours: parseInt(e.target.value) || 1 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
crud.setForm({ ...crud.form, weight: parseInt(e.target.value) || 0 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
crud.setForm({ ...crud.form, is_hard: e.target.checked })} className="w-5 h-5" />
} renderRow={(item) => { const c = item as TeacherMaxHoursDay return ( {tLabel(teachers, c.teacher_id)} {c.max_hours} {c.is_hard ? '✓' : '—'} {c.weight} ) }} /> ) } // ---------- Max Hours / Week ---------- type WeekForm = Omit const initialWeek: WeekForm = { teacher_id: '', max_hours: 28, is_hard: true, weight: 100, active: true, note: '' } export function TeacherMaxHoursWeekEditor() { const styles = useShellStyles() const teachers = useTeachers() const crud = useConstraintCrud(teacherMaxHoursWeekApi, initialWeek) return (
crud.setForm({ ...crud.form, max_hours: parseInt(e.target.value) || 1 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
crud.setForm({ ...crud.form, weight: parseInt(e.target.value) || 0 })} className={`w-full px-3 py-2 rounded-lg border ${styles.inputClass}`} />
crud.setForm({ ...crud.form, is_hard: e.target.checked })} className="w-5 h-5" />
} renderRow={(item) => { const c = item as TeacherMaxHoursWeek return ( {tLabel(teachers, c.teacher_id)} {c.max_hours} {c.is_hard ? '✓' : '—'} {c.weight} ) }} /> ) }