"use client"; import React, { useState, useEffect } from "react"; import { DashboardElement } from "@/components/admin/dashboard/types"; import { getApiUrl } from "@/lib/utils/apiUrl"; interface CustomerIssuesWidgetProps { element: DashboardElement; } interface Issue { id: string | number; issue_type?: string; issueType?: string; customer_name?: string; customerName?: string; description?: string; priority?: string; created_at?: string; createdAt?: string; status?: string; } /** * 고객 클레임/이슈 위젯 * - 클레임/이슈 목록 표시 * - 우선순위별 배지 표시 */ export default function CustomerIssuesWidget({ element }: CustomerIssuesWidgetProps) { const [issues, setIssues] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [filterPriority, setFilterPriority] = useState("all"); useEffect(() => { loadData(); // 자동 새로고침 (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"); const response = await fetch(getApiUrl("/api/dashboards/execute-query"), { 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(); if (result.success && result.data?.rows) { setIssues(result.data.rows); } setError(null); } catch (err) { setError(err instanceof Error ? err.message : "데이터 로딩 실패"); } finally { setLoading(false); } }; const getPriorityBadge = (priority: string) => { const priorityLower = priority?.toLowerCase() || ""; if (priorityLower.includes("긴급") || priorityLower.includes("high") || priorityLower.includes("urgent")) { return "bg-destructive text-destructive-foreground"; } else if (priorityLower.includes("보통") || priorityLower.includes("medium") || priorityLower.includes("normal")) { return "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-100"; } else if (priorityLower.includes("낮음") || priorityLower.includes("low")) { return "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-100"; } return "bg-muted text-muted-foreground"; }; const getStatusBadge = (status: string) => { const statusLower = status?.toLowerCase() || ""; if (statusLower.includes("처리중") || statusLower.includes("processing") || statusLower.includes("pending")) { return "bg-primary text-primary-foreground"; } else if (statusLower.includes("완료") || statusLower.includes("resolved") || statusLower.includes("closed")) { return "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-100"; } return "bg-muted text-muted-foreground"; }; const filteredIssues = filterPriority === "all" ? issues : issues.filter((issue) => { const priority = (issue.priority || "").toLowerCase(); return priority.includes(filterPriority); }); if (loading) { return (

데이터 로딩 중...

); } if (error) { return (

⚠️ {error}

); } if (!element?.dataSource?.query) { return (

클릭하여 데이터를 연결하세요

); } return (
{/* 헤더 */}

고객 클레임/이슈

{/* 필터 버튼 */}
{/* 총 건수 */}
{filteredIssues.length}
{/* 이슈 리스트 */}
{filteredIssues.length === 0 ? (

이슈가 없습니다

) : ( filteredIssues.map((issue, index) => (
{issue.priority || "보통"} {issue.status || "처리중"}

{issue.issue_type || issue.issueType || "기타"}

고객: {issue.customer_name || issue.customerName || "-"}

{issue.description || "설명 없음"}

{(issue.created_at || issue.createdAt) && (

{new Date(issue.created_at || issue.createdAt || "").toLocaleDateString("ko-KR")}

)}
)) )}
); }