"use client"; import { useState } from "react"; import { ReportMaster } from "@/types/report"; 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 { Pencil, Copy, Trash2, Loader2 } from "lucide-react"; import { reportApi } from "@/lib/api/reportApi"; import { useToast } from "@/hooks/use-toast"; import { useRouter } from "next/navigation"; import { format } from "date-fns"; interface ReportListTableProps { reports: ReportMaster[]; total: number; page: number; limit: number; isLoading: boolean; onPageChange: (page: number) => void; onRefresh: () => void; } export function ReportListTable({ reports, total, page, limit, isLoading, onPageChange, onRefresh, }: ReportListTableProps) { 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 = (reportId: string) => { router.push(`/admin/report/designer/${reportId}`); }; // 복사 const handleCopy = async (reportId: string) => { setIsCopying(true); try { const response = await reportApi.copyReport(reportId); if (response.success) { toast({ title: "성공", description: "리포트가 복사되었습니다.", }); onRefresh(); } } catch (error: any) { toast({ title: "오류", description: error.message || "리포트 복사에 실패했습니다.", variant: "destructive", }); } finally { setIsCopying(false); } }; // 삭제 확인 const handleDeleteClick = (reportId: string) => { setDeleteTarget(reportId); }; // 삭제 실행 const handleDeleteConfirm = async () => { if (!deleteTarget) return; setIsDeleting(true); try { const response = await reportApi.deleteReport(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 (reports.length === 0) { return (

등록된 리포트가 없습니다.

); } return ( <>
No 리포트명 작성자 수정일 액션 {reports.map((report, index) => { const rowNumber = (page - 1) * limit + index + 1; return ( {rowNumber}
{report.report_name_kor}
{report.report_name_eng && (
{report.report_name_eng}
)}
{report.created_by || "-"} {formatDate(report.updated_at || report.created_at)}
); })}
{/* 페이지네이션 */} {totalPages > 1 && (
{page} / {totalPages}
)} {/* 삭제 확인 다이얼로그 */} !open && setDeleteTarget(null)}> 리포트 삭제 이 리포트를 삭제하시겠습니까?
삭제된 리포트는 복구할 수 없습니다.
취소 {isDeleting ? ( <> 삭제 중... ) : ( "삭제" )}
); }