import { useState } from 'react'; import { View, Text, TextInput, TouchableOpacity, ScrollView, Alert, ActivityIndicator } from 'react-native'; import { router } from 'expo-router'; import { api } from '../../../services/api'; const METHODS = ['CASH', 'GCASH', 'MAYA', 'BANK']; export default function RecordPaymentScreen() { const [search, setSearch] = useState(''); const [client, setClient] = useState(null); const [amount, setAmount] = useState(''); const [method, setMethod] = useState('CASH'); const [reference, setReference] = useState(''); const [loading, setLoading] = useState(false); const [searching, setSearching] = useState(false); const searchClient = async () => { if (!search.trim()) return; setSearching(true); try { const res = await api.get(`/api/v1/clients?search=${search.trim()}&limit=1`); const found = res.data?.data?.[0] ?? res.data?.[0]; if (found) setClient(found); else Alert.alert('Not Found', 'No client found with that account number or name.'); } catch { Alert.alert('Error', 'Search failed.'); } finally { setSearching(false); } }; const submit = async () => { if (!client) return Alert.alert('Required', 'Search and select a client first.'); if (!amount || isNaN(Number(amount))) return Alert.alert('Required', 'Enter a valid amount.'); setLoading(true); try { await api.post('/api/v1/payments', { clientId: client.id, amount: Number(amount), paymentMethod: method, referenceNumber: reference || undefined, paymentDate: new Date().toISOString(), }); Alert.alert('Success', 'Payment recorded!', [{ text: 'OK', onPress: () => router.back() }]); } catch (e: any) { Alert.alert('Error', e?.response?.data?.message ?? 'Payment failed.'); } finally { setLoading(false); } }; return ( router.back()} className="mr-3"> Record Payment Search Client {searching ? : Find} {client && ( {client.firstName} {client.lastName} {client.accountNumber} )} Amount (₱) Payment Method {METHODS.map(m => ( setMethod(m)} className={`rounded-xl px-4 py-2 mr-2 mb-2 border ${method === m ? 'bg-primary border-primary' : 'bg-white border-gray-200'}`} > {m} ))} Reference # (optional) {loading ? : Submit Payment} ); }