import { useState, useCallback } from 'react' import { useQuery } from '@tanstack/react-query' import { queryOptions } from '@tanstack/react-query' import { api } from '@/lib/api-client' import { usePOSStore } from '@/stores/pos.store' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Input } from '@/components/ui/input' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Separator } from '@/components/ui/separator' import { Search, X, History } from 'lucide-react' interface Account { id: string name: string email: string | null phone: string | null accountNumber: string | null } interface TransactionLineItem { id: string description: string qty: number unitPrice: string lineTotal: string } interface Transaction { id: string transactionNumber: string total: string status: string paymentMethod: string | null transactionType: string completedAt: string | null createdAt: string lineItems?: TransactionLineItem[] } function accountSearchOptions(search: string) { return queryOptions({ queryKey: ['pos', 'accounts', search], queryFn: () => api.get<{ data: Account[] }>('/v1/accounts', { q: search, limit: 10 }), enabled: search.length >= 2, }) } function customerHistoryOptions(accountId: string | null, itemSearch?: string) { return queryOptions({ queryKey: ['pos', 'customer-history', accountId, itemSearch ?? ''], queryFn: () => api.get<{ data: Transaction[] }>('/v1/transactions', { accountId, limit: 10, sort: 'created_at', order: 'desc', ...(itemSearch ? { itemSearch } : {}), }), enabled: !!accountId, }) } interface POSCustomerDialogProps { open: boolean onOpenChange: (open: boolean) => void } export function POSCustomerDialog({ open, onOpenChange }: POSCustomerDialogProps) { const { accountId, accountName, setAccount, clearAccount } = usePOSStore() const [search, setSearch] = useState('') const [showHistory, setShowHistory] = useState(false) const [historySearch, setHistorySearch] = useState('') const { data: searchData, isLoading } = useQuery(accountSearchOptions(search)) const accounts = searchData?.data ?? [] const { data: historyData } = useQuery(customerHistoryOptions(showHistory ? accountId : null, historySearch || undefined)) const history = historyData?.data ?? [] function handleSelect(account: Account) { setAccount(account.id, account.name, account.phone, account.email) setSearch('') setShowHistory(false) onOpenChange(false) } function handleClear() { clearAccount() setSearch('') setShowHistory(false) onOpenChange(false) } const [expandedTxn, setExpandedTxn] = useState(null) // Fetch detail for expanded transaction const { data: txnDetail } = useQuery({ queryKey: ['pos', 'transaction-detail', expandedTxn], queryFn: () => api.get(`/v1/transactions/${expandedTxn}`), enabled: !!expandedTxn, }) const toggleExpand = useCallback((id: string) => { setExpandedTxn((prev) => prev === id ? null : id) }, []) // History view if (showHistory && accountId) { return ( Order History — {accountName} {/* Search items in history */}
setHistorySearch(e.target.value)} placeholder="Search items (e.g. strings, bow)..." className="pl-10 h-10 text-sm" />
{history.length === 0 ? (

{historySearch ? `No orders with "${historySearch}"` : 'No transactions found'}

) : (
{history.map((txn) => (
{expandedTxn === txn.id && txnDetail?.lineItems && (
{txnDetail.lineItems.map((item) => (
{item.qty} x {item.description} ${parseFloat(item.lineTotal).toFixed(2)}
))}
)}
))}
)}
) } return ( Customer {/* Current selection */} {accountId && ( <>

{accountName}

Selected customer

)} {/* Search */}
setSearch(e.target.value)} placeholder="Search by name, phone, email, account #..." className="pl-10 h-11" autoFocus />
{/* Results */}
{isLoading ? (

Searching...

) : search.length >= 2 && accounts.length === 0 ? (

No customers found

) : (
{accounts.map((account) => ( ))}
)}
{/* Walk-in button */} {accountId && ( )}
) }