From bac0228d21b7195065e9cddcc327ae33659c7bcc Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Sun, 1 Jun 2025 13:52:21 +0000
Subject: (대표님) 20250601까지 작업사항
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 components/form-data/update-form-sheet.tsx | 102 +++++++++++++++++++++--------
 1 file changed, 74 insertions(+), 28 deletions(-)

(limited to 'components/form-data/update-form-sheet.tsx')

diff --git a/components/form-data/update-form-sheet.tsx b/components/form-data/update-form-sheet.tsx
index 6f2a4722..c8772e2a 100644
--- a/components/form-data/update-form-sheet.tsx
+++ b/components/form-data/update-form-sheet.tsx
@@ -54,6 +54,7 @@ interface UpdateTagSheetProps
   rowData: Record<string, any> | null;
   formCode: string;
   contractItemId: number;
+  editableFieldsMap?: Map<string, string[]>; // 새로 추가
   /** 업데이트 성공 시 호출될 콜백 */
   onUpdateSuccess?: (updatedValues: Record<string, any>) => void;
 }
@@ -65,12 +66,66 @@ export function UpdateTagSheet({
   rowData,
   formCode,
   contractItemId,
+  editableFieldsMap = new Map(), // 기본값 설정
   onUpdateSuccess,
   ...props
 }: UpdateTagSheetProps) {
   const [isPending, startTransition] = React.useTransition();
   const router = useRouter();
 
+  // 현재 TAG의 편집 가능한 필드 목록 가져오기
+  const editableFields = React.useMemo(() => {
+    if (!rowData?.TAG_NO || !editableFieldsMap.has(rowData.TAG_NO)) {
+      return [];
+    }
+    return editableFieldsMap.get(rowData.TAG_NO) || [];
+  }, [rowData?.TAG_NO, editableFieldsMap]);
+
+  // 필드가 편집 가능한지 판별하는 함수
+  const isFieldEditable = React.useCallback((column: DataTableColumnJSON) => {
+    // 1. SHI-only 필드는 편집 불가
+    if (column.shi === true) {
+      return false;
+    }
+
+    // 2. TAG_NO와 TAG_DESC는 기본적으로 편집 가능 (필요에 따라 수정 가능)
+    if (column.key === "TAG_NO" || column.key === "TAG_DESC") {
+      return true;
+    }
+
+    // 3. editableFieldsMap이 있으면 해당 리스트에 있는지 확인
+    if (rowData?.TAG_NO && editableFieldsMap.has(rowData.TAG_NO)) {
+      return editableFields.includes(column.key);
+    }
+
+    // 4. editableFieldsMap 정보가 없으면 기본적으로 편집 불가 (안전한 기본값)
+    return false;
+  }, [rowData?.TAG_NO, editableFieldsMap, editableFields]);
+
+  // 읽기 전용 필드인지 판별하는 함수 (편집 가능의 반대)
+  const isFieldReadOnly = React.useCallback((column: DataTableColumnJSON) => {
+    return !isFieldEditable(column);
+  }, [isFieldEditable]);
+
+  // 읽기 전용 사유를 반환하는 함수
+  const getReadOnlyReason = React.useCallback((column: DataTableColumnJSON) => {
+    if (column.shi === true) {
+      return "SHI-only field (managed by SHI system)";
+    }
+    
+    if (column.key !== "TAG_NO" && column.key !== "TAG_DESC") {
+      if (!rowData?.TAG_NO || !editableFieldsMap.has(rowData.TAG_NO)) {
+        return "No editable fields information for this TAG";
+      }
+      
+      if (!editableFields.includes(column.key)) {
+        return "Not editable for this TAG class";
+      }
+    }
+    
+    return "Read-only field";
+  }, [rowData?.TAG_NO, editableFieldsMap, editableFields]);
+
   // 1) zod 스키마
   const dynamicSchema = React.useMemo(() => {
     const shape: Record<string, z.ZodType<any>> = {};
@@ -118,7 +173,7 @@ export function UpdateTagSheet({
         // 제출 전에 읽기 전용 필드를 원본 값으로 복원
         const finalValues = { ...values };
         for (const col of columns) {
-          if (col.shi || col.key === "TAG_NO" || col.key === "TAG_DESC") {
+          if (isFieldReadOnly(col)) {
             // 읽기 전용 필드는 원본 값으로 복원
             finalValues[col.key] = rowData?.[col.key] ?? "";
           }
@@ -161,13 +216,22 @@ export function UpdateTagSheet({
     });
   }
 
+  // 편집 가능한 필드 개수 계산
+  const editableFieldCount = React.useMemo(() => {
+    return columns.filter(col => isFieldEditable(col)).length;
+  }, [columns, isFieldEditable]);
+
   return (
     <Sheet open={open} onOpenChange={onOpenChange} {...props}>
       <SheetContent className="sm:max-w-xl md:max-w-3xl lg:max-w-4xl xl:max-w-5xl flex flex-col">
         <SheetHeader className="text-left">
-          <SheetTitle>Update Row</SheetTitle>
+          <SheetTitle>Update Row - {rowData?.TAG_NO || 'Unknown TAG'}</SheetTitle>
           <SheetDescription>
             Modify the fields below and save changes. Fields with <LockIcon className="inline h-3 w-3" /> are read-only.
+            <br />
+            <span className="text-sm text-green-600">
+              {editableFieldCount} of {columns.length} fields are editable for this TAG.
+            </span>
           </SheetDescription>
         </SheetHeader>
 
@@ -179,10 +243,8 @@ export function UpdateTagSheet({
             <div className="overflow-y-auto max-h-[80vh] flex-1 pr-4 -mr-4">
               <div className="flex flex-col gap-4 pt-2">
                 {columns.map((col) => {
-                  // 읽기 전용 조건 업데이트: shi가 true이거나 TAG_NO/TAG_DESC인 경우
-                  const isReadOnly = col.shi === true || 
-                                    col.key === "TAG_NO" || 
-                                    col.key === "TAG_DESC";
+                  const isReadOnly = isFieldReadOnly(col);
+                  const readOnlyReason = isReadOnly ? getReadOnlyReason(col) : "";
                   
                   return (
                     <FormField
@@ -214,9 +276,9 @@ export function UpdateTagSheet({
                                     )}
                                   />
                                 </FormControl>
-                                {isReadOnly && col.shi && (
+                                {isReadOnly && (
                                   <FormDescription className="text-xs text-gray-500">
-                                    This field is read-only
+                                    {readOnlyReason}
                                   </FormDescription>
                                 )}
                                 <FormMessage />
@@ -278,31 +340,15 @@ export function UpdateTagSheet({
                                     </Command>
                                   </PopoverContent>
                                 </Popover>
-                                {isReadOnly && col.shi && (
+                                {isReadOnly && (
                                   <FormDescription className="text-xs text-gray-500">
-                                    This field is read-only
+                                    {readOnlyReason}
                                   </FormDescription>
                                 )}
                                 <FormMessage />
                               </FormItem>
                             );
 
-                          // case "date":
-                          //   return (
-                          //     <FormItem>
-                          //       <FormLabel>{col.label}</FormLabel>
-                          //       <FormControl>
-                          //         <Input
-                          //           type="date"
-                          //           readOnly={isReadOnly}
-                          //           onChange={field.onChange}
-                          //           value={field.value ?? ""}
-                          //         />
-                          //       </FormControl>
-                          //       <FormMessage />
-                          //     </FormItem>
-                          //   )
-
                           case "STRING":
                           default:
                             return (
@@ -322,9 +368,9 @@ export function UpdateTagSheet({
                                     )}
                                   />
                                 </FormControl>
-                                {isReadOnly && col.shi && (
+                                {isReadOnly && (
                                   <FormDescription className="text-xs text-gray-500">
-                                    This field is read-only
+                                    {readOnlyReason}
                                   </FormDescription>
                                 )}
                                 <FormMessage />
-- 
cgit v1.2.3

