"use client"; import { useState } from "react"; import { toast } from "sonner"; import { ResponsiveDialog } from "@/components/responsive-dialog"; import { Button } from "@/components/ui/button"; import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { tr } from "@/i18n/tr"; import { todayIstanbul } from "@/shared/dates"; import { formatStock, quantityInput, STOCK_UNITS, WASTE_REASONS, type WasteReason, } from "@/shared/stock"; import { addStockMovementAction, countStockAction, saveProductTypeAction } from "../actions"; import type { ProductTypeView, StockOption } from "../service"; const t = tr.stock; type Errors = Record; const CUSTOM = "__custom__"; function Footer({ busy, formId, onClose }: { busy: boolean; formId: string; onClose: () => void }) { return ( <> ); } function errorsOf(r: { fieldErrors?: Partial>; error: string }): Errors { return { ...r.fieldErrors, root: r.fieldErrors ? undefined : r.error }; } /** Yeni ürün tipi / düzenle. */ export function ProductTypeFormDialog({ item, categories, defaultCategoryId, onClose, onCreated, }: { item: ProductTypeView | null; categories: StockOption[]; defaultCategoryId?: string; onClose: () => void; onCreated?: (id: string) => void; }) { const standard = (STOCK_UNITS as readonly string[]).includes(item?.unit ?? "adet"); const [v, setV] = useState({ name: item?.name ?? "", categoryId: item?.categoryId ?? defaultCategoryId ?? "", unitChoice: standard ? (item?.unit ?? "adet") : CUSTOM, customUnit: standard ? "" : (item?.unit ?? ""), targetQuantity: item ? quantityInput(item.targetQuantity) : "", thresholdPct: String(item?.thresholdPct ?? 20), location: item?.location ?? "", step: item ? quantityInput(item.step) : "1", countEveryDays: item?.countEveryDays ? String(item.countEveryDays) : "", note: item?.note ?? "", initialQuantity: "", }); const [errors, setErrors] = useState({}); const [busy, setBusy] = useState(false); const set = (k: K, x: (typeof v)[K]) => setV((s) => ({ ...s, [k]: x })); const unit = v.unitChoice === CUSTOM ? v.customUnit.trim() : v.unitChoice; async function submit(e: React.FormEvent) { e.preventDefault(); setBusy(true); const r = await saveProductTypeAction(item?.id ?? null, { name: v.name, categoryId: v.categoryId, unit, targetQuantity: v.targetQuantity, thresholdPct: v.thresholdPct, location: v.location, step: v.step, countEveryDays: v.countEveryDays, note: v.note, initialQuantity: item ? "" : v.initialQuantity, }); setBusy(false); if (r.ok) { toast.success(t.toast.saved); onClose(); if (!item) onCreated?.((r.data as { id: string }).id); } else setErrors(errorsOf(r)); } const f = t.fields; const unitSuffix = unit ? ` (${unit})` : ""; return ( !o && onClose()} title={item ? t.editTitle : t.createTitle} footer={