"use client";
import {
Archive,
ArchiveRestore,
ArrowLeft,
ClipboardCheck,
EllipsisVertical,
Minus,
Pencil,
ShoppingBag,
Trash,
Trash2,
} from "lucide-react";
import Link from "next/link";
import { useState } from "react";
import { toast } from "sonner";
import { PageHeader } from "@/components/page-header";
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { tr } from "@/i18n/tr";
import { cn } from "@/lib/utils";
import { formatStock, roundForDisplay } from "@/shared/stock";
import { deleteStockMovementAction, setProductTypeArchivedAction } from "../actions";
import type { ProductTypeDetail, StockMovementRow, StockOption } from "../service";
import {
ProductTypeFormDialog,
StockCountDialog,
StockMovementDialog,
type MovementKindInput,
} from "./stock-dialogs";
import {
ExpiryBadge,
LevelBadge,
QuickConsumeButton,
StockBar,
suggestionReasons,
ymdLabel,
} from "./stock-view";
const t = tr.stock;
type Pending =
| { kind: "edit" }
| { kind: "movement"; movement: MovementKindInput }
| { kind: "count" }
| { kind: "archive" }
| { kind: "deleteMovement"; row: StockMovementRow }
| null;
function Stat({ label, children }: { label: string; children: React.ReactNode }) {
return (
);
}
/** /stok/:id — mevcut durum, partiler (SKT) ve hareketler. */
export function ProductDetailView({
data,
categories,
}: {
data: ProductTypeDetail;
categories: StockOption[];
}) {
const [pending, setPending] = useState(null);
const close = () => setPending(null);
const q = (x: number) => formatStock(x, data.unit);
const active = data.canWrite && !data.archived;
return (
setPending({ kind: "edit" })}
>
{tr.common.edit}
{data.archived ? (
{
const r = await setProductTypeArchivedAction(data.id, false);
if (r.ok) toast.success(t.toast.unarchived);
else toast.error(r.error);
}}
>
{t.detail.unarchive}
) : (
setPending({ kind: "archive" })}
>
{t.detail.archive}
)}
) : null
}
/>
{data.archived ? {t.detail.archived} : null}
{q(Math.max(0, data.current))}
{q(data.targetQuantity)}
{q(data.level.thresholdQuantity)}{" "}
(%{data.thresholdPct})
{q(data.suggestedQuantity)}
{suggestionReasons(data).length ? (
{suggestionReasons(data).join(" ")}
) : null}
{data.suggestion.usage ? (
t.detail.perWeek(q(roundForDisplay(data.suggestion.usage.perDay * 7)))
) : (
{t.detail.noUsage}
)}
{data.count ? (
{t.detail.countEvery(data.count.everyDays)}
{data.count.due
? tr.stock.count.dueBadge(data.count.overdueDays)
: t.detail.nextCount(ymdLabel(data.count.nextDate))}
{data.count.lastDate
? ` · ${tr.stock.count.lastCount(ymdLabel(data.count.lastDate))}`
: ""}
) : (
{t.detail.noCountSchedule}
)}
{data.location ? (
{t.detail.location}: {data.location}
) : null}
{data.note ? {data.note}
: null}
{active ? (
) : null}
{t.detail.lots}
{t.detail.lotsHint}
{data.lots.length === 0 ? (
{t.detail.noLots}
) : (
{data.lots.map((lot, i) => (
-
{q(lot.remaining)}
· {ymdLabel(lot.date)}
{lot.expiryDate && lot.status && lot.status !== "ok" ? (
) : (
{lot.expiryDate ? t.expiring.on(ymdLabel(lot.expiryDate)) : t.detail.noExpiry}
)}
))}
)}
{t.detail.movements}
{data.movements.length === 0 ? (
{t.detail.noMovements}
) : (
{data.movements.map((row) => (
-
{row.initial ? t.initial : t.kinds[row.kind]}
0 ? "text-income" : "text-expense")}
>
{row.delta > 0 ? "+" : "−"}
{q(Math.abs(row.delta))}
{ymdLabel(row.date)}
{row.wasteReason ? ` · ${t.wasteReasons[row.wasteReason]}` : ""}
{row.expiryDate ? ` · ${t.expiring.on(ymdLabel(row.expiryDate))}` : ""}
{row.note ? ` · ${row.note}` : ""}
{t.detail.after(q(row.quantityAfter))}
{data.canWrite ? (
) : null}
))}
)}
{pending?.kind === "edit" ? (
) : null}
{pending?.kind === "movement" ? (
) : null}
{pending?.kind === "count" ? : null}
!o && close()}
>
{pending?.kind === "archive" ? t.detail.archiveTitle : t.detail.deleteMovementTitle}
{pending?.kind === "archive"
? t.detail.archiveDescription(data.name)
: t.detail.deleteMovementDescription}
{tr.common.cancel}
);
}