import React, { useEffect, useState } from 'react'; import { Text, View, FlatList, ScrollView, TouchableOpacity, ActivityIndicator, } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import Icon from 'react-native-vector-icons/Ionicons'; import { getStyles } from './leads.styles'; import { useTheme } from '@theme'; import { useAppDispatch, useAppSelector, RootState } from '@store'; import { getLeads, getLeadCountList } from './thunk'; import { LeadItem } from '@interfaces'; import { LeadItemCard, SearchInput, Loader, StatCard } from '@components'; import { LeadsStackParamList } from '../../navigation/leadsStack'; import { route } from '@utils'; type LeadsScreenNavigationProp = NativeStackNavigationProp< LeadsStackParamList, 'leads' >; export const LeadsScreen = () => { const dispatch = useAppDispatch(); const navigation = useNavigation(); const { theme: colors } = useTheme(); const styles = getStyles(colors); const [searchTerm, setSearchTerm] = useState(''); const [selectedStatusId, setSelectedStatusId] = useState(null); const { items, loading, error, counts, countsLoading } = useAppSelector( (state: RootState) => state.leads, ); const user_data = useAppSelector((state: RootState) => state.auth.user_data); useEffect(() => { if (user_data?.staffid) { dispatch(getLeads({ leadId: null, staffid: user_data.staffid })); dispatch(getLeadCountList(user_data.staffid)); } }, [dispatch, user_data]); const filteredLeads = items.filter(lead => { const matchesSearch = searchTerm ? lead.name.toLowerCase().includes(searchTerm.toLowerCase()) || lead.company.toLowerCase().includes(searchTerm.toLowerCase()) || lead.email.toLowerCase().includes(searchTerm.toLowerCase()) : true; const matchesStatus = !selectedStatusId || selectedStatusId === '0' ? true : lead.status === selectedStatusId; return matchesSearch && matchesStatus; }); const handleStatusPress = (id: string) => { setSelectedStatusId(prev => (prev === id ? null : id)); }; const handleLeadPress = (lead: LeadItem) => { navigation.navigate(route.leadDetails, { lead }); }; const renderLead = ({ item }: { item: LeadItem }) => ( handleLeadPress(item)} /> ); return ( {/* Search + Filter Header (Fixed at top) */} {/* Status count cards */} {countsLoading ? ( ) : counts.length > 0 ? ( {counts.map(item => ( handleStatusPress(item.id)} /> ))} ) : null} {/* Lead list */} {loading ? ( ) : error ? ( {error} ) : ( item.id} style={{ flex: 1 }} contentContainerStyle={styles.listContent} renderItem={renderLead} ListEmptyComponent={() => ( No Leads Found {searchTerm ? `No leads match "${searchTerm}". Try a different search.` : 'You have no leads yet. New leads will appear here.'} )} /> )} ); };