import React, { useState, useMemo } from "react"; import { View, Pressable, Modal, ScrollView, ActivityIndicator, TextInput, Dimensions, } from "react-native"; import { useSirouRouter } from "@sirou/react-native"; import { Text } from "@/components/ui/text"; import { Search, X, Plus, User, Building2, ChevronDown, Check } from "@/lib/icons"; import { api } from "@/lib/api"; import { useColorScheme } from "nativewind"; const { height: SCREEN_HEIGHT } = Dimensions.get("window"); interface CustomerData { id: string; name: string; email: string; phone: string; } interface CustomerPickerProps { selectedIds: string[]; selectedCustomers: CustomerData[]; onSelect: (ids: string[], customers: CustomerData[]) => void; placeholder?: string; } export function CustomerPicker({ selectedIds, selectedCustomers, onSelect, placeholder }: CustomerPickerProps) { const nav = useSirouRouter(); const { colorScheme } = useColorScheme(); const isDark = colorScheme === "dark"; const [show, setShow] = useState(false); const [customers, setCustomers] = useState([]); const [loading, setLoading] = useState(false); const [search, setSearch] = useState(""); const [tempIds, setTempIds] = useState(selectedIds); const [tempCustomers, setTempCustomers] = useState(selectedCustomers); const openPicker = async () => { setShow(true); setTempIds(selectedIds); setTempCustomers(selectedCustomers); setSearch(""); setLoading(true); try { const res = await api.customers.getAll({ query: { page: 1, limit: 50 } }); setCustomers(res?.data || []); } catch { setCustomers([]); } finally { setLoading(false); } }; const toggleCustomer = (c: any) => { const id = String(c.id); const name = c.displayName || `${c.firstName || ""} ${c.lastName || ""}`.trim() || c.companyName || ""; let newIds: string[]; let newCustomers: CustomerData[]; if (tempIds.includes(id)) { newIds = tempIds.filter((i) => i !== id); newCustomers = tempCustomers.filter((p) => p.id !== id); } else { newIds = [...tempIds, id]; newCustomers = [ ...tempCustomers, { id, name, email: c.email || "", phone: c.phone || "" }, ]; } setTempIds(newIds); setTempCustomers(newCustomers); onSelect(newIds, newCustomers); }; const filtered = useMemo(() => { if (!search.trim()) return customers; const q = search.toLowerCase(); return customers.filter( (c: any) => (c.displayName || "")?.toLowerCase().includes(q) || (c.firstName || "")?.toLowerCase().includes(q) || (c.lastName || "")?.toLowerCase().includes(q) || (c.email || "")?.toLowerCase().includes(q) || (c.phone || "")?.toLowerCase().includes(q), ); }, [customers, search]); const triggerLabel = selectedIds.length === 0 ? (placeholder || "Select customers") : selectedIds.length === 1 ? selectedCustomers[0]?.name || placeholder : `${selectedIds.length} customers selected`; return ( <> 0 ? "text-foreground" : "text-muted-foreground" }`} numberOfLines={1} > {triggerLabel} setShow(false)} > setShow(false)}> e.stopPropagation()} > Customers setShow(false)} className="h-8 w-8 bg-secondary/80 rounded-full items-center justify-center border border-border/10" > {search.length > 0 && ( setSearch("")}> )} { setShow(false); nav.go("customers/create"); }} className="bg-primary rounded-[6px] py-3.5 flex-row items-center justify-center gap-2" > Add New Customer {loading ? ( ) : filtered.length === 0 ? ( {search ? "No customers match your search" : "No customers found"} ) : ( filtered.map((c: any) => { const isCompany = c.type === "COMPANY"; const isSelected = tempIds.includes(String(c.id)); return ( toggleCustomer(c)} className="bg-card rounded-[6px] border border-border p-4 mb-3 flex-row items-center" > {isCompany ? ( ) : ( )} {c.displayName || `${c.firstName || ""} ${c.lastName || ""}`.trim() || c.companyName || "—"} {c.email && ( {c.email} )} {isCompany ? "Company" : "Individual"} {isSelected && } ); }) )} ); }