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 } from "@/lib/icons"; import { api } from "@/lib/api"; import { useColorScheme } from "nativewind"; const { height: SCREEN_HEIGHT } = Dimensions.get("window"); interface CustomerData { name: string; email: string; phone: string; } interface CustomerPickerProps { value: string; onSelect: (c: CustomerData) => void; placeholder?: string; } export function CustomerPicker({ value, 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 openPicker = async () => { setShow(true); setSearch(""); setLoading(true); try { const res = await api.customers.getAll({ query: { page: 1, limit: 50 } }); setCustomers(res?.data || []); } catch { setCustomers([]); } finally { setLoading(false); } }; 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]); return ( <> {value || placeholder || "Select a customer"} 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("")}> )} {/* Add New Customer */} { 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"; return ( { setShow(false); onSelect({ name: c.displayName || `${c.firstName || ""} ${c.lastName || ""}`.trim() || c.companyName || "", email: c.email || "", phone: c.phone || "", }); }} className="bg-card rounded-[6px] border border-border p-4 mb-3" > {isCompany ? ( ) : ( )} {c.displayName || `${c.firstName || ""} ${c.lastName || ""}`.trim() || c.companyName || "—"} {c.email && ( {c.email} )} {isCompany ? "Company" : "Individual"} ); }) )} ); }