import React, { useState, useCallback } from "react"; import { View, ScrollView, ActivityIndicator, Pressable, Modal, } from "react-native"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Stack, useLocalSearchParams, useFocusEffect } from "expo-router"; import { Text } from "@/components/ui/text"; import { User, Building2, Mail, Phone, MapPin, Hash, Tag, ShieldCheck, BookOpen, Pencil, Trash2, } from "@/lib/icons"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { StandardHeader } from "@/components/StandardHeader"; import { api } from "@/lib/api"; import { toast } from "@/lib/toast-store"; export default function CustomerDetailScreen() { const nav = useSirouRouter(); const { id } = useLocalSearchParams(); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [deleting, setDeleting] = useState(false); const [showDeleteModal, setShowDeleteModal] = useState(false); const fetch = useCallback(async () => { try { setLoading(true); const cId = Array.isArray(id) ? id[0] : id; if (!cId) return; const response = await api.customers.getById({ params: { id: cId } }); setData(response || null); } catch { toast.error("Error", "Failed to load customer"); setData(null); } finally { setLoading(false); } }, [id]); useFocusEffect(useCallback(() => { fetch(); }, [fetch])); const handleDelete = async () => { try { setDeleting(true); const cId = Array.isArray(id) ? id[0] : id; await api.customers.delete({ params: { id: cId } }); toast.success("Deleted", "Customer has been deleted"); setShowDeleteModal(false); nav.back(); } catch (err: any) { toast.error("Error", err?.message || "Failed to delete customer"); } finally { setDeleting(false); } }; if (loading) { return ( ); } if (!data) { return ( Failed to load customer details. Retry ); } const isCompany = data?.type === "COMPANY"; const d = data || {}; return ( {/* Identity Header */} {isCompany ? ( ) : ( )} {d.displayName || "—"} {isCompany ? "Company" : "Individual"} Created {d.createdAt ? new Date(d.createdAt).toLocaleDateString() : "—"} {/* Contact */} {(d.email || d.phone) && ( Contact } label="Email" value={d.email} /> {d.email && d.phone ? : null} } label="Phone" value={d.phone} /> )} {/* Identity Details */} {(d.firstName || d.lastName || d.companyName) && ( Identity Details {isCompany ? ( } label="Company Name" value={d.companyName} /> ) : ( <> } label="First Name" value={d.firstName} /> {d.firstName && d.lastName ? : null} } label="Last Name" value={d.lastName} /> )} )} {/* Documents */} {(d.tin || d.vatRegistrationNumber || d.businessLicenseNumber) && ( Documents } label="TIN Number" value={d.tin} /> {d.tin && d.vatRegistrationNumber ? : null} } label="VAT Registration" value={d.vatRegistrationNumber} /> {(d.tin || d.vatRegistrationNumber) && d.businessLicenseNumber ? : null} } label="Business License" value={d.businessLicenseNumber} /> )} {/* Address */} {d.address && ( Address {d.address} )} {/* Notes */} {d.notes && ( Notes {d.notes} )} {/* Divider */} {/* Action Buttons */} { const cId = Array.isArray(id) ? id[0] : id; nav.go("customers/edit", { id: cId }); }} className="bg-primary h-11 rounded-[6px] flex-row items-center justify-center gap-2" > Edit Customer setShowDeleteModal(true)} className="bg-red-500 h-11 rounded-[6px] flex-row items-center justify-center gap-2" > Delete Customer {/* Delete Confirmation Modal */} setShowDeleteModal(false)} > setShowDeleteModal(false)} > e.stopPropagation()} > Delete Customer? This will permanently delete{" "} {d.displayName} {" "} and all associated data. This action cannot be undone. {deleting ? ( ) : ( Yes, Delete )} setShowDeleteModal(false)} className="bg-secondary h-12 rounded-[6px] items-center justify-center border border-border" > Cancel ); } function InfoRow({ icon, label, value }: { icon: React.ReactNode; label: string; value: string }) { if (!value) return null; return ( {icon} {label} {value} ); }