import type { Dispatch, SetStateAction } from 'react'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '../ui/dialog'; import { Button } from '../ui/button'; import { Input } from '../ui/input'; import { Label } from '../ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select'; import { Plus, XCircle } from 'lucide-react'; import { formatCurrencyEgp, formatNameWithId } from '../../services/procurementUi'; export interface MedicineOrderFormItem { medicine: string; company: string; fishTypeIds: string[]; quantity: number; unitCost: number; } interface SupplierOption { id: string; name: string; } interface FishTypeOption { id: string; name: string; } interface CreateMedicineOrderDialogProps { open: boolean; submitting: boolean; supplierId: string; suppliers: SupplierOption[]; items: MedicineOrderFormItem[]; fishTypes: FishTypeOption[]; totalCost: number; onOpenChange: (open: boolean) => void; onSupplierChange: (value: string) => void; onItemsChange: Dispatch>; onAddItem: () => void; onCreate: () => void | Promise; } export function CreateMedicineOrderDialog({ open, submitting, supplierId, suppliers, items, fishTypes, totalCost, onOpenChange, onSupplierChange, onItemsChange, onAddItem, onCreate, }: CreateMedicineOrderDialogProps) { const updateItem = (index: number, patch: Partial) => { onItemsChange((previous) => previous.map((entry, entryIndex) => (entryIndex === index ? { ...entry, ...patch } : entry)), ); }; const removeItem = (index: number) => { onItemsChange((previous) => previous.length === 1 ? previous : previous.filter((_, entryIndex) => entryIndex !== index), ); }; const toggleFishType = (index: number, fishTypeId: string) => { onItemsChange((previous) => previous.map((entry, entryIndex) => { if (entryIndex !== index) { return entry; } const isSelected = entry.fishTypeIds.includes(fishTypeId); return { ...entry, fishTypeIds: isSelected ? entry.fishTypeIds.filter((entryFishTypeId) => entryFishTypeId !== fishTypeId) : [...entry.fishTypeIds, fishTypeId], }; }), ); }; return ( Create New Medicine Purchase Order Configure medicine line items with manufacturer, fish type applicability, and calculated totals.

Order Items

{items.map((item, index) => (

Line Item {index + 1}

updateItem(index, { medicine: event.target.value })} />
updateItem(index, { company: event.target.value })} />
updateItem(index, { quantity: Number(event.target.value) || 0 })} />
updateItem(index, { unitCost: Number(event.target.value) || 0 })} />
Line Total {formatCurrencyEgp(item.quantity * item.unitCost)}
{item.fishTypeIds.length} selected
{fishTypes.map((fishType) => ( ))}
))}
Grand Total: {formatCurrencyEgp(totalCost)}
); }