import { useState, useRef } from "react" import { toast } from "sonner" import { useMutation, useQueryClient } from "@tanstack/react-query" import { attachmentApi } from '../../lib/api-services' import Card from "../../components/ui/Card" import Button from "../../components/ui/Button" import { Select } from "../../components/ui/Input" import { Paperclip, Trash2, Eye, Download } from "lucide-react" export default function AttachmentInfo({ employeeId, attachments }: { employeeId: string; attachments: any[] }) { const queryClient = useQueryClient() const fileInputRef = useRef(null) const [fileType, setFileType] = useState<'ID_CARD' | 'BANK_CARD' | 'EDUCATION' | 'CERTIFICATE' | 'CONTRACT' | 'PHOTO' | 'OTHER'>('ID_CARD') const addAttachmentMutation = useMutation({ mutationFn: (data: any) => attachmentApi.add(data), onSuccess: () => queryClient.invalidateQueries({ queryKey: ['roster-profile', employeeId] }), }) const deleteAttachmentMutation = useMutation({ mutationFn: (id: string) => attachmentApi.remove(id), onSuccess: () => queryClient.invalidateQueries({ queryKey: ['roster-profile', employeeId] }), }) const handleFileUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (!file) return // 文件类型校验 const allowedExts = ['.pdf', '.jpg', '.jpeg', '.png', '.heic', '.gif', '.bmp', '.webp', '.doc', '.docx', '.xls', '.xlsx', '.tiff', '.tif'] const ext = file.name.toLowerCase().substring(file.name.lastIndexOf('.')) if (!allowedExts.includes(ext)) { toast.error('不支持的文件格式,支持 PDF、图片、Word、Excel 等常见格式') return } // 文件大小校验(10MB) const maxSize = 10 * 1024 * 1024 if (file.size > maxSize) { toast.error(`文件过大,请上传小于 10MB 的文件(当前: ${formatSize(file.size)})`) return } const reader = new FileReader() reader.onload = (event) => { const fileUrl = event.target?.result as string addAttachmentMutation.mutate({ employeeId, fileName: file.name, fileType, fileUrl, fileSize: file.size }) } reader.readAsDataURL(file) } const fileTypeLabels: Record = { ID_CARD: '身份证', BANK_CARD: '银行卡', EDUCATION: '学历证书', CERTIFICATE: '职业资格证书', CONTRACT: '合同扫描件', PHOTO: '员工照片', OTHER: '其他' } const fileTypeColors: Record = { ID_CARD: 'bg-blue-50 text-blue-600', BANK_CARD: 'bg-green-50 text-safe', EDUCATION: 'bg-amber-50 text-amber-600', CERTIFICATE: 'bg-purple-50 text-purple-600', CONTRACT: 'bg-cyan-50 text-cyan-600', PHOTO: 'bg-pink-50 text-pink-600', OTHER: 'bg-gray-100 text-gray-500' } const formatSize = (bytes: number) => { if (!bytes) return '-' if (bytes < 1024) return `${bytes}B` if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)}KB` return `${(bytes / 1024 / 1024).toFixed(1)}MB` } return (

附件管理({attachments?.length || 0}个)

支持 PDF/JPG/PNG,最大 10MB
{attachments?.length ? (
{attachments.map((att) => (
{att.fileName}
{fileTypeLabels[att.fileType] || att.fileType} {formatSize(att.fileSize)} {new Date(att.createdAt).toLocaleDateString('zh-CN')}
))}
) :
暂无附件
}
) }