"use client"; import * as React from "react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"; import { RefreshCw, Database, CheckCircle, AlertCircle, Clock, TrendingUp } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { syncMaterialGroupMaster, getMaterialGroupSyncStatus, type SyncResult } from "@/lib/material-groups/sync-service"; export function MaterialGroupSyncButton() { const [isOpen, setIsOpen] = React.useState(false); const [isLoading, setIsLoading] = React.useState(false); const [syncStatus, setSyncStatus] = React.useState<{ totalMDGRecords: number; totalMasterRecords: number; lastSyncDate?: string; } | null>(null); const [lastSyncResult, setLastSyncResult] = React.useState(null); const { toast } = useToast(); // 다이얼로그 열릴 때 상태 정보 로드 React.useEffect(() => { if (isOpen) { loadSyncStatus(); } }, [isOpen]); const loadSyncStatus = async () => { try { const status = await getMaterialGroupSyncStatus(); if (status.success && status.data) { setSyncStatus(status.data); } } catch (error) { console.error("동기화 상태 로드 실패:", error); } }; const handleSync = async () => { setIsLoading(true); try { const result = await syncMaterialGroupMaster(); setLastSyncResult(result); if (result.success) { toast({ title: "동기화 완료", description: result.message, variant: "default", }); // 상태 다시 로드 await loadSyncStatus(); } else { toast({ title: "동기화 실패", description: result.message, variant: "destructive", }); } } catch (error) { toast({ title: "동기화 오류", description: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.", variant: "destructive", }); } finally { setIsLoading(false); } }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleString('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }); }; const getSyncBadgeVariant = () => { if (!syncStatus) return "secondary"; if (syncStatus.totalMDGRecords === syncStatus.totalMasterRecords) { return "default"; // 동기화됨 } return "destructive"; // 동기화 필요 }; return ( 자재그룹 마스터 동기화 MDG 테이블로부터 자재그룹 마스터 데이터를 동기화합니다.
{/* 현재 상태 */} {syncStatus && (

현재 상태

MDG 자재그룹:
{syncStatus.totalMDGRecords.toLocaleString()}개
마스터 테이블:
{syncStatus.totalMasterRecords.toLocaleString()}개
{syncStatus.lastSyncDate && (
최근 동기화:
{formatDate(syncStatus.lastSyncDate)}
)}
{syncStatus.totalMDGRecords === syncStatus.totalMasterRecords ? "동기화됨" : "동기화 필요"}
)} {/* 마지막 동기화 결과 */} {lastSyncResult && (

{lastSyncResult.success ? ( ) : ( )} 마지막 동기화 결과

{lastSyncResult.message}

{lastSyncResult.data && (
{lastSyncResult.data.newRecords}
신규
{lastSyncResult.data.updatedRecords}
업데이트
{lastSyncResult.data.skippedRecords}
건너뜀
)}
)} {/* 동기화 실행 버튼 */}

* 동기화는 기존 데이터를 업데이트하며 새로운 자재그룹을 추가합니다.

); }