'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 { 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; 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, className, }: PoActionsProps) { const inputRef = useRef(null); const [uploading, setUploading] = useState(false); const pdfUrl = buildPoFileUrl(purchaseOrder); // Stock pickups have nowhere to store a photo, so a PO made only of // pickups gets 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'; return (
{pdfUrl && ( )} {canPhoto && ( <> )}
); }