summaryrefslogtreecommitdiff
path: root/lib/tbe-last/vendor/vendor-comment-dialog.tsx
blob: 8aa8d97c1ae9e46baf0a8f11c85619a6761062c8 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// lib/vendor-rfq-response/vendor-tbe-table/vendor-qa-dialog.tsx

"use client"

import * as React from "react"
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogDescription,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import { toast } from "sonner"
import { 
  MessageSquare, 
  Send, 
  Loader2, 
  Clock, 
  CheckCircle,
  AlertCircle 
} from "lucide-react"
import { formatDate } from "@/lib/utils"

interface VendorQuestion {
  id: string
  category: string
  question: string
  askedAt: string
  askedBy: number
  askedByName?: string
  answer?: string
  answeredAt?: string
  answeredBy?: number
  answeredByName?: string
  status: "open" | "answered" | "closed"
  priority?: "high" | "normal" | "low"
}

interface VendorQADialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  sessionId: number | null
  sessionDetail: any
  onQuestionSubmit: () => void
}

export function VendorQADialog({
  open,
  onOpenChange,
  sessionId,
  sessionDetail,
  onQuestionSubmit
}: VendorQADialogProps) {
  
  const [category, setCategory] = React.useState("general")
  const [priority, setPriority] = React.useState("normal")
  const [question, setQuestion] = React.useState("")
  const [isSubmitting, setIsSubmitting] = React.useState(false)
  const [questions, setQuestions] = React.useState<VendorQuestion[]>([])
  const [isLoading, setIsLoading] = React.useState(false)
  
  // Load questions when dialog opens
  React.useEffect(() => {
    if (open && sessionId) {
      loadQuestions()
    }
  }, [open, sessionId])
  
  const loadQuestions = async () => {
    if (!sessionId) return
    
    setIsLoading(true)
    try {
      const response = await fetch(`/api/tbe/sessions/${sessionId}/vendor-questions`)
      if (response.ok) {
        const data = await response.json()
        setQuestions(data)
      }
    } catch (error) {
      console.error("Failed to load questions:", error)
    } finally {
      setIsLoading(false)
    }
  }
  
  // Submit question
  const handleSubmit = async () => {
    if (!sessionId || !question.trim()) {
      toast.error("질문을 입력해주세요")
      return
    }
    
    setIsSubmitting(true)
    
    try {
      const response = await fetch(`/api/tbe/sessions/${sessionId}/vendor-questions`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          category,
          question,
          priority
        })
      })
      
      if (!response.ok) throw new Error("Failed to submit question")
      
      toast.success("질문이 제출되었습니다")
      
      // Reset form
      setCategory("general")
      setPriority("normal")
      setQuestion("")
      
      // Reload questions
      await loadQuestions()
      
      // Callback
      onQuestionSubmit()
      
    } catch (error) {
      console.error("Question submission error:", error)
      toast.error("질문 제출 중 오류가 발생했습니다")
    } finally {
      setIsSubmitting(false)
    }
  }
  
  // Get status icon
  const getStatusIcon = (status: string) => {
    switch (status) {
      case "answered":
        return <CheckCircle className="h-4 w-4 text-green-600" />
      case "closed":
        return <CheckCircle className="h-4 w-4 text-gray-600" />
      default:
        return <Clock className="h-4 w-4 text-orange-600" />
    }
  }
  
  // Get priority color
  const getPriorityColor = (priority?: string) => {
    switch (priority) {
      case "high":
        return "destructive"
      case "low":
        return "secondary"
      default:
        return "default"
    }
  }
  
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-3xl max-h-[80vh]">
        <DialogHeader>
          <DialogTitle>Q&A with Buyer</DialogTitle>
          <DialogDescription>
            {sessionDetail?.session?.sessionCode} - 구매자에게 질문하기
          </DialogDescription>
        </DialogHeader>
        
        <div className="space-y-4">
          {/* Previous Questions */}
          {questions.length > 0 && (
            <div>
              <Label className="text-sm font-medium mb-2">Previous Q&A</Label>
              <ScrollArea className="h-[200px] border rounded-lg p-3">
                <div className="space-y-3">
                  {questions.map(q => (
                    <div key={q.id} className="border-b pb-3 last:border-0">
                      <div className="flex items-start justify-between mb-2">
                        <div className="flex items-center gap-2">
                          {getStatusIcon(q.status)}
                          <Badge variant="outline" className="text-xs">
                            {q.category}
                          </Badge>
                          {q.priority && q.priority !== "normal" && (
                            <Badge variant={getPriorityColor(q.priority)} className="text-xs">
                              {q.priority}
                            </Badge>
                          )}
                        </div>
                        <span className="text-xs text-muted-foreground">
                          {formatDate(q.askedAt, "KR")}
                        </span>
                      </div>
                      
                      <div className="space-y-2">
                        <div className="text-sm">
                          <strong>Q:</strong> {q.question}
                        </div>
                        
                        {q.answer && (
                          <div className="text-sm text-muted-foreground ml-4">
                            <strong>A:</strong> {q.answer}
                            <span className="text-xs ml-2">
                              ({formatDate(q.answeredAt!, "KR")})
                            </span>
                          </div>
                        )}
                      </div>
                    </div>
                  ))}
                </div>
              </ScrollArea>
            </div>
          )}
          
          {/* New Question Form */}
          <div className="space-y-3">
            <div className="grid grid-cols-2 gap-3">
              <div>
                <Label htmlFor="category">Category</Label>
                <Select value={category} onValueChange={setCategory}>
                  <SelectTrigger>
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="general">일반 문의</SelectItem>
                    <SelectItem value="technical">기술 관련</SelectItem>
                    <SelectItem value="commercial">상업 조건</SelectItem>
                    <SelectItem value="delivery">납기 관련</SelectItem>
                    <SelectItem value="quality">품질 관련</SelectItem>
                    <SelectItem value="document">문서 관련</SelectItem>
                    <SelectItem value="clarification">명확화 요청</SelectItem>
                  </SelectContent>
                </Select>
              </div>
              
              <div>
                <Label htmlFor="priority">Priority</Label>
                <Select value={priority} onValueChange={setPriority}>
                  <SelectTrigger>
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="high">High</SelectItem>
                    <SelectItem value="normal">Normal</SelectItem>
                    <SelectItem value="low">Low</SelectItem>
                  </SelectContent>
                </Select>
              </div>
            </div>
            
            <div>
              <Label htmlFor="question">Your Question</Label>
              <Textarea
                id="question"
                value={question}
                onChange={(e) => setQuestion(e.target.value)}
                placeholder="구매자에게 질문할 내용을 입력하세요..."
                className="min-h-[100px]"
                disabled={isSubmitting}
              />
            </div>
            
            <div className="flex justify-end gap-2">
              <Button
                variant="outline"
                onClick={() => onOpenChange(false)}
                disabled={isSubmitting}
              >
                취소
              </Button>
              <Button
                onClick={handleSubmit}
                disabled={!question.trim() || isSubmitting}
              >
                {isSubmitting ? (
                  <>
                    <Loader2 className="h-4 w-4 mr-2 animate-spin" />
                    제출 중...
                  </>
                ) : (
                  <>
                    <Send className="h-4 w-4 mr-2" />
                    질문 제출
                  </>
                )}
              </Button>
            </div>
          </div>
          
          {/* Info Box */}
          <div className="p-3 bg-muted/50 rounded-lg">
            <div className="flex items-start gap-2">
              <AlertCircle className="h-4 w-4 text-muted-foreground mt-0.5" />
              <p className="text-xs text-muted-foreground">
                제출된 질문은 구매담당자가 확인 후 답변을 제공합니다.
                긴급한 질문은 Priority를 High로 설정해주세요.
              </p>
            </div>
          </div>
        </div>
      </DialogContent>
    </Dialog>
  )
}