import { useMemo, useState } from 'react' import { Link } from 'react-router-dom' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' import { CheckCircle2, Plus, Truck, XCircle } from 'lucide-react' import { PageShell } from '@/layouts/PageShell' import { Card, CardContent } from '@/components/ui/card' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { ClearFiltersButton, FilterSelect, TableToolbar } from '@/components/table/TableToolbar' import { RowActions } from '@/components/table/RowActions' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Label } from '@/components/ui/label' import { SelectNative } from '@/components/ui/select-native' import { useAuthStore } from '@/zustand/authStore' import { branches, orders as seedOrders, riders } from '@/mocks/data' import { formatEtb } from '@/lib/utils' import type { Order } from '@/types' export function OrdersPage() { const { t } = useTranslation() const user = useAuthStore((s) => s.user) const [rows, setRows] = useState([...seedOrders]) const [q, setQ] = useState('') const [status, setStatus] = useState('all') const [fulfillment, setFulfillment] = useState('all') const [branchFilter, setBranchFilter] = useState('all') const [channel, setChannel] = useState('all') const [assignTarget, setAssignTarget] = useState(null) const [riderId, setRiderId] = useState('') const canPickBranch = user?.role !== 'pharmacist' const filtered = useMemo(() => { let list = rows if (user?.role === 'pharmacist' && user.branchId) { list = list.filter((o) => o.branchId === user.branchId) } else if (branchFilter !== 'all') { list = list.filter((o) => o.branchId === branchFilter) } if (status !== 'all') list = list.filter((o) => o.status === status) if (fulfillment !== 'all') list = list.filter((o) => o.fulfillment === fulfillment) if (channel !== 'all') list = list.filter((o) => o.channel === channel) if (q) { const needle = q.toLowerCase() list = list.filter( (o) => o.customerName.toLowerCase().includes(needle) || o.id.toLowerCase().includes(needle), ) } return list }, [rows, user, q, status, fulfillment, branchFilter, channel]) const filtersActive = status !== 'all' || fulfillment !== 'all' || branchFilter !== 'all' || channel !== 'all' || !!q const confirm = (orderId: string) => { setRows((prev) => prev.map((o) => (o.id === orderId ? { ...o, status: 'confirmed' } : o))) const target = seedOrders.find((o) => o.id === orderId) if (target) target.status = 'confirmed' toast.success('Order confirmed') } const cancel = (orderId: string) => { setRows((prev) => prev.map((o) => (o.id === orderId ? { ...o, status: 'cancelled' } : o))) toast.message('Order cancelled') } const saveAssign = () => { if (!assignTarget || !riderId) return setRows((prev) => prev.map((o) => o.id === assignTarget.id ? { ...o, assignedRiderId: riderId, status: 'out_for_delivery' } : o, ), ) const target = seedOrders.find((o) => o.id === assignTarget.id) if (target) { target.assignedRiderId = riderId target.status = 'out_for_delivery' } toast.success('Delivery person assigned') setAssignTarget(null) setRiderId('') } return ( {canPickBranch ? ( ({ value: b.id, label: b.name })), ]} /> ) : null} { setQ('') setStatus('all') setFulfillment('all') setBranchFilter('all') setChannel('all') }} /> } actions={ } /> ID Customer {t('common.branch')} Fulfillment {t('common.status')} Total {t('common.actions')} {filtered.length === 0 ? ( {t('common.noResults')} ) : ( filtered.map((o) => ( {o.id} {o.customerName} {branches.find((b) => b.id === o.branchId)?.name} {o.fulfillment} {o.status} {formatEtb(o.totalEtb)} , onClick: () => confirm(o.id), disabled: o.status === 'confirmed' || o.status === 'cancelled', }, ]} items={[ ...(o.fulfillment === 'delivery' ? [ { label: t('orders.assignRider'), icon: , onClick: () => { setAssignTarget(o) setRiderId(o.assignedRiderId ?? '') }, }, ] : []), { label: 'Cancel', icon: , variant: 'destructive' as const, onClick: () => cancel(o.id), disabled: o.status === 'cancelled', }, ]} /> )) )}
!open && setAssignTarget(null)}> {t('orders.assignRider')} {assignTarget?.id} · {assignTarget?.customerName}
setRiderId(e.target.value)}> {riders.map((r) => ( ))}
) }