import { BASE_URL } from '@/api/URLs'; import { useTheme } from '@/components/ThemeContext'; import { products_api } from '@/screens/home/lib/api'; import { CompanyResponse } from '@/screens/home/lib/types'; import { BottomSheetBackdrop, BottomSheetModal, BottomSheetScrollView } from '@gorhom/bottom-sheet'; import { useInfiniteQuery } from '@tanstack/react-query'; import { Building2, ChevronRight, MapPin, Package } from 'lucide-react-native'; import React, { useCallback, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { ActivityIndicator, Dimensions, FlatList, Image, StyleSheet, Text, TouchableOpacity, View, } from 'react-native'; const { width: SCREEN_WIDTH } = Dimensions.get('window'); export default function CompanyList({ query }: { query: string }) { const { isDark } = useTheme(); const { t } = useTranslation(); const bottomSheetModalRef = useRef(null); const [selectedCompany, setSelectedCompany] = useState(null); const PAGE_SIZE = 10; const { data, isLoading, isError, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery({ queryKey: ['company-list', query], queryFn: async ({ pageParam = 1 }) => { const response = await products_api.getCompany({ page: pageParam, page_size: PAGE_SIZE, search: query, }); return response.data.data; }, getNextPageParam: (lastPage) => lastPage.current_page < lastPage.total_pages ? lastPage.current_page + 1 : undefined, initialPageParam: 1, }); const allCompanies = data?.pages.flatMap((page) => page.results) ?? []; const handlePresentModal = useCallback((company: CompanyResponse) => { setSelectedCompany(company); bottomSheetModalRef.current?.present(); }, []); const renderBackdrop = useCallback( (props: any) => ( ), [] ); if (isLoading) { return ( ); } if (isError) { return ( {t('Xatolik yuz berdi')} ); } return ( <> item.id.toString()} renderItem={({ item }) => ( handlePresentModal(item)} > {item.company_name} {item.country_name}, {item.region_name} )} onEndReached={() => hasNextPage && !isFetchingNextPage && fetchNextPage()} onEndReachedThreshold={0.4} ListFooterComponent={ isFetchingNextPage ? : null } showsVerticalScrollIndicator={false} /> {selectedCompany && ( <> {selectedCompany.company_name} {selectedCompany.country_name}, {selectedCompany.region_name},{' '} {selectedCompany.district_name} {selectedCompany.product_service_company && selectedCompany.product_service_company.length > 0 && ( <> {t('Mahsulotlar')} ({selectedCompany.product_service_company.length}) {selectedCompany.product_service_company.map((product) => ( {product.files?.[0]?.file ? ( ) : ( )} {product.title} {product.description || 'Tavsif mavjud emas.'} ))} )} )} ); } const CARD_WIDTH = SCREEN_WIDTH - 32; const styles = StyleSheet.create({ center: { flex: 1, justifyContent: 'center', alignItems: 'center' }, // --- Company Card --- card: { width: CARD_WIDTH - 4, marginLeft: 2, borderRadius: 16, padding: 16, marginBottom: 8, shadowColor: '#000', shadowOffset: { width: 0, height: 4 }, shadowOpacity: 0.2, shadowRadius: 8, elevation: 2, }, darkCard: { backgroundColor: '#1e293b', }, lightCard: { backgroundColor: '#ffffff', }, cardHeader: { flexDirection: 'row', alignItems: 'center', gap: 10, marginBottom: 6 }, cardTitle: { fontSize: 17, fontWeight: '700', flex: 1 }, darkText: { color: '#f1f5f9', }, lightText: { color: '#0f172a', }, cardLocation: { flexDirection: 'row', alignItems: 'center', gap: 6 }, cardLocationText: { fontSize: 13 }, darkSubText: { color: '#64748b', }, lightSubText: { color: '#94a3b8', }, // --- Bottom Sheet --- sheetContent: { padding: 20, paddingBottom: 40 }, sheetHeader: { alignItems: 'center', marginBottom: 20 }, sheetTitle: { fontSize: 22, fontWeight: '800', marginTop: 8, textAlign: 'center', }, sheetLocation: { flexDirection: 'row', alignItems: 'center', gap: 6, marginTop: 6 }, sheetLocationText: { fontSize: 14 }, divider: { height: 1, marginBottom: 16 }, darkDivider: { backgroundColor: '#334155', }, lightDivider: { backgroundColor: '#e2e8f0', }, sectionLabel: { fontSize: 14, fontWeight: '700', textTransform: 'uppercase', marginBottom: 12, }, // --- Product Card --- productCard: { flexDirection: 'row', borderRadius: 12, padding: 16, marginBottom: 12, gap: 16, alignItems: 'flex-start', }, darkProductCard: { backgroundColor: '#1e293b', }, lightProductCard: { backgroundColor: '#f8fafc', }, productImage: { width: 60, height: 60, borderRadius: 8, alignItems: 'center', justifyContent: 'center', overflow: 'hidden', borderWidth: 1, }, darkProductImage: { backgroundColor: '#0f172a', borderColor: '#334155', }, lightProductImage: { backgroundColor: '#ffffff', borderColor: '#e2e8f0', }, productImg: { width: '100%', height: '100%', borderRadius: 8 }, productInfo: { flex: 1 }, productTitle: { fontSize: 15, fontWeight: '700' }, productDesc: { fontSize: 13, marginTop: 2 }, });