import React, { useEffect, useState } from 'react'; import { useParams, Link, useNavigate } from 'react-router-dom'; import { AccessTimeIcon, AddIcon, ArrowBackIcon, CalendarTodayIcon, DeleteIcon, EditIcon, MenuBookIcon, PeopleIcon } from '@mui/icons-material'; import useTimetableStore from '../../stores/timetableStore'; import { useProfile } from '../../contexts/ProfileContext'; import Modal from '../../components/common/Modal'; const ClassDetailPage: React.FC = () => { const { classId } = useParams<{ classId: string }>(); const navigate = useNavigate(); const { profile } = useProfile(); const { currentClass, timetables, enrolledStudents, classTeachers, classDetailLoading, classDetailError, fetchClassDetail, deleteClass, clearCurrentClass, } = useTimetableStore(); const [activeTab, setActiveTab] = useState<'timetables' | 'students' | 'teachers'>('timetables'); const [showDeleteModal, setShowDeleteModal] = useState(false); useEffect(() => { if (classId) { fetchClassDetail(classId); } return () => { clearCurrentClass(); }; }, [classId, fetchClassDetail, clearCurrentClass]); const handleDeleteClass = async () => { if (!classId) return; await deleteClass(classId); navigate('/timetable/classes'); }; const isOwner = currentClass?.created_by === profile?.id; const isTeacher = classTeachers.some(t => t.teacher_id === profile?.id && t.is_primary); if (classDetailLoading) { return (
); } if (classDetailError || !currentClass) { return (

Error Loading Class

{classDetailError || 'Class not found'}

Back to Classes
); } return (
{/* Header */}
Back to Classes

{currentClass.name}

{currentClass.subject}

{currentClass.school_year} • {currentClass.academic_term}

{(isOwner || isTeacher) && (
Edit
)}
{/* Stats */}

Timetables

{currentClass.timetable_count}

Students

{currentClass.student_count}

Teachers

{classTeachers.length}

{/* Tabs */}
{/* Tab Content */}
{activeTab === 'timetables' && (

Timetables

{(isOwner || isTeacher) && ( Add Timetable )}
{timetables.length === 0 ? (

No timetables yet

Create a timetable to start scheduling lessons

) : (
{timetables.map((timetable) => (

{timetable.name}

{timetable.lesson_count} lessons {timetable.is_recurring && ' • Recurring'}

))}
)}
)} {activeTab === 'students' && (

Enrolled Students

{enrolledStudents.length === 0 ? (

No students enrolled

Students can request enrollment or be added by teachers

) : (
{enrolledStudents.map((student) => (
{student.full_name.charAt(0)}

{student.full_name}

{student.email}

))}
)}
)} {activeTab === 'teachers' && (

Teachers

{classTeachers.map((teacher) => (
{teacher.full_name.charAt(0)}

{teacher.full_name}

{teacher.email}

{teacher.is_primary && ( Primary )}
))}
)}
{/* Delete Modal */} setShowDeleteModal(false)} title="Delete Class" >

Are you sure you want to delete "{currentClass.name}"? This action cannot be undone and will remove all timetables, lessons, and whiteboards associated with this class.

); }; export default ClassDetailPage;