"use client"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { cn } from "@/lib/utils/ui"; import { useMemo, type ReactNode } from "react"; import type { CalendarDay, CalendarEvent } from "@/components/calendario/types"; import { parseDateKey } from "@/components/calendario/utils"; import { EVENT_TYPE_STYLES } from "@/components/calendario/day-cell"; import { currencyFormatter } from "@/lib/lancamentos/formatting-helpers"; type EventModalProps = { open: boolean; day: CalendarDay | null; onClose: () => void; onCreate: (date: string) => void; }; const fullDateFormatter = new Intl.DateTimeFormat("pt-BR", { day: "numeric", month: "long", year: "numeric", }); const capitalize = (value: string) => value.length > 0 ? value[0]?.toUpperCase().concat(value.slice(1)) : value; const formatCurrency = (value: number, isReceita: boolean) => { const formatted = currencyFormatter.format(value ?? 0); return isReceita ? `+${formatted}` : formatted; }; const EventCard = ({ children, type, }: { children: ReactNode; type: CalendarEvent["type"]; }) => { const style = EVENT_TYPE_STYLES[type]; return (
{children}
); }; const renderLancamento = ( event: Extract ) => { const isReceita = event.lancamento.transactionType === "Receita"; const subtitleParts = [ event.lancamento.categoriaName, event.lancamento.paymentMethod, event.lancamento.pagadorName, ].filter(Boolean); return (
{event.lancamento.name} {subtitleParts.length ? ( {subtitleParts.join(" • ")} ) : null}
{formatCurrency(event.lancamento.amount, isReceita)}
{capitalize(event.lancamento.transactionType)} {event.lancamento.condition} {event.lancamento.paymentMethod}
); }; const renderBoleto = (event: Extract) => { const isPaid = Boolean(event.lancamento.isSettled); const dueDate = event.lancamento.dueDate; const formattedDueDate = dueDate ? new Intl.DateTimeFormat("pt-BR").format(new Date(dueDate)) : null; return (
{event.lancamento.name} Boleto{formattedDueDate ? ` - Vence em ${formattedDueDate}` : ""}
{currencyFormatter.format(event.lancamento.amount ?? 0)}
{isPaid ? "Pago" : "Pendente"}
); }; const renderCard = (event: Extract) => (
Cartão {event.card.name} Vencimento dia {event.card.dueDay}
{event.card.totalDue !== null ? ( {currencyFormatter.format(event.card.totalDue)} ) : null}
Status: {event.card.status ?? "Indefinido"} {event.card.closingDay ? ( Fechamento dia {event.card.closingDay} ) : null}
); const renderEvent = (event: CalendarEvent) => { switch (event.type) { case "lancamento": return renderLancamento(event); case "boleto": return renderBoleto(event); case "cartao": return renderCard(event); default: return null; } }; export function EventModal({ open, day, onClose, onCreate }: EventModalProps) { const formattedDate = useMemo(() => { if (!day) return ""; const parsed = parseDateKey(day.date); return capitalize(fullDateFormatter.format(parsed)); }, [day]); const handleCreate = () => { if (!day) return; onClose(); onCreate(day.date); }; const description = day?.events.length ? "Confira os lançamentos e vencimentos cadastrados para este dia." : "Nenhum lançamento encontrado para este dia. Você pode criar um novo lançamento agora."; return ( (!value ? onClose() : null)}> {formattedDate} {description}
{day?.events.length ? ( day.events.map((event) => (
{renderEvent(event)}
)) ) : (
Nenhum lançamento ou vencimento registrado. Clique em{" "} Novo lançamento{" "} para começar.
)}
); }