summaryrefslogtreecommitdiff
path: root/lib/b-rfq/vendor-response/waive-response-dialog.tsx
blob: 5ded4da38a4c36c9c4a8fd1430d51a39eed834a3 (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
// components/rfq/waive-response-dialog.tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog";
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form";
import { Textarea } from "@/components/ui/textarea";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import { FileX, Loader2, AlertTriangle } from "lucide-react";
import { useToast } from "@/hooks/use-toast";
import { useRouter } from "next/navigation";

const waiveFormSchema = z.object({
  responseComment: z.string().min(1, "포기 사유를 입력해주세요"),
  vendorComment: z.string().optional(),
});

type WaiveFormData = z.infer<typeof waiveFormSchema>;

interface WaiveResponseDialogProps {
  responseId: number;
  attachmentType: string;
  serialNo: string;
  trigger?: React.ReactNode;
  onSuccess?: () => void;
}

export function WaiveResponseDialog({
  responseId,
  attachmentType,
  serialNo,
  trigger,
  onSuccess,
}: WaiveResponseDialogProps) {
  const [open, setOpen] = useState(false);
  const [isSubmitting, setIsSubmitting] = useState(false);
  const { toast } = useToast();
  const router = useRouter();

  const form = useForm<WaiveFormData>({
    resolver: zodResolver(waiveFormSchema),
    defaultValues: {
      responseComment: "",
      vendorComment: "",
    },
  });

  const onSubmit = async (data: WaiveFormData) => {
    setIsSubmitting(true);
    
    try {
      const response = await fetch("/api/vendor-responses/waive", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          responseId,
          responseComment: data.responseComment,
          vendorComment: data.vendorComment,
        }),
      });
      
      if (!response.ok) {
        const error = await response.json();
        throw new Error(error.message || "응답 포기 처리 실패");
      }
      
      toast({
        title: "응답 포기 완료",
        description: "해당 항목에 대한 응답이 포기 처리되었습니다.",
      });
      
      setOpen(false);
      form.reset();

      router.refresh();
      onSuccess?.();
      
    } catch (error) {
      console.error("Waive error:", error);
      toast({
        title: "처리 실패",
        description: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.",
        variant: "destructive",
      });
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        {trigger || (
          <Button size="sm" variant="outline">
            <FileX className="h-3 w-3 mr-1" />
            포기
          </Button>
        )}
      </DialogTrigger>
      <DialogContent className="max-w-lg">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2 text-orange-600">
            <FileX className="h-5 w-5" />
            응답 포기
          </DialogTitle>
          <div className="flex items-center gap-2 text-sm text-muted-foreground">
            <Badge variant="outline">{serialNo}</Badge>
            <span>{attachmentType}</span>
          </div>
        </DialogHeader>

        <div className="bg-orange-50 border border-orange-200 rounded-lg p-4 mb-4">
          <div className="flex items-center gap-2 text-orange-800 text-sm font-medium mb-2">
            <AlertTriangle className="h-4 w-4" />
            주의사항
          </div>
          <p className="text-orange-700 text-sm">
            응답을 포기하면 해당 항목에 대한 입찰 참여가 불가능합니다. 
            포기 사유를 명확히 기입해 주세요.
          </p>
        </div>

        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
            {/* 포기 사유 (필수) */}
            <FormField
              control={form.control}
              name="responseComment"
              render={({ field }) => (
                <FormItem>
                  <FormLabel className="text-red-600">
                    포기 사유 <span className="text-red-500">*</span>
                  </FormLabel>
                  <FormControl>
                    <Textarea
                      placeholder="응답을 포기하는 사유를 구체적으로 입력하세요..."
                      className="resize-none"
                      rows={4}
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />

            {/* 내부 코멘트 (선택) */}
            <FormField
              control={form.control}
              name="vendorComment"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>내부 코멘트 (선택)</FormLabel>
                  <FormControl>
                    <Textarea
                      placeholder="내부 참고용 코멘트를 입력하세요..."
                      className="resize-none"
                      rows={2}
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />

            {/* 버튼 */}
            <div className="flex justify-end gap-2">
              <Button
                type="button"
                variant="outline"
                onClick={() => setOpen(false)}
                disabled={isSubmitting}
              >
                취소
              </Button>
              <Button 
                type="submit" 
                variant="destructive" 
                disabled={isSubmitting}
              >
                {isSubmitting && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
                {isSubmitting ? "처리 중..." : "포기하기"}
              </Button>
            </div>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  );
}