summaryrefslogtreecommitdiff
path: root/lib/techsales-rfq/vendor-response/detail/communication-tab.tsx
blob: 0332232cb43af37e044cbd3b7848c1d9bc01c24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
"use client"

import * as React from "react"
import { useState } from "react"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
import { Textarea } from "@/components/ui/textarea"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Separator } from "@/components/ui/separator"
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Send, MessageCircle } from "lucide-react"
import { formatDateTime } from "@/lib/utils"
import { toast } from "sonner"

interface CommunicationTabProps {
  quotation: {
    id: number
    rfq: {
      id: number
      rfqCode: string | null
      createdByUser?: {
        id: number
        name: string | null
        email: string | null
      } | null
    } | null
    vendor: {
      vendorName: string
    } | null
  }
}

// 임시 코멘트 데이터 (실제로는 API에서 가져와야 함)
const MOCK_COMMENTS = [
  {
    id: 1,
    content: "안녕하세요. 해당 자재에 대한 견적 요청 드립니다. 납기일은 언제까지 가능한지 문의드립니다.",
    createdAt: new Date("2024-01-15T09:00:00"),
    author: {
      name: "김구매",
      email: "buyer@company.com",
      role: "구매담당자"
    }
  },
  {
    id: 2,
    content: "안녕하세요. 견적 요청 확인했습니다. 해당 자재의 경우 약 2주 정도의 제작 기간이 필요합니다. 상세한 견적은 내일까지 제출하겠습니다.",
    createdAt: new Date("2024-01-15T14:30:00"),
    author: {
      name: "이벤더",
      email: "vendor@supplier.com",
      role: "벤더"
    }
  },
  {
    id: 3,
    content: "감사합니다. 추가로 품질 인증서도 함께 제출 가능한지 확인 부탁드립니다.",
    createdAt: new Date("2024-01-16T10:15:00"),
    author: {
      name: "김구매",
      email: "buyer@company.com",
      role: "구매담당자"
    }
  }
]

export function CommunicationTab({ quotation }: CommunicationTabProps) {
  const [newComment, setNewComment] = useState("")
  const [isLoading, setIsLoading] = useState(false)
  const [comments, setComments] = useState(MOCK_COMMENTS)

  const handleSendComment = async () => {
    if (!newComment.trim()) {
      toast.error("메시지를 입력해주세요.")
      return
    }

    setIsLoading(true)
    try {
      // TODO: API 호출로 코멘트 전송
      const newCommentData = {
        id: comments.length + 1,
        content: newComment,
        createdAt: new Date(),
        author: {
          name: "현재사용자", // 실제로는 세션에서 가져와야 함
          email: "current@user.com",
          role: "벤더"
        }
      }
      
      setComments([...comments, newCommentData])
      setNewComment("")
      toast.success("메시지가 전송되었습니다.")
    } catch {
      toast.error("메시지 전송 중 오류가 발생했습니다.")
    } finally {
      setIsLoading(false)
    }
  }

  const getAuthorInitials = (name: string) => {
    return name
      .split(" ")
      .map(word => word[0])
      .join("")
      .toUpperCase()
      .slice(0, 2)
  }

  const getRoleBadgeVariant = (role: string) => {
    return role === "구매담당자" ? "default" : "secondary"
  }

  return (
    <div className="h-full flex flex-col">
      {/* 헤더 */}
      <Card className="mb-4">
        <CardHeader>
          <CardTitle className="flex items-center gap-2">
            <MessageCircle className="h-5 w-5" />
            커뮤니케이션
          </CardTitle>
          <CardDescription>
            RFQ {quotation.rfq?.rfqCode || "미할당"}에 대한 구매담당자와의 커뮤니케이션
          </CardDescription>
        </CardHeader>
        <CardContent>
          <div className="flex items-center gap-4 text-sm text-muted-foreground">
            <span>구매담당자: {quotation.rfq?.createdByUser?.name || "N/A"}</span>
            <span>•</span>
            <span>벤더: {quotation.vendor?.vendorName}</span>
          </div>
        </CardContent>
      </Card>

      {/* 메시지 목록 */}
      <Card className="flex-1 flex flex-col min-h-0">
        <CardHeader>
          <CardTitle className="text-lg">메시지 ({comments.length})</CardTitle>
        </CardHeader>
        <CardContent className="flex-1 flex flex-col min-h-0">
          <ScrollArea className="flex-1 pr-4">
            <div className="space-y-4">
              {comments.length === 0 ? (
                <div className="text-center py-8 text-muted-foreground">
                  <MessageCircle className="h-12 w-12 mx-auto mb-4 opacity-50" />
                  <p>아직 메시지가 없습니다.</p>
                  <p className="text-sm">첫 번째 메시지를 보내보세요.</p>
                </div>
              ) : (
                comments.map((comment) => (
                  <div key={comment.id} className="flex gap-3">
                    <Avatar className="h-8 w-8 mt-1">
                      <AvatarFallback className="text-xs">
                        {getAuthorInitials(comment.author.name)}
                      </AvatarFallback>
                    </Avatar>
                    <div className="flex-1 space-y-2">
                      <div className="flex items-center gap-2">
                        <span className="font-medium text-sm">{comment.author.name}</span>
                        <Badge variant={getRoleBadgeVariant(comment.author.role)} className="text-xs">
                          {comment.author.role}
                        </Badge>
                        <span className="text-xs text-muted-foreground">
                          {formatDateTime(comment.createdAt)}
                        </span>
                      </div>
                      <div className="bg-muted p-3 rounded-lg text-sm">
                        {comment.content}
                      </div>
                    </div>
                  </div>
                ))
              )}
            </div>
          </ScrollArea>

          <Separator className="my-4" />

          {/* 새 메시지 입력 */}
          <div className="space-y-3">
            <Textarea
              placeholder="메시지를 입력하세요..."
              value={newComment}
              onChange={(e) => setNewComment(e.target.value)}
              rows={3}
              className="resize-none"
              onKeyDown={(e) => {
                if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
                  e.preventDefault()
                  handleSendComment()
                }
              }}
            />
            <div className="flex justify-between items-center">
              <div className="text-xs text-muted-foreground">
                Ctrl + Enter로 빠른 전송
              </div>
              <Button
                onClick={handleSendComment}
                disabled={isLoading || !newComment.trim()}
                size="sm"
              >
                <Send className="h-4 w-4 mr-2" />
                전송
              </Button>
            </div>
          </div>
        </CardContent>
      </Card>
    </div>
  )
}