'use client'; import type { ShipRequestDetailItem, AllocRequestDetailItem, } from '@/types/requests'; import { Table, TableBody, TableCell, TableRow, } from '@/components/ui/table'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Plus, Trash2, ShoppingCart } from 'lucide-react'; import { useState } from 'react'; type ShipItemRow = ShipRequestDetailItem & { _type: 'ship' }; type AllocItemRow = AllocRequestDetailItem & { _type: 'alloc' }; type Props = { items: (ShipRequestDetailItem | AllocRequestDetailItem)[]; variant: 'ship' | 'alloc'; onRemoveItem: (detailId: string) => Promise; onUpdateItem: (detailId: string, data: { quantity?: number; notes?: string | null }) => Promise; onOpenBrowser: () => void; readOnly?: boolean; }; export function CartItemsTable({ items, variant, onRemoveItem, onUpdateItem, onOpenBrowser, readOnly = false, }: Props) { const [removingId, setRemovingId] = useState(null); const [editingId, setEditingId] = useState(null); const [editQty, setEditQty] = useState(''); const [editNotes, setEditNotes] = useState(''); const handleRemove = async (id: string) => { setRemovingId(id); try { await onRemoveItem(id); } finally { setRemovingId(null); } }; const startEdit = (item: ShipRequestDetailItem | AllocRequestDetailItem) => { setEditingId(item.id); setEditQty(String(item.quantity)); setEditNotes(item.notes || ''); }; const saveEdit = async (id: string) => { await onUpdateItem(id, { quantity: parseFloat(editQty) || 0, notes: editNotes || null, }); setEditingId(null); }; const cancelEdit = () => { setEditingId(null); }; const isShipItem = ( item: ShipRequestDetailItem | AllocRequestDetailItem ): item is ShipRequestDetailItem => { return 'plant' in item; }; return (
Cart Items ({items.length}) {!readOnly && ( )}
{items.length === 0 ? (

No items in cart

{!readOnly && ( )}
) : (
{variant === 'ship' ? ( <> ) : ( )} {!readOnly && ( )} {items.map((item, i) => { const isEditing = editingId === item.id; return ( {item.part_num} {item.lot_num || '-'} {variant === 'ship' && isShipItem(item) ? ( <> {item.plant} {item.warehouse || '-'} ) : !isShipItem(item) ? ( {item.coil_number || '-'} ) : null} {isEditing ? ( setEditQty(e.target.value)} className="h-8 w-24 text-right" min={0} step="any" /> ) : ( !readOnly && startEdit(item)} > {item.quantity} )} {isEditing ? ( setEditNotes(e.target.value)} className="h-8" placeholder="Notes..." /> ) : ( !readOnly && startEdit(item)} > {item.notes || '-'} )} {!readOnly && ( {isEditing ? (
) : ( )}
)}
); })}
Part # Lot # Plant Warehouse Coil # Qty Notes Actions
)}
); }