2025-10-15 16:16:27 +09:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import React, { useState, useEffect } from "react";
|
|
|
|
|
|
import { DashboardElement } from "@/components/admin/dashboard/types";
|
2025-10-24 16:08:57 +09:00
|
|
|
|
import { getApiUrl } from "@/lib/utils/apiUrl";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
|
|
|
|
|
|
interface DeliveryStatusSummaryWidgetProps {
|
|
|
|
|
|
element: DashboardElement;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface DeliveryStatus {
|
|
|
|
|
|
status: string;
|
|
|
|
|
|
count: number;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 배송 상태 요약 위젯
|
|
|
|
|
|
* - 배송중, 완료, 지연, 픽업 대기 상태별 카운트 표시
|
|
|
|
|
|
*/
|
|
|
|
|
|
export default function DeliveryStatusSummaryWidget({ element }: DeliveryStatusSummaryWidgetProps) {
|
|
|
|
|
|
const [statusData, setStatusData] = useState<DeliveryStatus[]>([]);
|
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
loadData();
|
2025-10-22 13:40:15 +09:00
|
|
|
|
|
2025-10-15 16:16:27 +09:00
|
|
|
|
// 자동 새로고침 (30초마다)
|
|
|
|
|
|
const interval = setInterval(loadData, 30000);
|
|
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
|
}, [element]);
|
|
|
|
|
|
|
|
|
|
|
|
const loadData = async () => {
|
|
|
|
|
|
if (!element?.dataSource?.query) {
|
|
|
|
|
|
setError("쿼리가 설정되지 않았습니다");
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
const token = localStorage.getItem("authToken");
|
2025-10-24 16:08:57 +09:00
|
|
|
|
const response = await fetch(getApiUrl("/api/dashboards/execute-query"), {
|
2025-10-15 16:16:27 +09:00
|
|
|
|
method: "POST",
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
|
},
|
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
|
query: element.dataSource.query,
|
|
|
|
|
|
connectionType: element.dataSource.connectionType || "current",
|
|
|
|
|
|
connectionId: element.dataSource.connectionId,
|
|
|
|
|
|
}),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!response.ok) throw new Error("데이터 로딩 실패");
|
|
|
|
|
|
|
|
|
|
|
|
const result = await response.json();
|
2025-10-22 13:40:15 +09:00
|
|
|
|
|
2025-10-15 16:16:27 +09:00
|
|
|
|
// 데이터 처리
|
|
|
|
|
|
if (result.success && result.data?.rows) {
|
|
|
|
|
|
const rows = result.data.rows;
|
2025-10-22 13:40:15 +09:00
|
|
|
|
|
2025-10-15 16:16:27 +09:00
|
|
|
|
// 상태별 카운트 계산
|
|
|
|
|
|
const statusCounts = rows.reduce((acc: any, row: any) => {
|
|
|
|
|
|
const status = row.status || "알 수 없음";
|
|
|
|
|
|
acc[status] = (acc[status] || 0) + 1;
|
|
|
|
|
|
return acc;
|
|
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
|
|
|
|
const formattedData: DeliveryStatus[] = [
|
|
|
|
|
|
{ status: "배송중", count: statusCounts["배송중"] || statusCounts["delivering"] || 0 },
|
|
|
|
|
|
{ status: "완료", count: statusCounts["완료"] || statusCounts["delivered"] || 0 },
|
|
|
|
|
|
{ status: "지연", count: statusCounts["지연"] || statusCounts["delayed"] || 0 },
|
|
|
|
|
|
{ status: "픽업 대기", count: statusCounts["픽업 대기"] || statusCounts["pending"] || 0 },
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
setStatusData(formattedData);
|
|
|
|
|
|
}
|
2025-10-22 13:40:15 +09:00
|
|
|
|
|
2025-10-15 16:16:27 +09:00
|
|
|
|
setError(null);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
setError(err instanceof Error ? err.message : "데이터 로딩 실패");
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getBorderColor = (status: string) => {
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
case "배송중":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "border-primary";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "완료":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "border-success";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "지연":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "border-destructive";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "픽업 대기":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "border-warning";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
default:
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "border-border";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getDotColor = (status: string) => {
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
case "배송중":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "bg-primary";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "완료":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "bg-success";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "지연":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "bg-destructive";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "픽업 대기":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "bg-warning/100";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
default:
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "bg-muted0";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getTextColor = (status: string) => {
|
|
|
|
|
|
switch (status) {
|
|
|
|
|
|
case "배송중":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "text-primary";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "완료":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "text-success";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "지연":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "text-destructive";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
case "픽업 대기":
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "text-warning";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
default:
|
2025-10-29 17:53:03 +09:00
|
|
|
|
return "text-foreground";
|
2025-10-15 16:16:27 +09:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex h-full items-center justify-center">
|
|
|
|
|
|
<div className="text-center">
|
|
|
|
|
|
<div className="border-primary mx-auto h-8 w-8 animate-spin rounded-full border-2 border-t-transparent" />
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<p className="mt-2 text-sm text-muted-foreground">데이터 로딩 중...</p>
|
2025-10-15 16:16:27 +09:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex h-full items-center justify-center">
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<div className="text-center text-destructive">
|
2025-10-15 16:16:27 +09:00
|
|
|
|
<p className="text-sm">⚠️ {error}</p>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={loadData}
|
2025-10-29 17:53:03 +09:00
|
|
|
|
className="mt-2 rounded bg-destructive/10 px-3 py-1 text-xs text-destructive hover:bg-destructive/20"
|
2025-10-15 16:16:27 +09:00
|
|
|
|
>
|
|
|
|
|
|
다시 시도
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!element?.dataSource?.query) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex h-full items-center justify-center">
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<div className="text-center text-muted-foreground">
|
2025-10-22 13:40:15 +09:00
|
|
|
|
<p className="text-sm">데이터를 연결하세요</p>
|
2025-10-15 16:16:27 +09:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const totalCount = statusData.reduce((sum, item) => sum + item.count, 0);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<div className="flex h-full w-full flex-col overflow-hidden bg-gradient-to-br from-background to-primary/10 p-2">
|
2025-10-15 16:16:27 +09:00
|
|
|
|
{/* 헤더 */}
|
|
|
|
|
|
<div className="mb-2 flex flex-shrink-0 items-center justify-between">
|
|
|
|
|
|
<div className="flex-1">
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<h3 className="text-sm font-bold text-foreground">📊 배송 상태 요약</h3>
|
2025-10-15 16:16:27 +09:00
|
|
|
|
{totalCount > 0 ? (
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<p className="text-xs text-muted-foreground">총 {totalCount.toLocaleString()}건</p>
|
2025-10-15 16:16:27 +09:00
|
|
|
|
) : (
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<p className="text-xs text-warning">⚙️ 데이터 연결 필요</p>
|
2025-10-15 16:16:27 +09:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={loadData}
|
2025-10-29 17:53:03 +09:00
|
|
|
|
className="border-border hover:bg-accent flex h-7 w-7 items-center justify-center rounded border bg-background p-0 text-xs disabled:opacity-50"
|
2025-10-15 16:16:27 +09:00
|
|
|
|
disabled={loading}
|
|
|
|
|
|
>
|
|
|
|
|
|
{loading ? "⏳" : "🔄"}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 스크롤 가능한 콘텐츠 영역 */}
|
|
|
|
|
|
<div className="flex-1 overflow-y-auto">
|
|
|
|
|
|
{/* 상태별 카드 */}
|
|
|
|
|
|
<div className="grid grid-cols-2 gap-1.5">
|
|
|
|
|
|
{statusData.map((item) => (
|
|
|
|
|
|
<div
|
|
|
|
|
|
key={item.status}
|
2025-10-29 17:53:03 +09:00
|
|
|
|
className={`rounded border-l-2 bg-background p-1.5 shadow-sm ${getBorderColor(item.status)}`}
|
2025-10-15 16:16:27 +09:00
|
|
|
|
>
|
|
|
|
|
|
<div className="mb-0.5 flex items-center gap-1">
|
|
|
|
|
|
<div className={`h-1.5 w-1.5 rounded-full ${getDotColor(item.status)}`}></div>
|
2025-10-29 17:53:03 +09:00
|
|
|
|
<div className="text-xs font-medium text-foreground">{item.status}</div>
|
2025-10-15 16:16:27 +09:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className={`text-lg font-bold ${getTextColor(item.status)}`}>{item.count.toLocaleString()}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|