"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { ResizableDialog, ResizableDialogContent, ResizableDialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Plus, Check, Trash2 } from "lucide-react"; import YardLayoutCreateModal from "./yard-3d/YardLayoutCreateModal"; import YardEditor from "./yard-3d/YardEditor"; import Yard3DViewer from "./yard-3d/Yard3DViewer"; import { yardLayoutApi } from "@/lib/api/yardLayoutApi"; import type { YardManagementConfig } from "../types"; interface YardLayout { id: number; name: string; description: string; placement_count: number; created_at: string; updated_at: string; } interface YardManagement3DWidgetProps { isEditMode?: boolean; config?: YardManagementConfig; onConfigChange?: (config: YardManagementConfig) => void; } export default function YardManagement3DWidget({ isEditMode = false, config, onConfigChange, }: YardManagement3DWidgetProps) { const [layouts, setLayouts] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const [editingLayout, setEditingLayout] = useState(null); const [deleteLayoutId, setDeleteLayoutId] = useState(null); // 레이아웃 목록 로드 const loadLayouts = async () => { try { setIsLoading(true); const response = await yardLayoutApi.getAllLayouts(); if (response.success) { setLayouts(response.data as YardLayout[]); } } catch (error) { console.error("야드 레이아웃 목록 조회 실패:", error); } finally { setIsLoading(false); } }; useEffect(() => { if (isEditMode) { loadLayouts(); } }, [isEditMode]); // 레이아웃 목록이 로드되었고, 설정이 없으면 첫 번째 레이아웃 자동 선택 useEffect(() => { if (isEditMode && layouts.length > 0 && !config?.layoutId && onConfigChange) { // console.log("🔧 첫 번째 야드 레이아웃 자동 선택:", layouts[0]); onConfigChange({ layoutId: layouts[0].id, layoutName: layouts[0].name, }); } }, [isEditMode, layouts, config?.layoutId, onConfigChange]); // 레이아웃 선택 (편집 모드에서만) const handleSelectLayout = (layout: YardLayout) => { if (onConfigChange) { onConfigChange({ layoutId: layout.id, layoutName: layout.name, }); } }; // 새 레이아웃 생성 const handleCreateLayout = async (name: string) => { try { const response = await yardLayoutApi.createLayout({ name }); if (response.success) { await loadLayouts(); setIsCreateModalOpen(false); setEditingLayout(response.data as YardLayout); } } catch (error) { console.error("야드 레이아웃 생성 실패:", error); throw error; } }; // 편집 완료 const handleEditComplete = () => { if (editingLayout && onConfigChange) { onConfigChange({ layoutId: editingLayout.id, layoutName: editingLayout.name, }); } setEditingLayout(null); loadLayouts(); }; // 레이아웃 삭제 const handleDeleteLayout = async () => { if (!deleteLayoutId) return; try { const response = await yardLayoutApi.deleteLayout(deleteLayoutId); if (response.success) { // 삭제된 레이아웃이 현재 선택된 레이아웃이면 설정 초기화 if (config?.layoutId === deleteLayoutId && onConfigChange) { onConfigChange({ layoutId: 0, layoutName: "" }); } await loadLayouts(); } } catch (error) { console.error("레이아웃 삭제 실패:", error); } finally { setDeleteLayoutId(null); } }; // 편집 모드: 편집 중인 경우 YardEditor 표시 if (isEditMode && editingLayout) { return (
); } // 편집 모드: 레이아웃 선택 UI if (isEditMode) { return (

야드 레이아웃 선택

{config?.layoutName ? `선택됨: ${config.layoutName}` : "표시할 야드 레이아웃을 선택하세요"}

{isLoading ? (
로딩 중...
) : layouts.length === 0 ? (
🏗️
생성된 야드 레이아웃이 없습니다
먼저 야드 레이아웃을 생성하세요
) : (
{layouts.map((layout) => (
))}
)}
{/* 생성 모달 */} setIsCreateModalOpen(false)} onCreate={handleCreateLayout} /> {/* 삭제 확인 모달 */} { if (!open) setDeleteLayoutId(null); }} > e.stopPropagation()} className="sm:max-w-[425px]"> 야드 레이아웃 삭제

이 야드 레이아웃을 삭제하시겠습니까?
레이아웃 내의 모든 배치 정보도 함께 삭제됩니다.
이 작업은 되돌릴 수 없습니다.

); } // 뷰 모드: 선택된 레이아웃의 3D 뷰어 표시 if (!config?.layoutId) { console.warn("⚠️ 야드관리 위젯: layoutId가 설정되지 않음", { config, isEditMode }); return (
🏗️
야드 레이아웃이 설정되지 않았습니다
대시보드 편집에서 레이아웃을 선택하세요
디버그: config={JSON.stringify(config)}
); } // 선택된 레이아웃의 3D 뷰어 표시 return (
); }