'use client'; import React, { useState, useEffect, useMemo } from 'react'; import { completeLesson, type Lesson } from '@/api/schedule'; import { useAuth } from '@/contexts/AuthContext'; import { parseISOToUserTimezone } from '@/utils/timezone'; interface FeedbackModalProps { isOpen: boolean; lesson: Lesson | null; onClose: () => void; onSuccess: () => void; } export function FeedbackModal({ isOpen, lesson, onClose, onSuccess }: FeedbackModalProps) { const { user } = useAuth(); const [formData, setFormData] = useState({ mentor_grade: '', school_grade: '', mentor_notes: '', }); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // Парсим время с учётом timezone пользователя const parsedTimes = useMemo(() => { if (!lesson) return null; return { start: parseISOToUserTimezone(lesson.start_time, user?.timezone), end: parseISOToUserTimezone(lesson.end_time, user?.timezone), }; }, [lesson, user?.timezone]); useEffect(() => { if (isOpen && lesson) { setFormData({ mentor_grade: lesson.mentor_grade?.toString() || '', school_grade: lesson.school_grade?.toString() || '', mentor_notes: lesson.mentor_notes || '', }); } }, [isOpen, lesson]); if (!lesson || !parsedTimes) return null; const visible = isOpen; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setLoading(true); try { await completeLesson( lesson.id, formData.mentor_notes.trim(), formData.mentor_grade ? parseInt(formData.mentor_grade) : undefined, formData.school_grade ? parseInt(formData.school_grade) : undefined ); onSuccess(); onClose(); } catch (err: unknown) { const errMsg = err instanceof Error ? err.message : 'Ошибка сохранения обратной связи'; setError(String(errMsg)); } finally { setLoading(false); } }; const handleClose = () => { if (!loading) { setError(null); onClose(); } }; const clientName = typeof lesson.client === 'object' && lesson.client?.user ? `${lesson.client.user.first_name} ${lesson.client.user.last_name}` : (lesson as { client_name?: string }).client_name || 'Студент'; const subjectName = typeof lesson.subject === 'string' ? lesson.subject : (lesson.subject as { name?: string } | null | undefined)?.name || 'Занятие'; return ( <> {/* Затемнённый фон — клик закрывает панель */}
{/* Панель справа */}

Обратная связь

{lesson.title} — {subjectName}

Дата: {parsedTimes.start.dateObj.toLocaleDateString('ru-RU')}
Время: {parsedTimes.start.time} {' — '} {parsedTimes.end.time}
Студент: {clientName}
Длительность: {(lesson as { duration?: number }).duration || 60} мин

Оценки

setFormData((p) => ({ ...p, mentor_grade: e.target.value }))} style={{ width: '100%', padding: '12px 16px', borderRadius: 12, border: '1px solid var(--md-sys-color-outline)', background: 'var(--md-sys-color-surface)', fontSize: 15, color: 'var(--md-sys-color-on-surface)', }} placeholder="5" disabled={loading} />
setFormData((p) => ({ ...p, school_grade: e.target.value }))} style={{ width: '100%', padding: '12px 16px', borderRadius: 12, border: '1px solid var(--md-sys-color-outline)', background: 'var(--md-sys-color-surface)', fontSize: 15, color: 'var(--md-sys-color-on-surface)', }} placeholder="4" disabled={loading} />