import React, { useEffect, useState, useMemo } from 'react'; import { Table, Button, Modal, Form, Input, InputNumber, Select, Space, message, Tag, Popconfirm, Upload, Tooltip, Empty, } from 'antd'; import { PlusOutlined, UploadOutlined, DownloadOutlined, UndoOutlined, InboxOutlined, } from '@ant-design/icons'; import api from '../../api'; import PermissionButton from '../../components/PermissionButton'; const statusMap: Record = { available: { text: '可用', color: 'green' }, in_use: { text: '使用中', color: 'blue' }, reserved: { text: '已预留', color: 'purple' }, maintenance: { text: '维护中', color: 'orange' }, archived: { text: '已归档', color: '#999' }, }; interface CurrentUsage { type: 'schedule' | 'rental'; title: string; startTime: string; endTime: string; } const typeColor: Record = { 大: 'volcano', 次大: 'geekblue', 小: 'cyan', }; const ClassroomsPage: React.FC = () => { const [data, setData] = useState([]); const [loading, setLoading] = useState(false); const [modalOpen, setModalOpen] = useState(false); const [editing, setEditing] = useState(null); const [showArchived, setShowArchived] = useState(false); const [form] = Form.useForm(); const [searchText, setSearchText] = useState(''); const [filterStatus, setFilterStatus] = useState(undefined); const [saving, setSaving] = useState(false); const filteredData = useMemo(() => { let result = data; if (searchText) { const s = searchText.toLowerCase(); result = result.filter((d: Record) => (typeof d.name === 'string' && d.name.toLowerCase().includes(s)) || (typeof d.building === 'string' && d.building.toLowerCase().includes(s))); } if (filterStatus) result = result.filter((d: Record) => d.status === filterStatus); return result; }, [data, searchText, filterStatus]); const fetchData = async () => { setLoading(true); try { const res: any = await api.get('/classrooms', { params: { includeArchived: showArchived } }); setData(res); } catch (e: any) { message.error(e?.message || '加载失败,请稍后重试'); } setLoading(false); }; useEffect(() => { fetchData(); }, [showArchived]); const handleSave = async () => { const values = await form.validateFields(); setSaving(true); try { if (editing) { await api.put(`/classrooms/${editing.id}`, values); message.success('更新成功'); } else { await api.post('/classrooms', values); message.success('创建成功'); } setModalOpen(false); form.resetFields(); setEditing(null); fetchData(); } catch (e: any) { message.error(e?.message || '操作失败'); } finally { setSaving(false); } }; const handleArchive = async (id: number) => { try { await api.delete(`/classrooms/${id}`); message.success('已归档'); fetchData(); } catch (e: any) { message.error(e?.message || '归档失败'); } }; const handleRestore = async (id: number) => { try { await api.put(`/classrooms/${id}/restore`); message.success('已恢复'); fetchData(); } catch (e: any) { message.error(e?.message || '恢复失败'); } }; const handleDownloadTemplate = () => { const baseURL = import.meta.env.PROD ? '/api' : `http://localhost:${import.meta.env.VITE_API_PORT || 3002}/api`; const token = localStorage.getItem('token'); fetch(`${baseURL}/classrooms/template`, { headers: { Authorization: `Bearer ${token}` } }) .then((res) => res.blob()) .then((blob) => { const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = '教室导入模板.xlsx'; a.click(); URL.revokeObjectURL(url); }) .catch(() => message.error('下载失败')); }; const columns = useMemo(() => [ { title: '教室名', width: 120, dataIndex: 'name', sorter: (a: any, b: any) => a.name.localeCompare(b.name), }, { title: '楼栋', dataIndex: 'building', width: 80 }, { title: '楼层', dataIndex: 'floor', width: 80 }, { title: '类型', width: 90, dataIndex: 'roomType', render: (v: string) => {v || '-'}, }, { title: '容量', dataIndex: 'capacity', width: 80 }, { title: '课程类型', dataIndex: 'courseType', width: 100, render: (v: string) => v || '-' }, { title: '负责人', dataIndex: 'supervisor', width: 100, render: (v: string) => v || '-' }, { title: '状态', width: 100, dataIndex: 'status', render: (s: string, record: { currentUsage?: CurrentUsage | null }) => { const effectiveStatus = record.currentUsage ? 'in_use' : s; return ( {statusMap[effectiveStatus]?.text || s} ); }, }, { title: '操作', width: 180, render: (_: any, record: any) => ( {record.status === 'archived' ? ( handleRestore(record.id)}> } type="link"> 恢复 ) : ( <> { setEditing(record); form.setFieldsValue(record); setModalOpen(true); }} > 编辑 handleArchive(record.id)} okText="归档" cancelText="取消" > }> 归档 )} ), }, ], []); return (
setSearchText(v)} onChange={(e) => { if (!e.target.value) setSearchText(''); }} />
); }; export default ClassroomsPage;