'use client'; import { useRef, useState } from 'react'; import { Camera, FileText, Loader2, Check } from 'lucide-react'; import { OrderItem, PurchaseOrder } from '@/lib/types'; import { buildPoFileUrl, countInvoicePages, invoicePhotoTarget } from '@/lib/poDocs'; import InvoiceCheck from '@/components/InvoiceCheck'; import { cn } from '@/lib/utils'; interface PoActionsProps { purchaseOrder: PurchaseOrder | null; items: OrderItem[]; /** Uploads one invoice page for this PO. Rejects on failure. */ onAddInvoicePage: (items: OrderItem[], file: File) => Promise; /** False hides the photo button (e.g. the caller can't upload). */ canPhoto?: boolean; /** 'lg' = full-width stacked buttons, for the PO bottom sheet. */ size?: 'sm' | 'lg'; className?: string; } // The two PO-level buttons on a PO header: open the PO PDF in one tap, // and photograph the supplier invoice once for the whole PO (with // "Page 2", "Page 3"… for multi-page invoices). export default function PoActions({ purchaseOrder, items, onAddInvoicePage, canPhoto: photoAllowed = true, size = 'sm', className, }: PoActionsProps) { const inputRef = useRef(null); const [uploading, setUploading] = useState(false); const pdfUrl = buildPoFileUrl(purchaseOrder); // A customer PO's photo goes on one of its lines; a stock order's on // the stock purchase. No target (no PO at all) means no photo button. const canPhoto = photoAllowed && invoicePhotoTarget(items) !== null; const pages = countInvoicePages(items); if (!pdfUrl && !canPhoto) return null; const handleFile = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; // Reset so taking the same shot again still fires onChange. e.target.value = ''; if (!file || uploading) return; setUploading(true); try { await onAddInvoicePage(items, file); } catch { // The page handler already told the driver; nothing to add here. } finally { setUploading(false); } }; const poLabel = purchaseOrder?.po_number ? `PO ${purchaseOrder.po_number}` : 'this PO'; const lg = size === 'lg'; const btn = lg ? 'h-12 w-full rounded-xl text-[15px] font-semibold flex items-center justify-center gap-2 transition-colors touch-manipulation' : 'h-9 px-3 rounded-lg text-[13px] font-semibold flex items-center gap-1.5 transition-colors touch-manipulation'; const icon = lg ? 18 : 14; return (
{pdfUrl && ( )} {canPhoto && ( <> )} {/* Beta helper: renders nothing unless switched on in Settings. */} {purchaseOrder?.po_number && ( )}
); }