"use client"; import { ArrowLeft, Plus, Trash2 } from "lucide-react"; import Link from "next/link"; import { useState } from "react"; import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts"; 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, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { ChartContainer, ChartLegend, ChartLegendContent, ChartTooltip, ChartTooltipContent, type ChartConfig, } from "@/components/ui/chart"; import { AttachmentStrip } from "@/features/attachments/components/attachment-strip"; import { tr } from "@/i18n/tr"; import { formatMoney } from "@/shared/format"; import { formatUnitPrice } from "@/shared/prices"; import { deleteProductPriceAction } from "../actions"; import { priceProductOption } from "../options"; import type { PriceRow, ProductPriceDetail } from "../service"; import { OffersList, StoreDot, ymdLabel } from "./offers-list"; import { PriceFormDialog } from "./price-dialog"; const t = tr.productPrices; const shortDate = (ymd: string) => new Intl.DateTimeFormat("tr-TR", { day: "numeric", month: "short" }).format( new Date(`${ymd}T12:00:00+03:00`), ); const compactTl = (minor: number) => new Intl.NumberFormat("tr-TR", { maximumFractionDigits: 0 }).format(minor / 100) + " ₺"; /** Mağaza başına paket fiyatı; fiyat yeni kayda kadar geçerli (basamaklı çizgi). */ function PriceChart({ data }: { data: ProductPriceDetail }) { const config = Object.fromEntries( data.series.map((s) => [`s${s.id}`, { label: s.label, color: `var(--series-${s.slot})` }]), ) satisfies ChartConfig; return ( shortDate(v)} /> compactTl(v)} /> payload?.[0] ? ymdLabel(String(payload[0].payload.date)) : "" } formatter={(value, name) => (
`s${s.id}` === name)?.slot ?? 1} /> {config[String(name)]?.label} {formatMoney(Number(value))}
)} /> } /> } /> {data.series.map((s) => ( ))}
); } /** /stok/urunler/:id — ürün bilgisi, mağazalardaki fiyatlar, fiyat geçmişi ve kayıtlar. */ export function ProductPriceView({ data }: { data: ProductPriceDetail }) { const [adding, setAdding] = useState(false); const [deleting, setDeleting] = useState(null); const p = data.product; const canAdd = data.canWrite && !p.archived && data.stores.length > 0; return (
{t.detail.typeLink(p.productTypeName)} {" · "} {p.packageLabel} {p.barcode ? ` · ${tr.products.barcode(p.barcode)}` : ""} } actions={ canAdd ? ( ) : null } />
{p.archived ? {tr.products.archived} : null} {p.conversionMissing ? ( {tr.products.conversionMissing} ) : null}
{t.detail.currentTitle} {t.detail.currentDescription} {data.offers.length === 0 ? (

{t.noPrices}

) : ( )}
{data.history.length > 1 ? ( {t.detail.chartTitle} {t.detail.chartDescription} ) : null} {data.history.length ? (

{t.detail.historyTitle}

    {data.history.map((r) => (
  • {r.store.label}

    {ymdLabel(r.date)} {r.isPromo ? ` · ${r.promoEnded ? t.promoEnded : t.promo}${ r.promoUntil ? ` (${t.promoUntil(ymdLabel(r.promoUntil))})` : "" }` : ""} {` · ${t.sources[r.source.split(":")[0]!] ?? r.source}`} {r.note ? ` · ${r.note}` : ""}

    {formatMoney(r.priceMinor)}

    {r.unitPriceMinor !== null ? (

    {formatUnitPrice(r.unitPriceMinor, p.typeUnit)}

    ) : null}
    {data.canWrite ? ( ) : null}
  • ))}
) : null} {adding ? ( setAdding(false)} /> ) : null} !o && setDeleting(null)}> {t.detail.deleteTitle} {t.detail.deleteDescription} {tr.common.cancel}
); }