import React, { useState, useEffect } from "react"; import { View, ScrollView, ActivityIndicator, Alert, Linking, useColorScheme, Pressable, } from "react-native"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Stack, useLocalSearchParams } from "expo-router"; import { Text } from "@/components/ui/text"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { FileText, Calendar, Share2, Download, Trash2, Package, Clock, ExternalLink, ChevronRight, User, CreditCard, Hash, AlertCircle, } from "@/lib/icons"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { ShadowWrapper } from "@/components/ShadowWrapper"; import { StandardHeader } from "@/components/StandardHeader"; import { api, BASE_URL } from "@/lib/api"; import { toast } from "@/lib/toast-store"; import { useAuthStore } from "@/lib/auth-store"; export default function ProformaDetailScreen() { const nav = useSirouRouter(); const { id } = useLocalSearchParams(); const colorScheme = useColorScheme(); const isDark = colorScheme === "dark"; const [loading, setLoading] = useState(true); const [proforma, setProforma] = useState(null); useEffect(() => { fetchProforma(); }, [id]); const fetchProforma = async () => { try { setLoading(true); // Ensure id is a string if useLocalSearchParams returns an array const proformaId = Array.isArray(id) ? id[0] : id; if (!proformaId) throw new Error("No ID provided"); const data = await api.proforma.getById({ params: { id: proformaId } }); setProforma(data); } catch (error: any) { console.error("[ProformaDetail] Error:", error); toast.error("Error", "Failed to load proforma details"); } finally { setLoading(false); } }; const handleGetPdf = async () => { try { const { token } = useAuthStore.getState(); const pdfUrl = `${BASE_URL}proforma/${id}/pdf?token=${token}`; await Linking.openURL(pdfUrl); } catch (error) { console.error("[ProformaDetail] PDF Error:", error); toast.error("Error", "Failed to open PDF"); } }; const handleDelete = async () => { Alert.alert( "Delete Proforma", "Are you sure you want to delete this proforma? This action cannot be undone.", [ { text: "Cancel", style: "cancel" }, { text: "Delete", style: "destructive", onPress: async () => { try { setLoading(true); const proformaId = Array.isArray(id) ? id[0] : id; await api.proforma.delete({ params: { id: proformaId as string }, }); toast.success("Success", "Proforma deleted successfully"); nav.back(); } catch (error) { console.error("[ProformaDetail] Delete Error:", error); toast.error("Error", "Failed to delete proforma"); setLoading(false); } }, }, ], ); }; if (loading) { return ( ); } if (!proforma) { return ( Proforma Not Found The requested document could not be retrieved. ); } const amountValue = Number( typeof proforma.amount === "object" ? proforma.amount.value : proforma.amount, ); const taxAmountValue = Number( typeof proforma.taxAmount === "object" ? proforma.taxAmount?.value : proforma.taxAmount || 0, ); const discountValue = Number( typeof proforma.discountAmount === "object" ? proforma.discountAmount?.value : proforma.discountAmount || 0, ); const subtotalValue = amountValue - taxAmountValue + discountValue; const items = proforma.items || []; const statusColors = { PAID: { bg: "bg-emerald-500/10", text: "text-emerald-500", dot: "bg-emerald-500", }, PENDING: { bg: "bg-amber-500/10", text: "text-amber-500", dot: "bg-amber-500", }, DRAFT: { bg: "bg-blue-500/10", text: "text-blue-500", dot: "bg-blue-500" }, DEFAULT: { bg: "bg-slate-500/10", text: "text-slate-500", dot: "bg-slate-500", }, }; const status = (proforma.status || "PENDING").toUpperCase(); const colors = statusColors[status as keyof typeof statusColors] || statusColors.DEFAULT; return ( nav.go("proforma/edit", { id: proforma.id })} /> {/* Modern Hero Area */} {status} Proforma Estimated Total {amountValue.toLocaleString(undefined, { minimumFractionDigits: 2, })} {proforma.currency || "ETB"} {/* Quick Stats Grid */} Issue Date {new Date( proforma.issueDate || proforma.createdAt, ).toLocaleDateString()} Due Date {new Date(proforma.dueDate).toLocaleDateString()} {/* Client Box */} Quotation For {proforma.customerName || "Interested Client"} {proforma.customerEmail && ( {proforma.customerEmail} )} #{proforma.id.split("-")[0]} {/* Detailed Items Table */} Estimate Summary {items.map((item: any, idx: number) => ( {item.description} {Number( item.total?.value || item.total || 0, ).toLocaleString()} {item.quantity} units x{" "} {Number( item.unitPrice?.value || item.unitPrice || 0, ).toLocaleString()}{" "} {proforma.currency} ))} {items.length === 0 && ( Empty line items list )} {/* Billing Breakdown */} Net Price {subtotalValue.toLocaleString()} {proforma.currency} {taxAmountValue > 0 && ( Estimated Tax +{taxAmountValue.toLocaleString()} {proforma.currency} )} {discountValue > 0 && ( Applicable Discount -{discountValue.toLocaleString()} {proforma.currency} )} Estimated Total Valid as of today {amountValue.toLocaleString()} {proforma.currency} {/* Notes */} {proforma.notes && ( Internal Notes " {proforma.notes} " )} {/* Premium Actions */} ); }