"use client"; import React, { useState, useEffect } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Loader2, Plus, Trash2, Download, ShieldCheck } from "lucide-react"; import { cn } from "@/lib/utils"; import { apiClient } from "@/lib/api/client"; interface BomVersion { id: string; version_name: string; revision: number; status: string; created_by: string; created_date: string; } interface BomVersionModalProps { open: boolean; onOpenChange: (open: boolean) => void; bomId: string | null; tableName?: string; detailTable?: string; onVersionLoaded?: () => void; } const STATUS_STYLE: Record = { developing: { label: "개발중", className: "bg-red-50 text-red-600 ring-red-200" }, active: { label: "사용", className: "bg-emerald-50 text-emerald-600 ring-emerald-200" }, inactive: { label: "사용중지", className: "bg-gray-100 text-gray-500 ring-gray-200" }, }; export function BomVersionModal({ open, onOpenChange, bomId, tableName = "bom_version", detailTable = "bom_detail", onVersionLoaded }: BomVersionModalProps) { const [versions, setVersions] = useState([]); const [loading, setLoading] = useState(false); const [creating, setCreating] = useState(false); const [actionId, setActionId] = useState(null); useEffect(() => { if (open && bomId) loadVersions(); }, [open, bomId]); const loadVersions = async () => { if (!bomId) return; setLoading(true); try { const res = await apiClient.get(`/bom/${bomId}/versions`, { params: { tableName } }); if (res.data?.success) setVersions(res.data.data || []); } catch (error) { console.error("[BomVersion] 로드 실패:", error); } finally { setLoading(false); } }; const handleCreateVersion = async () => { if (!bomId) return; setCreating(true); try { const res = await apiClient.post(`/bom/${bomId}/versions`, { tableName, detailTable }); if (res.data?.success) loadVersions(); } catch (error) { console.error("[BomVersion] 생성 실패:", error); } finally { setCreating(false); } }; const handleLoadVersion = async (versionId: string) => { if (!bomId) return; setActionId(versionId); try { const res = await apiClient.post(`/bom/${bomId}/versions/${versionId}/load`, { tableName, detailTable }); if (res.data?.success) { onVersionLoaded?.(); loadVersions(); } } catch (error) { console.error("[BomVersion] 불러오기 실패:", error); } finally { setActionId(null); } }; const handleActivateVersion = async (versionId: string) => { if (!bomId || !confirm("이 버전을 사용 확정하시겠습니까?\n기존 사용중 버전은 사용중지로 변경됩니다.")) return; setActionId(versionId); try { const res = await apiClient.post(`/bom/${bomId}/versions/${versionId}/activate`, { tableName }); if (res.data?.success) { loadVersions(); window.dispatchEvent(new CustomEvent("refreshTable")); } } catch (error) { console.error("[BomVersion] 사용 확정 실패:", error); } finally { setActionId(null); } }; const handleDeleteVersion = async (versionId: string) => { if (!bomId || !confirm("이 버전을 삭제하시겠습니까?")) return; setActionId(versionId); try { const res = await apiClient.delete(`/bom/${bomId}/versions/${versionId}`, { params: { tableName } }); if (res.data?.success) loadVersions(); } catch (error) { console.error("[BomVersion] 삭제 실패:", error); } finally { setActionId(null); } }; const formatDate = (dateStr: string) => { if (!dateStr) return "-"; try { return new Date(dateStr).toLocaleDateString("ko-KR", { year: "numeric", month: "2-digit", day: "2-digit", }); } catch { return dateStr; } }; const getStatus = (status: string) => STATUS_STYLE[status] || STATUS_STYLE.inactive; return ( 버전 관리 BOM 버전을 관리합니다. 불러오기로 특정 버전을 복원할 수 있습니다.
{loading ? (
) : versions.length === 0 ? (

생성된 버전이 없습니다

) : ( versions.map((ver) => { const st = getStatus(ver.status); const isActing = actionId === ver.id; return (
Version {ver.version_name} {st.label}
차수: {ver.revision} 등록일: {formatDate(ver.created_date)} {ver.created_by && 등록자: {ver.created_by}}
{ver.status === "active" ? ( 사용중 ) : ( <> )}
); }) )}
); }