From f63cf682d6846210a04ce4a3eb8ebe9afd6d6dba Mon Sep 17 00:00:00 2001
From: joonhoekim <26rote@gmail.com>
Date: Tue, 14 Oct 2025 20:27:47 +0900
Subject: (김준회) 자재코드 선택기 구현 및 일반견적 생성시 반영
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/rfq-last/table/create-general-rfq-dialog.tsx | 108 ++++++++++++++++++-----
 1 file changed, 85 insertions(+), 23 deletions(-)

(limited to 'lib/rfq-last/table/create-general-rfq-dialog.tsx')

diff --git a/lib/rfq-last/table/create-general-rfq-dialog.tsx b/lib/rfq-last/table/create-general-rfq-dialog.tsx
index 4ad21695..b46249f0 100644
--- a/lib/rfq-last/table/create-general-rfq-dialog.tsx
+++ b/lib/rfq-last/table/create-general-rfq-dialog.tsx
@@ -50,6 +50,8 @@ import { Separator } from "@/components/ui/separator"
 import { createGeneralRfqAction, previewGeneralRfqCode } from "../service"
 import { MaterialGroupSelectorDialogSingle } from "@/components/common/material/material-group-selector-dialog-single"
 import { MaterialSearchItem } from "@/lib/material/material-group-service" // 단순 타입 임포트 목적
+import { MaterialSelectorDialogSingle } from "@/components/common/selectors/material/material-selector-dialog-single"
+import { MaterialSearchItem as SAPMaterialSearchItem } from "@/components/common/selectors/material/material-service"
 import { ProjectSelector } from "@/components/ProjectSelector"
 import { 
   PurchaseGroupCodeSingleSelector,
@@ -60,6 +62,8 @@ import {
 const itemSchema = z.object({
   itemCode: z.string().optional(),
   itemName: z.string().min(1, "자재명을 입력해주세요"),
+  materialCode: z.string().optional(),
+  materialName: z.string().optional(),
   quantity: z.number().min(1, "수량은 1 이상이어야 합니다"),
   uom: z.string().min(1, "단위를 입력해주세요"),
   remark: z.string().optional(),
@@ -99,23 +103,25 @@ export function CreateGeneralRfqDialog({ onSuccess }: CreateGeneralRfqDialogProp
 
   const form = useForm<CreateGeneralRfqFormValues>({
     resolver: zodResolver(createGeneralRfqSchema),
-    defaultValues: {
-      rfqType: "",
-      rfqTitle: "",
-      dueDate: undefined,
-      picUserId: userId || undefined,
-      projectId: undefined,
-      remark: "",
-      items: [
-        {
-          itemCode: "",
-          itemName: "",
-          quantity: 1,
-          uom: "",
-          remark: "",
-        },
-      ],
-    },
+      defaultValues: {
+        rfqType: "",
+        rfqTitle: "",
+        dueDate: undefined,
+        picUserId: userId || undefined,
+        projectId: undefined,
+        remark: "",
+        items: [
+          {
+            itemCode: "",
+            itemName: "",
+            materialCode: "",
+            materialName: "",
+            quantity: 1,
+            uom: "",
+            remark: "",
+          },
+        ],
+      },
   })
 
   const { fields, append, remove } = useFieldArray({
@@ -195,6 +201,8 @@ export function CreateGeneralRfqDialog({ onSuccess }: CreateGeneralRfqDialogProp
           {
             itemCode: "",
             itemName: "",
+            materialCode: "",
+            materialName: "",
             quantity: 1,
             uom: "",
             remark: "",
@@ -208,7 +216,25 @@ export function CreateGeneralRfqDialog({ onSuccess }: CreateGeneralRfqDialogProp
   }
 
   const handleCancel = () => {
-    form.reset()
+    form.reset({
+      rfqType: "",
+      rfqTitle: "",
+      dueDate: undefined,
+      picUserId: userId || undefined,
+      projectId: undefined,
+      remark: "",
+      items: [
+        {
+          itemCode: "",
+          itemName: "",
+          materialCode: "",
+          materialName: "",
+          quantity: 1,
+          uom: "",
+          remark: "",
+        },
+      ],
+    })
     setOpen(false)
   }
 
@@ -232,6 +258,8 @@ export function CreateGeneralRfqDialog({ onSuccess }: CreateGeneralRfqDialogProp
         items: data.items as Array<{
           itemCode: string;
           itemName: string;
+          materialCode?: string;
+          materialName?: string;
           quantity: number;
           uom: string;
           remark?: string;
@@ -275,6 +303,8 @@ export function CreateGeneralRfqDialog({ onSuccess }: CreateGeneralRfqDialogProp
     append({
       itemCode: "",
       itemName: "",
+      materialCode: "",
+      materialName: "",
       quantity: 1,
       uom: "",
       remark: "",
@@ -541,14 +571,14 @@ export function CreateGeneralRfqDialog({ onSuccess }: CreateGeneralRfqDialogProp
                         )}
                       </div>
                       
-                      {/* 자재 선택 - 그리드 외부 */}
+                      {/* 자재그룹 선택 - 그리드 외부 */}
                       <div className="mb-3">
                         <FormLabel className="text-xs">
                           자재그룹(자재그룹명) <span className="text-red-500">*</span>
                         </FormLabel>
                         <div className="mt-1">
                           <MaterialGroupSelectorDialogSingle
-                            triggerLabel="자재 선택"
+                            triggerLabel="자재그룹 선택"
                             selectedMaterial={(() => {
                               const itemCode = form.watch(`items.${index}.itemCode`);
                               const itemName = form.watch(`items.${index}.itemName`);
@@ -565,9 +595,41 @@ export function CreateGeneralRfqDialog({ onSuccess }: CreateGeneralRfqDialogProp
                               form.setValue(`items.${index}.itemCode`, material?.materialGroupCode || '');
                               form.setValue(`items.${index}.itemName`, material?.materialGroupDescription || '');
                             }}
-                            placeholder="자재를 검색하세요..."
-                            title="자재 선택"
-                            description="원하는 자재를 검색하고 선택해주세요."
+                            placeholder="자재그룹을 검색하세요..."
+                            title="자재그룹 선택"
+                            description="원하는 자재그룹을 검색하고 선택해주세요."
+                            triggerVariant="outline"
+                          />
+                        </div>
+                      </div>
+
+                      {/* 자재코드 선택 - 그리드 외부 */}
+                      <div className="mb-3">
+                        <FormLabel className="text-xs">
+                          자재코드(자재명)
+                        </FormLabel>
+                        <div className="mt-1">
+                          <MaterialSelectorDialogSingle
+                            triggerLabel="자재코드 선택"
+                            selectedMaterial={(() => {
+                              const materialCode = form.watch(`items.${index}.materialCode`);
+                              const materialName = form.watch(`items.${index}.materialName`);
+                              if (materialCode && materialName) {
+                                return {
+                                  materialCode: materialCode,
+                                  materialName: materialName,
+                                  displayText: `${materialCode} - ${materialName}`
+                                } as SAPMaterialSearchItem;
+                              }
+                              return null;
+                            })()}
+                            onMaterialSelect={(material) => {
+                              form.setValue(`items.${index}.materialCode`, material?.materialCode || '');
+                              form.setValue(`items.${index}.materialName`, material?.materialName || '');
+                            }}
+                            placeholder="자재코드를 검색하세요..."
+                            title="자재코드 선택"
+                            description="원하는 자재코드를 검색하고 선택해주세요."
                             triggerVariant="outline"
                           />
                         </div>
-- 
cgit v1.2.3

