"use client"; import { useState } from "react"; import { BarcodeLabelMaster } from "@/types/barcode"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Copy, Trash2, Loader2 } from "lucide-react"; import { barcodeApi } from "@/lib/api/barcodeApi"; import { useToast } from "@/hooks/use-toast"; import { useRouter } from "next/navigation"; import { format } from "date-fns"; interface BarcodeListTableProps { labels: BarcodeLabelMaster[]; total: number; page: number; limit: number; isLoading: boolean; onPageChange: (page: number) => void; onRefresh: () => void; } export function BarcodeListTable({ labels, total, page, limit, isLoading, onPageChange, onRefresh, }: BarcodeListTableProps) { const [deleteTarget, setDeleteTarget] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const [isCopying, setIsCopying] = useState(false); const { toast } = useToast(); const router = useRouter(); const totalPages = Math.ceil(total / limit); const handleEdit = (labelId: string) => { router.push(`/admin/screenMng/barcodeList/designer/${labelId}`); }; const handleCopy = async (labelId: string) => { setIsCopying(true); try { const response = await barcodeApi.copyLabel(labelId); if (response.success) { toast({ title: "성공", description: "바코드 라벨이 복사되었습니다.", }); onRefresh(); } } catch (error: any) { toast({ title: "오류", description: error.message || "바코드 라벨 복사에 실패했습니다.", variant: "destructive", }); } finally { setIsCopying(false); } }; const handleDeleteClick = (labelId: string) => { setDeleteTarget(labelId); }; const handleDeleteConfirm = async () => { if (!deleteTarget) return; setIsDeleting(true); try { const response = await barcodeApi.deleteLabel(deleteTarget); if (response.success) { toast({ title: "성공", description: "바코드 라벨이 삭제되었습니다.", }); setDeleteTarget(null); onRefresh(); } } catch (error: any) { toast({ title: "오류", description: error.message || "바코드 라벨 삭제에 실패했습니다.", variant: "destructive", }); } finally { setIsDeleting(false); } }; const formatDate = (dateString: string | null) => { if (!dateString) return "-"; try { return format(new Date(dateString), "yyyy-MM-dd"); } catch { return dateString; } }; if (isLoading) { return (
); } if (labels.length === 0) { return (

등록된 바코드 라벨이 없습니다.

); } return ( <>
No 라벨명 템플릿 유형 작성자 수정일 액션 {labels.map((label, index) => { const rowNumber = (page - 1) * limit + index + 1; return ( handleEdit(label.label_id)} className="cursor-pointer hover:bg-muted/50" > {rowNumber}
{label.label_name_kor}
{label.label_name_eng && (
{label.label_name_eng}
)}
{label.width_mm != null && label.height_mm != null ? `${label.width_mm}×${label.height_mm}mm` : label.template_type || "-"} {label.created_by || "-"} {formatDate(label.updated_at || label.created_at)}
e.stopPropagation()}>
); })}
{totalPages > 1 && (
{page} / {totalPages}
)} !open && setDeleteTarget(null)}> 바코드 라벨 삭제 이 바코드 라벨을 삭제하시겠습니까?
삭제된 라벨은 복구할 수 없습니다.
취소 {isDeleting ? ( <> 삭제 중... ) : ( "삭제" )}
); }