import React, { useState, useEffect, useCallback } from "react"; import { View, Pressable, ActivityIndicator, FlatList, ListRenderItem, } from "react-native"; import { Text } from "@/components/ui/text"; import { Card } from "@/components/ui/card"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Plus, Send, FileText, Clock, ChevronRight } from "@/lib/icons"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { StandardHeader } from "@/components/StandardHeader"; import { Button } from "@/components/ui/button"; import { EmptyState } from "@/components/EmptyState"; import { api } from "@/lib/api"; import { useAuthStore } from "@/lib/auth-store"; import { PERMISSION_MAP, hasPermission } from "@/lib/permissions"; interface ProformaItem { id: string; proformaNumber: string; customerName: string; customerEmail: string; customerPhone: string; amount: any; currency: string; issueDate: string; dueDate: string; description: string; notes: string; taxAmount: any; discountAmount: any; pdfPath: string; userId: string; items: any[]; createdAt: string; updatedAt: string; } const dummyData: ProformaItem = { id: "dummy-1", proformaNumber: "PF-001", customerName: "John Doe", customerEmail: "john@example.com", customerPhone: "+1234567890", amount: { value: 1000, currency: "USD" }, currency: "USD", issueDate: "2026-03-10T11:51:36.134Z", dueDate: "2026-03-10T11:51:36.134Z", description: "Dummy proforma", notes: "Test notes", taxAmount: { value: 100, currency: "USD" }, discountAmount: { value: 50, currency: "USD" }, pdfPath: "dummy.pdf", userId: "user-1", items: [ { id: "item-1", description: "Test item", quantity: 1, unitPrice: { value: 1000, currency: "USD" }, total: { value: 1000, currency: "USD" } } ], createdAt: "2026-03-10T11:51:36.134Z", updatedAt: "2026-03-10T11:51:36.134Z" }; export default function ProformaScreen() { const nav = useSirouRouter(); const permissions = useAuthStore((s) => s.permissions); const [proformas, setProformas] = useState([]); const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); const [loadingMore, setLoadingMore] = useState(false); // Check permissions const canCreateProformas = hasPermission(permissions, PERMISSION_MAP["proforma:create"]); const fetchProformas = useCallback( async (pageNum: number, isRefresh = false) => { const { isAuthenticated } = useAuthStore.getState(); if (!isAuthenticated) return; try { if (!isRefresh) { pageNum === 1 ? setLoading(true) : setLoadingMore(true); } const response = await api.proforma.getAll({ query: { page: pageNum, limit: 10 }, }); let newProformas = response.data; const newData = newProformas; if (isRefresh) { setProformas(newData); } else { setProformas((prev) => pageNum === 1 ? newData : [...prev, ...newData], ); } setHasMore(response.meta.hasNextPage); setPage(pageNum); } catch (err: any) { console.error("[Proforma] Fetch error:", err); setHasMore(false); } finally { setLoading(false); setRefreshing(false); setLoadingMore(false); } }, [], ); useEffect(() => { fetchProformas(1); }, [fetchProformas]); const onRefresh = () => { setRefreshing(true); fetchProformas(1, true); }; const loadMore = () => { if (hasMore && !loadingMore && !loading) { fetchProformas(page + 1); } }; const renderProformaItem: ListRenderItem = ({ item }) => { const amountVal = typeof item.amount === "object" ? item.amount.value : item.amount; const issuedStr = item.issueDate ? new Date(item.issueDate).toLocaleDateString() : ""; const dueStr = item.dueDate ? new Date(item.dueDate).toLocaleDateString() : ""; const itemsCount = Array.isArray(item.items) ? item.items.length : 0; return ( nav.go("proforma/[id]", { id: item.id })} className="mb-3" > {item.proformaNumber || "Proforma"} {item.customerName || "Customer"} {item.currency || "$"} {amountVal?.toLocaleString?.() ?? amountVal ?? "0"} Issued: {issuedStr} | Due: {dueStr} | {itemsCount} item{itemsCount !== 1 ? "s" : ""} ); }; return ( item.id} contentContainerStyle={{ paddingBottom: 150 }} showsVerticalScrollIndicator={false} onRefresh={onRefresh} refreshing={refreshing} onEndReached={loadMore} onEndReachedThreshold={0.5} ListHeaderComponent={ <> {/* {canCreateProformas && ( */} {/* )} */} } ListFooterComponent={ loadingMore ? ( ) : null } ListEmptyComponent={ !loading ? ( ) : ( ) } /> ); }