import { View, Text, ScrollView, ActivityIndicator, TouchableOpacity, Linking } from 'react-native'; import { useLocalSearchParams, router } from 'expo-router'; import { useQuery } from '@tanstack/react-query'; import { useState } from 'react'; import { api } from '../../../services/api'; export default function ClientDetailScreen() { const { id } = useLocalSearchParams<{ id: string }>(); const [tab, setTab] = useState<'profile' | 'subscription' | 'invoices' | 'payments'>('profile'); const { data: client, isLoading } = useQuery({ queryKey: ['client', id], queryFn: () => api.get(`/api/v1/clients/${id}`).then(r => r.data), }); const { data: invoices } = useQuery({ queryKey: ['client-invoices', id], queryFn: () => api.get(`/api/v1/invoices?clientId=${id}&limit=20`).then(r => r.data?.data ?? r.data), enabled: tab === 'invoices', }); const { data: payments } = useQuery({ queryKey: ['client-payments', id], queryFn: () => api.get(`/api/v1/clients/${id}/payments`).then(r => r.data?.data ?? r.data), enabled: tab === 'payments', }); if (isLoading) return ; const TABS = ['profile', 'subscription', 'invoices', 'payments'] as const; return ( router.back()} className="mb-2"> ← Back {client?.firstName} {client?.lastName} {client?.accountNumber} {TABS.map(t => ( setTab(t)} className={`px-3 py-1.5 rounded-full ${tab === t ? 'bg-primary' : 'bg-gray-100'}`}> {t} ))} {tab === 'profile' && ( {[ { label: 'Full Name', value: `${client?.firstName} ${client?.lastName}` }, { label: 'Phone', value: client?.phone, action: () => Linking.openURL(`tel:${client?.phone}`) }, { label: 'Email', value: client?.email }, { label: 'Address', value: client?.address }, { label: 'Area', value: client?.area?.name }, { label: 'Status', value: client?.status }, ].map((row, i) => ( 0 ? 'border-t border-gray-100' : ''}`}> {row.label} {row.value ?? '—'} ))} )} {tab === 'subscription' && ( Current Subscription {client?.subscription ? ( <> Plan: {client.subscription.plan?.name} Status: {client.subscription.status} Billing Day: {client.subscription.billingDay} Monthly: ₱{client.subscription.plan?.price?.toLocaleString()} ) : ( No active subscription )} )} {tab === 'invoices' && ( {(invoices ?? []).map((inv: any) => ( {inv.invoiceNumber} ₱{inv.totalAmount?.toLocaleString()} · {inv.status} ))} {!invoices?.length && No invoices} )} {tab === 'payments' && ( {(payments ?? []).map((p: any) => ( ₱{p.amount?.toLocaleString()} {p.paymentMethod} · {new Date(p.paymentDate).toLocaleDateString()} ))} {!payments?.length && No payments} )} ); }