import React, { useState, useEffect } from 'react'; import { Bell, Search, Menu, Building, Info, Check, AlertTriangle } from 'lucide-react'; import { User, School, Notification, UserRole } from '../types'; import { api } from '../services/api'; interface HeaderProps { user: User; title: string; onMenuClick?: () => void; } export const Header: React.FC = ({ user, title, onMenuClick }) => { const [schools, setSchools] = useState([]); const [selectedSchool, setSelectedSchool] = useState(localStorage.getItem('admin_view_school_id') || ''); const [currentSchoolName, setCurrentSchoolName] = useState(''); // Notification State const [notifications, setNotifications] = useState([]); const [showNotif, setShowNotif] = useState(false); const [hasUnread, setHasUnread] = useState(false); const fetchSchools = async () => { // Only ADMIN (Super Admin) can switch schools if (user.role === UserRole.ADMIN) { try { const data = await api.schools.getAll(); setSchools(data); if (data.length > 0) { if (!selectedSchool || !data.find((s: School) => s._id === selectedSchool)) { const defaultId = data[0]._id!; setSelectedSchool(defaultId); localStorage.setItem('admin_view_school_id', defaultId); if (!localStorage.getItem('admin_view_school_id_init')) { localStorage.setItem('admin_view_school_id_init', 'true'); window.location.reload(); } } } } catch (e) { console.error(e); } } else { // Principal, Teacher, Student see their own school try { // For display purposes, we can fetch public info or just user's school info const data = await api.schools.getPublic(); const mySchool = data.find((s: School) => s._id === user.schoolId); if (mySchool) setCurrentSchoolName(mySchool.name); } catch(e) { console.error(e); } } }; useEffect(() => { fetchSchools(); // Listen for custom event to refresh schools const handleSchoolUpdate = () => { fetchSchools(); }; window.addEventListener('school-updated', handleSchoolUpdate); // Fetch notifications const fetchNotifs = async () => { try { const list = await api.notifications.getAll(user._id || '', user.role); setNotifications(list); const lastReadTime = localStorage.getItem('last_read_notif_time'); if (list.length > 0) { if (!lastReadTime || new Date(list[0].createTime) > new Date(lastReadTime)) { setHasUnread(true); } } } catch (e) { console.error(e); } }; fetchNotifs(); return () => { window.removeEventListener('school-updated', handleSchoolUpdate); }; }, [user]); const handleSchoolChange = (e: React.ChangeEvent) => { const newVal = e.target.value; if (!newVal) return; setSelectedSchool(newVal); localStorage.setItem('admin_view_school_id', newVal); window.location.reload(); }; const handleOpenNotif = () => { setShowNotif(!showNotif); if (!showNotif) { setHasUnread(false); localStorage.setItem('last_read_notif_time', new Date().toISOString()); } }; const roleLabels: Record = { [UserRole.ADMIN]: '超级管理员', [UserRole.PRINCIPAL]: '校长', [UserRole.TEACHER]: '教师', [UserRole.STUDENT]: '学生' }; return (

{title}

{user.role === UserRole.ADMIN ? ( ) : ( {currentSchoolName || '我的学校'} )}
{showNotif && (

系统消息

{notifications.length > 0 ? notifications.map(n => (

{n.title}

{n.content}

{new Date(n.createTime).toLocaleString()}

)) :

暂无新消息

}
)}

{user.trueName || user.username}

{roleLabels[user.role] || '用户'}

Profile
); };