import { useState, useRef } from "react" import { toast } from "sonner" import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query" import api from "../../lib/api" import Card from "../../components/ui/Card" import Button from "../../components/ui/Button" import { Input, Label, Select } from "../../components/ui/Input" import Modal from "../../components/ui/Modal" import { Users, FileText, AlertTriangle, Calendar, GraduationCap, TrendingUp, Scale, X, Plus, Paperclip, Trash2, Printer, Calculator, Shield, Info, Check, Eye, Download, UserX, UserPlus, Briefcase, FileSignature, DollarSign, Building2, RotateCcw, History } from "lucide-react" import { fmt } from "./shared" 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' | 'OTHER'>('ID_CARD') const addAttachmentMutation = useMutation({ mutationFn: (data: any) => api.post('/attachments', data), onSuccess: () => queryClient.invalidateQueries({ queryKey: ['roster-profile', employeeId] }), }) const deleteAttachmentMutation = useMutation({ mutationFn: (id: string) => api.delete(`/attachments/${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: '学历证书', 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', 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')}
))}
) :
暂无附件
}
) }