"use client"; import { useEffect, useRef, useState } from "react"; import * as XLSX from "xlsx"; import { Check, ChevronsUpDown, Download, Loader2, Plus, Trash2, Upload } from "lucide-react"; import { Skeleton } from "@/app/components/ui/skeleton"; import { Button } from "@/app/components/ui/button"; import { DatePicker } from "@/app/components/ui/date-picker"; import { Input } from "@/app/components/ui/input"; import { Label } from "@/app/components/ui/label"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/app/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@/app/components/ui/popover"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/app/components/ui/select"; import { postStockIn, getProductVariations, getSkuCatalog } from "@/app/lib/services/adminStock"; import { listAdminWarehouses } from "@/app/lib/services/adminWarehouses"; import { listProducts } from "@/app/lib/services/adminProduct"; import { productPickerLabel } from "@/app/lib/productPickerLabel"; import { buildStockInWorkbook, parseStockInWorkbook } from "@/app/lib/inventory/stockInWorkbook"; import { readSheetRows } from "@/app/lib/inventory/workbookRows"; import type { AdminWarehouse, ProductListRow, ProductVariationOption, StockInPayload } from "@/app/lib/types"; import { cn } from "@/app/lib/utils"; import { parseQuantityInput } from "@/app/lib/inventory/quantityInput"; import { useT } from "@/app/lib/i18n/MessagesProvider"; // Stock-in form (openspec/changes/add-inventory-stock-in, I2 Task 5; enhanced I2.1 Task 8A). // - Destination: a {activeWarehouses.map(w => ( {w.code} — {w.name} ))} )} {/* Download template + Import actions — in-browser only, no file upload */}
{/* Hidden file input — triggered by the Import button */}
{/* Line editor */}
{lines.map((line, idx) => ( patchLine(line.key, { pickerOpen: open })} onProductPick={product => handleProductPick(line.key, product)} onVariationChange={(skuVariationId, variationId) => patchLine(line.key, { skuVariationId, variationId }) } showQuantityError={quantityAttempted && line.quantity < 1} onQuantityChange={text => patchLine(line.key, parseQuantityInput(text))} onBatchNumberChange={v => patchLine(line.key, { batchNumber: v })} onExpiryDateChange={v => patchLine(line.key, { expiryDate: v })} onRemove={ lines.length > 1 ? () => setLines(prev => prev.filter(l => l.key !== line.key)) : undefined } /> ))}
{/* Feedback */} {importErrors.length > 0 && (

{t("StockInImportErrors")}

)} {error && (
{t(error)}
)} {successDocNo && (
{t("StockInSubmitted", { docNo: t(successDocNo) })}
)} ); } // ─── StockInLineEditor ─────────────────────────────────────────────────────── interface StockInLineEditorProps { line: StockInLine; lineNumber: number; products: ProductListRow[]; onPickerOpenChange: (open: boolean) => void; onProductPick: (product: ProductListRow) => void; onVariationChange: (skuVariationId: string, variationId: string) => void; showQuantityError: boolean; onQuantityChange: (text: string) => void; onBatchNumberChange: (v: string) => void; onExpiryDateChange: (v: string) => void; onRemove?: () => void; } function StockInLineEditor({ line, lineNumber, products, onPickerOpenChange, onProductPick, onVariationChange, showQuantityError, onQuantityChange, onBatchNumberChange, onExpiryDateChange, onRemove, }: StockInLineEditorProps) { const variationSelectId = `variation-${line.key}`; const t = useT(); const qtyId = `qty-${line.key}`; const qtyErrorId = `qty-error-${line.key}`; const batchId = `batch-${line.key}`; const expiryId = `expiry-${line.key}`; return (
{/* Line header */}
{t("StockLine", { number: lineNumber })} {onRemove && ( )}
{/* Product picker — Popover + Command pattern (mirrors MemberCombobox in reports/page.tsx) */}
{t("StockNoProductFound")} {products.map(product => ( onProductPick(product)} > {product.productName}{" "} {product.productCode} {line.productId === product.id && ( )} ))}
{/* Variation select — shown after a product is picked */} {line.productId && (
{line.variationsLoading ? (
{t("StockLoadingVariations")}
) : ( )}
)} {/* Quantity */}
onQuantityChange(e.target.value)} aria-label={t("Quantity")} aria-invalid={showQuantityError || undefined} aria-describedby={showQuantityError ? qtyErrorId : undefined} className="w-[120px]" /> {showQuantityError && (

{t("StockQuantityRequired")}

)}
{/* Batch number (optional) */}
onBatchNumberChange(e.target.value)} placeholder={t("StockBatchPlaceholder")} className="w-[240px]" />
{/* Expiry date (optional) — sent as ISO yyyy-MM-dd string */}
); }