summaryrefslogtreecommitdiff
path: root/lib/rfq-last/vendor/edit-contract-dialog.tsx
blob: 62b851fa286b3ed41bf769c4185a6c6260052af6 (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
"use client";

import * as React from "react";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import { Checkbox } from "@/components/ui/checkbox";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { Separator } from "@/components/ui/separator";
import { Badge } from "@/components/ui/badge";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { FileText, Shield, Globe, Info, Loader2 } from "lucide-react";
import { toast } from "sonner";
import { updateVendorContractRequirements } from "../service";

interface EditContractDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  rfqId: number;
  vendor: {
    detailId: number;
    vendorId: number;
    vendorName: string;
    vendorCode?: string;
    vendorCountry?: string;
    agreementYn?: boolean;
    ndaYn?: boolean;
    generalGtcYn?: boolean;
    projectGtcYn?: boolean;
    gtcType?: "general" | "project" | "none";
  };
  onSuccess: () => void;
}

export function EditContractDialog({
  open,
  onOpenChange,
  rfqId,
  vendor,
  onSuccess,
}: EditContractDialogProps) {
  const [isLoading, setIsLoading] = React.useState(false);
  
  // 기본계약 상태
  const [contractAgreement, setContractAgreement] = React.useState(false);
  const [contractNDA, setContractNDA] = React.useState(false);
  const [contractGTC, setContractGTC] = React.useState<"general" | "project" | "none">("none");
  
  // 국외 업체 확인
  const isInternational = React.useMemo(() => {
    return vendor?.vendorCountry && 
           vendor.vendorCountry !== "KR" && 
           vendor.vendorCountry !== "한국";
  }, [vendor]);
  
  // 초기값 설정
  React.useEffect(() => {
    if (open && vendor) {
      setContractAgreement(vendor.agreementYn || false);
      setContractNDA(vendor.ndaYn || false);
      
      // GTC 타입 결정
      if (vendor.gtcType) {
        setContractGTC(vendor.gtcType);
      } else if (vendor.generalGtcYn) {
        setContractGTC("general");
      } else if (vendor.projectGtcYn) {
        setContractGTC("project");
      } else {
        setContractGTC("none");
      }
    }
  }, [open, vendor]);
  
  // 제출 처리
  const handleSubmit = async () => {
    setIsLoading(true);
    
    try {
      const result = await updateVendorContractRequirements({
        rfqId,
        detailId: vendor.detailId,
        contractRequirements: {
          agreementYn: contractAgreement,
          ndaYn: contractNDA,
          gtcType: isInternational ? contractGTC : "none",
        },
      });
      
      if (result.success) {
        toast.success("기본계약 요구사항이 업데이트되었습니다.");
        onSuccess();
        onOpenChange(false);
      } else {
        toast.error(result.error || "업데이트에 실패했습니다.");
      }
    } catch (error) {
      console.error("Update error:", error);
      toast.error("오류가 발생했습니다.");
    } finally {
      setIsLoading(false);
    }
  };
  
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-md">
        <DialogHeader>
          <DialogTitle>기본계약 수정</DialogTitle>
          <DialogDescription>
            <div className="flex items-center gap-2 mt-2">
              <Badge variant="outline">{vendor?.vendorCode}</Badge>
              <span className="text-sm font-medium">{vendor?.vendorName}</span>
              {vendor?.vendorCountry && (
                <Badge 
                  variant={isInternational ? "secondary" : "default"}
                  className="text-xs"
                >
                  {vendor.vendorCountry}
                </Badge>
              )}
            </div>
          </DialogDescription>
        </DialogHeader>
        
        <div className="space-y-4 py-4">
          {/* 필수 계약 */}
          <div className="space-y-3">
            <Label className="text-sm font-semibold">필수 계약</Label>
            <div className="space-y-2">
              <div className="flex items-center space-x-2">
                <Checkbox
                  id="edit-agreement"
                  checked={contractAgreement}
                  onCheckedChange={(checked) => setContractAgreement(!!checked)}
                />
                <label
                  htmlFor="edit-agreement"
                  className="flex items-center gap-2 text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
                >
                  <FileText className="h-4 w-4 text-blue-500" />
                  기술자료 제공 동의
                </label>
              </div>
              <div className="flex items-center space-x-2">
                <Checkbox
                  id="edit-nda"
                  checked={contractNDA}
                  onCheckedChange={(checked) => setContractNDA(!!checked)}
                />
                <label
                  htmlFor="edit-nda"
                  className="flex items-center gap-2 text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
                >
                  <Shield className="h-4 w-4 text-green-500" />
                  비밀유지 계약 (NDA)
                </label>
              </div>
            </div>
          </div>
          
          {/* GTC 선택 (국외 업체만) */}
          {isInternational && (
            <>
              <Separator />
              <div className="space-y-3">
                <div className="flex items-center justify-between">
                  <Label className="text-sm font-semibold flex items-center gap-2">
                    <Globe className="h-4 w-4" />
                    GTC (General Terms & Conditions)
                  </Label>
                  <Badge variant="outline" className="text-xs">
                    국외 업체
                  </Badge>
                </div>
                <RadioGroup 
                  value={contractGTC} 
                  onValueChange={(value: any) => setContractGTC(value)}
                >
                  <div className="flex items-center space-x-2">
                    <RadioGroupItem value="none" id="edit-gtc-none" />
                    <label htmlFor="edit-gtc-none" className="text-sm">
                      GTC 요구하지 않음
                    </label>
                  </div>
                  <div className="flex items-center space-x-2">
                    <RadioGroupItem value="general" id="edit-gtc-general" />
                    <label htmlFor="edit-gtc-general" className="text-sm">
                      General GTC
                    </label>
                  </div>
                  <div className="flex items-center space-x-2">
                    <RadioGroupItem value="project" id="edit-gtc-project" />
                    <label htmlFor="edit-gtc-project" className="text-sm">
                      Project GTC
                    </label>
                  </div>
                </RadioGroup>
              </div>
            </>
          )}
          
          {/* 국내 업체 안내 */}
          {!isInternational && (
            <Alert>
              <Info className="h-4 w-4" />
              <AlertDescription>
                국내 업체는 GTC가 적용되지 않습니다.
              </AlertDescription>
            </Alert>
          )}
        </div>
        
        <DialogFooter>
          <Button
            variant="outline"
            onClick={() => onOpenChange(false)}
            disabled={isLoading}
          >
            취소
          </Button>
          <Button onClick={handleSubmit} disabled={isLoading}>
            {isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
            저장
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}