import React, { useState } from "react"; import { View, ScrollView, Pressable, ActivityIndicator, useColorScheme, } from "react-native"; import { api } from "@/lib/api"; import { Text } from "@/components/ui/text"; import { EmptyState } from "@/components/EmptyState"; import { Card, CardContent } from "@/components/ui/card"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Plus, History as HistoryIcon, Briefcase, ChevronRight, Clock, DollarSign, FileText, ScanLine, } from "@/lib/icons"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { ShadowWrapper } from "@/components/ShadowWrapper"; import { StandardHeader } from "@/components/StandardHeader"; import { useAuthStore } from "@/lib/auth-store"; export default function HomeScreen() { const [activeFilter, setActiveFilter] = useState("All"); const [stats, setStats] = useState({ total: 0, paid: 0, pending: 0, overdue: 0, totalRevenue: 0, }); const [invoices, setInvoices] = useState([]); const [loading, setLoading] = useState(false); const nav = useSirouRouter(); React.useEffect(() => { fetchStats(); }, []); React.useEffect(() => { fetchInvoices(); }, [activeFilter]); const fetchStats = async () => { const { isAuthenticated } = useAuthStore.getState(); if (!isAuthenticated) return; try { const data = await api.invoices.stats(); setStats(data); } catch (e) { console.error("[HomeScreen] Failed to fetch stats:", e); } }; const fetchInvoices = async () => { const { isAuthenticated } = useAuthStore.getState(); if (!isAuthenticated) return; setLoading(true); try { const statusParam = activeFilter === "All" ? undefined : activeFilter.toUpperCase(); const response = await api.invoices.getAll({ query: { limit: 5, status: statusParam, }, }); setInvoices(response.data || []); } catch (e) { console.error("[HomeScreen] Failed to fetch invoices:", e); } finally { setLoading(false); } }; const colorScheme = useColorScheme(); const isDark = colorScheme === "dark"; return ( {/* Balance Card Section */} Available Balance $ {stats.total.toLocaleString()} Pending ${stats.pending.toLocaleString()} Income ${stats.totalRevenue.toLocaleString()} {/* Circular Quick Actions Section */} } label="Company" onPress={() => nav.go("company")} /> } label="Scan SMS" onPress={() => nav.go("sms-scan")} /> } label="Create Proforma" onPress={() => nav.go("proforma/create")} /> } label="History" onPress={() => nav.go("history")} /> {/* Recent Activity Header */} Recent Activity nav.go("history")} className="px-4 py-2 rounded-full" > View all {/* Filters */} {["All", "Draft", "Pending", "Paid", "Overdue", "Cancelled"].map( (filter) => ( setActiveFilter(filter)} className={`rounded-[4px] px-4 py-1.5 ${ activeFilter === filter ? "bg-primary" : "bg-card border border-border" }`} > {filter} ), )} {/* Transactions List */} {loading ? ( ) : invoices.length > 0 ? ( invoices.map((inv) => ( nav.go("invoices/[id]", { id: inv.id })} > {inv.customerName} {new Date(inv.issueDate).toLocaleDateString()} ยท Proforma ${Number(inv.amount).toLocaleString()} {inv.status} )) ) : ( nav.go("proforma/create")} previewLines={3} /> )} ); } function QuickAction({ icon, label, onPress, }: { icon: React.ReactNode; label: string; onPress?: () => void; }) { return ( {icon} {label} ); }