import React, { useState, useEffect } from 'react'; import { View, Text, ScrollView, TouchableOpacity, Linking } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useRouter, useLocalSearchParams } from 'expo-router'; import { StatusBar } from 'expo-status-bar'; import { ChevronLeft, Download, CheckCircle2, XCircle, Calendar, CreditCard } from 'lucide-react-native'; import api from '@/utils/api'; import { useAlert } from '@/components/AlertComponent'; import LoadingScreen from '@/components/LoadingScreen'; import { InvoiceInfo, InvoiceAccounting } from '@/types/types'; export default function InvoiceDetailScreen() { const router = useRouter(); const { id } = useLocalSearchParams(); const alert = useAlert(); const [invoiceInfo, setInvoiceInfo] = useState(null); const [partite, setPartite] = useState([]); const [isLoading, setIsLoading] = useState(true); const fetchInvoiceInfo = async () => { try { const response = await api.get(`/invoice-supplier/get-info?id=${id}`); if (response.data?.success) { setInvoiceInfo(response.data.result.info); // Map backend keys to our frontend types const mappedPartite: InvoiceAccounting[] = response.data.result.partite.map((p: any) => ({ expiry: p.scadenza, tpa_description: p.tpa_descrizione, amount: p.importo_dovuto, isPaid: p.pagata })); setPartite(mappedPartite); } } catch (error) { console.error('Error fetching invoice info:', error); alert.showAlert('error', 'Errore', 'Impossibile recuperare il dettaglio della fattura.'); } finally { setIsLoading(false); } }; useEffect(() => { if (id) { fetchInvoiceInfo(); } }, [id]); const handleDownload = () => { if (invoiceInfo?.link) { Linking.openURL(invoiceInfo.link); } else { alert.showAlert('error', 'Nessun Documento', 'Non รจ presente un documento allegato per questa fattura.'); } }; if (isLoading || !invoiceInfo) { return ; } return ( {/* Header */} router.back()} className="p-2 bg-gray-50 rounded-full active:bg-gray-100"> Dettaglio Fattura {/* General Info Card */} Informazioni Numero Documento {invoiceInfo.documentNumber} Fornitore {invoiceInfo.supplier} Cantiere {invoiceInfo.placeName} Data Documento {invoiceInfo.date} Importo {invoiceInfo.totalAmount} {/* Expiries */} {partite && partite.length > 0 && ( Scadenze {partite.map((item, idx) => ( {item.expiry} {item.isPaid ? : } {item.isPaid ? 'Pagata' : 'Da Pagare'} {item.tpa_description} {item.amount} ))} )} ); }