'use client' import { useState, useEffect } from 'react' import { roomRequiresTypeApi, roomUnavailableApi, subjectsApi, roomsApi } from '@/lib/stundenplan/api' import type { RoomRequiresType, RoomUnavailable, TimetableSubject, TimetableRoom, } from '@/app/stundenplan/types' import { useConstraintCrud, ConstraintShell, useShellStyles, DAYS, dayLabel } from './_shell' // ---------- Room Requires Type (Subject → required room_type) ---------- type ReqForm = Omit const initialReq: ReqForm = { subject_id: '', room_type: '', is_hard: true, weight: 100, active: true, note: '' } export function RoomRequiresTypeEditor() { const styles = useShellStyles() const [subjects, setSubjects] = useState([]) const crud = useConstraintCrud(roomRequiresTypeApi, initialReq) useEffect(() => { subjectsApi.list().then(setSubjects).catch(() => setSubjects([])) }, []) const sLabel = (id: string): string => { const s = subjects.find(x => x.id === id); return s ? s.name : id.slice(0, 8) + '…' } return (
crud.setForm({ ...crud.form, room_type: e.target.value })} placeholder="z.B. Sporthalle" 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 RoomRequiresType return ( {sLabel(c.subject_id)} {c.room_type} {c.is_hard ? '✓' : '—'} {c.weight} ) }} /> ) } // ---------- Room Unavailable ---------- type UnavForm = Omit const initialUnav: UnavForm = { room_id: '', day_of_week: 1, period_index: 1, is_hard: true, weight: 100, active: true, note: '' } export function RoomUnavailableEditor() { const styles = useShellStyles() const [rooms, setRooms] = useState([]) const crud = useConstraintCrud(roomUnavailableApi, initialUnav) useEffect(() => { roomsApi.list().then(setRooms).catch(() => setRooms([])) }, []) const rLabel = (id: string): string => { const r = rooms.find(x => x.id === id); return r ? r.name : id.slice(0, 8) + '…' } return (
crud.setForm({ ...crud.form, period_index: 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 RoomUnavailable return ( {rLabel(c.room_id)} {dayLabel(c.day_of_week)} {c.period_index}. {c.is_hard ? '✓' : '—'} {c.weight} ) }} /> ) }