From d0d2eaa2de58a0c33e9a21604b126961403cd69e Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Wed, 14 May 2025 06:12:13 +0000
Subject: (최겸) 기술영업 조선, 해양Top, 해양 Hull 아이템 리스트 개발(CRUD,
 excel import/export/template)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/items-tech/table/update-items-sheet.tsx | 390 ++++++++++++++++++++++++++++
 1 file changed, 390 insertions(+)
 create mode 100644 lib/items-tech/table/update-items-sheet.tsx

(limited to 'lib/items-tech/table/update-items-sheet.tsx')

diff --git a/lib/items-tech/table/update-items-sheet.tsx b/lib/items-tech/table/update-items-sheet.tsx
new file mode 100644
index 00000000..2db3b193
--- /dev/null
+++ b/lib/items-tech/table/update-items-sheet.tsx
@@ -0,0 +1,390 @@
+"use client"
+
+import * as React from "react"
+import { useForm } from "react-hook-form"
+
+import { Button } from "@/components/ui/button"
+import {
+  Sheet,
+  SheetClose,
+  SheetContent,
+  SheetDescription,
+  SheetFooter,
+  SheetHeader,
+  SheetTitle,
+} from "@/components/ui/sheet"
+import {
+  Form,
+  FormControl,
+  FormField,
+  FormItem,
+  FormLabel,
+  FormMessage,
+} from "@/components/ui/form"
+import { Input } from "@/components/ui/input"
+import {
+  Select,
+  SelectContent,
+  SelectItem,
+  SelectTrigger,
+  SelectValue,
+} from "@/components/ui/select"
+import { Textarea } from "@/components/ui/textarea"
+import { toast } from "sonner"
+
+import { 
+  modifyShipbuildingItem,
+  modifyOffshoreTopItem,
+  modifyOffshoreHullItem
+} from "../service"
+import { ItemType } from "./delete-items-dialog"
+
+const shipbuildingWorkTypes = [
+  { value: "기장", label: "기장" },
+  { value: "전장", label: "전장" },
+  { value: "선실", label: "선실" },
+  { value: "배관", label: "배관" },
+  { value: "철의", label: "철의" },
+] as const
+
+const offshoreTopWorkTypes = [
+  { value: "TM", label: "TM" },
+  { value: "TS", label: "TS" },
+  { value: "TE", label: "TE" },
+  { value: "TP", label: "TP" },
+] as const
+
+const offshoreHullWorkTypes = [
+  { value: "HA", label: "HA" },
+  { value: "HE", label: "HE" },
+  { value: "HH", label: "HH" },
+  { value: "HM", label: "HM" },
+  { value: "NC", label: "NC" },
+] as const
+
+interface CommonItemFields {
+  id: number
+  itemId: number
+  itemCode: string
+  itemName: string
+  description: string | null
+  createdAt: Date
+  updatedAt: Date
+}
+
+type ShipbuildingItem = CommonItemFields & {
+  workType: "기장" | "전장" | "선실" | "배관" | "철의"
+  shipTypes: string
+}
+
+type OffshoreTopItem = CommonItemFields & {
+  workType: "TM" | "TS" | "TE" | "TP"
+  itemList1: string | null
+  itemList2: string | null
+  itemList3: string | null
+  itemList4: string | null
+}
+
+type OffshoreHullItem = CommonItemFields & {
+  workType: "HA" | "HE" | "HH" | "HM" | "NC"
+  itemList1: string | null
+  itemList2: string | null
+  itemList3: string | null
+  itemList4: string | null
+}
+
+type UpdateItemSchema = {
+  itemCode?: string
+  itemName?: string
+  description?: string
+  workType?: string
+  shipTypes?: string
+  itemList1?: string
+  itemList2?: string
+  itemList3?: string
+  itemList4?: string
+}
+
+interface UpdateItemSheetProps {
+  item: ShipbuildingItem | OffshoreTopItem | OffshoreHullItem
+  itemType: ItemType
+  open: boolean
+  onOpenChange: (open: boolean) => void
+}
+
+export function UpdateItemSheet({ item, itemType, open, onOpenChange }: UpdateItemSheetProps) {
+  const [isSubmitting, setIsSubmitting] = React.useState(false)
+  
+  // 초기값 설정
+  const form = useForm<UpdateItemSchema>({
+    defaultValues: {
+      itemCode: item.itemCode,
+      itemName: item.itemName,
+      description: item.description || "",
+      workType: item.workType,
+      ...getItemTypeSpecificDefaults(item, itemType),
+    },
+  })
+
+  function getItemTypeSpecificDefaults(
+    item: ShipbuildingItem | OffshoreTopItem | OffshoreHullItem, 
+    itemType: ItemType
+  ) {
+    switch (itemType) {
+      case 'shipbuilding':
+        return {
+          shipTypes: (item as ShipbuildingItem).shipTypes
+        };
+      case 'offshoreTop':
+      case 'offshoreHull':
+        const offshoreItem = item as OffshoreTopItem | OffshoreHullItem;
+        return {
+          itemList1: offshoreItem.itemList1 || "",
+          itemList2: offshoreItem.itemList2 || "",
+          itemList3: offshoreItem.itemList3 || "",
+          itemList4: offshoreItem.itemList4 || "",
+        };
+      default:
+        return {};
+    }
+  }
+
+  async function onSubmit(data: UpdateItemSchema) {
+    try {
+      setIsSubmitting(true)
+      let result;
+      
+      switch (itemType) {
+        case 'shipbuilding':
+          result = await modifyShipbuildingItem({ 
+            ...data, 
+            id: item.id 
+          });
+          break;
+        case 'offshoreTop':
+          result = await modifyOffshoreTopItem({ 
+            ...data, 
+            id: item.id 
+          });
+          break;
+        case 'offshoreHull':
+          result = await modifyOffshoreHullItem({ 
+            ...data, 
+            id: item.id 
+          });
+          break;
+        default:
+          toast.error("지원하지 않는 아이템 타입입니다");
+          return;
+      }
+      
+      if (result.success) {
+        toast.success(result.message || "아이템이 수정되었습니다.")
+        onOpenChange(false)
+      } else {
+        toast.error(result.error || "아이템 수정 중 오류가 발생했습니다. 다시 시도해주세요.")
+      }
+    } catch (error) {
+      toast.error("오류가 발생했습니다.")
+      console.error(error)
+    } finally {
+      setIsSubmitting(false)
+    }
+  }
+
+  const getItemTypeLabel = () => {
+    switch (itemType) {
+      case 'shipbuilding':
+        return '조선 아이템';
+      case 'offshoreTop':
+        return '해양 TOP 아이템';
+      case 'offshoreHull':
+        return '해양 HULL 아이템';
+      default:
+        return '아이템';
+    }
+  }
+
+  const getWorkTypeOptions = () => {
+    switch (itemType) {
+      case 'shipbuilding':
+        return shipbuildingWorkTypes;
+      case 'offshoreTop':
+        return offshoreTopWorkTypes;
+      case 'offshoreHull':
+        return offshoreHullWorkTypes;
+      default:
+        return [];
+    }
+  }
+
+  return (
+    <Sheet open={open} onOpenChange={onOpenChange}>
+      <SheetContent>
+        <SheetHeader>
+          <SheetTitle>{getItemTypeLabel()} 수정</SheetTitle>
+          <SheetDescription>
+            {getItemTypeLabel()} 정보를 수정합니다. 수정할 필드를 입력해주세요.
+          </SheetDescription>
+        </SheetHeader>
+        <Form {...form}>
+          <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
+            <FormField
+              control={form.control}
+              name="itemCode"
+              render={({ field }) => (
+                <FormItem>
+                  <FormLabel>Material Group</FormLabel>
+                  <FormControl>
+                    <Input placeholder="Material Group을 입력하세요" {...field} />
+                  </FormControl>
+                  <FormMessage />
+                </FormItem>
+              )}
+            />
+            <FormField
+              control={form.control}
+              name="itemName"
+              render={({ field }) => (
+                <FormItem>
+                  <FormLabel>Description</FormLabel>
+                  <FormControl>
+                    <Input placeholder="Description을 입력하세요" {...field} />
+                  </FormControl>
+                  <FormMessage />
+                </FormItem>
+              )}
+            />
+            <FormField
+              control={form.control}
+              name="workType"
+              render={({ field }) => (
+                <FormItem>
+                  <FormLabel>기능(공종)</FormLabel>
+                  <Select
+                    onValueChange={field.onChange}
+                    defaultValue={field.value}
+                  >
+                    <FormControl>
+                      <SelectTrigger>
+                        <SelectValue placeholder="기능(공종)을 선택하세요" />
+                      </SelectTrigger>
+                    </FormControl>
+                    <SelectContent>
+                      {getWorkTypeOptions().map((type) => (
+                        <SelectItem key={type.value} value={type.value}>
+                          {type.label}
+                        </SelectItem>
+                      ))}
+                    </SelectContent>
+                  </Select>
+                  <FormMessage />
+                </FormItem>
+              )}
+            />
+
+            {/* 조선 아이템 전용 필드 */}
+            {itemType === 'shipbuilding' && (
+              <FormField
+                control={form.control}
+                name="shipTypes"
+                render={({ field }) => (
+                  <FormItem>
+                    <FormLabel>선종</FormLabel>
+                    <FormControl>
+                      <Input placeholder="선종을 입력하세요" {...field} />
+                    </FormControl>
+                    <FormMessage />
+                  </FormItem>
+                )}
+              />
+            )}
+
+            {/* 해양 TOP 또는 HULL 아이템 전용 필드 */}
+            {(itemType === 'offshoreTop' || itemType === 'offshoreHull') && (
+              <>
+                <FormField
+                  control={form.control}
+                  name="itemList1"
+                  render={({ field }) => (
+                    <FormItem>
+                      <FormLabel>Item List 1</FormLabel>
+                      <FormControl>
+                        <Input placeholder="Item List 1을 입력하세요" {...field} />
+                      </FormControl>
+                      <FormMessage />
+                    </FormItem>
+                  )}
+                />
+                <FormField
+                  control={form.control}
+                  name="itemList2"
+                  render={({ field }) => (
+                    <FormItem>
+                      <FormLabel>Item List 2</FormLabel>
+                      <FormControl>
+                        <Input placeholder="Item List 2를 입력하세요" {...field} />
+                      </FormControl>
+                      <FormMessage />
+                    </FormItem>
+                  )}
+                />
+                <FormField
+                  control={form.control}
+                  name="itemList3"
+                  render={({ field }) => (
+                    <FormItem>
+                      <FormLabel>Item List 3</FormLabel>
+                      <FormControl>
+                        <Input placeholder="Item List 3을 입력하세요" {...field} />
+                      </FormControl>
+                      <FormMessage />
+                    </FormItem>
+                  )}
+                />
+                <FormField
+                  control={form.control}
+                  name="itemList4"
+                  render={({ field }) => (
+                    <FormItem>
+                      <FormLabel>Item List 4</FormLabel>
+                      <FormControl>
+                        <Input placeholder="Item List 4를 입력하세요" {...field} />
+                      </FormControl>
+                      <FormMessage />
+                    </FormItem>
+                  )}
+                />
+              </>
+            )}
+
+            <FormField
+              control={form.control}
+              name="description"
+              render={({ field }) => (
+                <FormItem>
+                  <FormLabel>Size/Dimension</FormLabel>
+                  <FormControl>
+                    <Textarea
+                      placeholder="Size/Dimension을 입력하세요"
+                      {...field}
+                    />
+                  </FormControl>
+                  <FormMessage />
+                </FormItem>
+              )}
+            />
+            <SheetFooter>
+              <SheetClose asChild>
+                <Button variant="outline">취소</Button>
+              </SheetClose>
+              <Button type="submit" disabled={isSubmitting}>
+                {isSubmitting ? "수정 중..." : "수정"}
+              </Button>
+            </SheetFooter>
+          </form>
+        </Form>
+      </SheetContent>
+    </Sheet>
+  )
+} 
\ No newline at end of file
-- 
cgit v1.2.3

