import { formatValue } from '../../lib/format'; import { Badge } from '../reusable/Badge'; import { CheckCircle2Icon, Factory, UserIcon } from 'lucide-react'; export function OrderCard({ row, fields }: { row: Record; fields: any[] }) { const orderIdStr = row.order_id; // Extract store lookup object if it exists const storeObj = (row.select_store || row.store) as Record | null; const storeVal = formatValue(storeObj?.business_name || '—'); const routeVal = formatValue(storeObj?.route_name || '—'); const distributorVal = formatValue(storeObj?.distributor_name || '—'); // Extract user/performed_by object const userKey = Object.keys(row).find(k => k.includes('user_id') || k.includes('created_by')); const userObj = userKey ? row[userKey] as Record : null; const userVal = formatValue(userObj?.name || userObj?.user_name || '—'); const userEmail = userObj?.email ? String(userObj.email) : ''; const dateVal = row.date_of_order ? formatValue(row.date_of_order) : '—'; const stateName = String(row.current_state_name || ''); const isProductive = stateName.toLowerCase().includes('productive') || stateName.toLowerCase().includes('closed'); const productiveLabel = stateName || 'Pending'; // Order Details Grid const gridField = fields.find(f => f.data_type === 'grid' || f.field_key === 'order_details'); const gridValRaw = gridField ? row[gridField.field_key] : row.order_details; const gridVal = Array.isArray(gridValRaw) ? gridValRaw : []; return (
{/* Top Header Section */}
Order ID {orderIdStr}
{isProductive && } {productiveLabel}
Date {dateVal}
Store & Route {storeVal} ({routeVal})
{/* Middle Section: Distributor & User */}
Distributor {distributorVal}
Placed by {userVal}
{/* Bottom Section: Order Items */} {gridVal.length > 0 && (

Order Items {gridVal.length}

{gridVal.map((item, i) => { const productName = formatValue(item.product_name || item.product_category || 'Unknown Product'); const sku = formatValue(item.sku_code || item.sku || 'N/A'); const bags = formatValue(item.bags || item.total_bags || item.quantity || 0); return (
{sku}
{productName}
{bags} bags
); })}
)}
); }