"use client"; import { RiCalendarEventLine } from "@remixicon/react"; import type { ReactNode } from "react"; import { EVENT_TYPE_STYLES } from "@/features/calendar/components/day-cell"; import MoneyValues from "@/shared/components/money-values"; import { Badge } from "@/shared/components/ui/badge"; import { Button } from "@/shared/components/ui/button"; import { Card } from "@/shared/components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/shared/components/ui/dialog"; import type { CalendarDay, CalendarEvent } from "@/shared/lib/types/calendar"; import { friendlyDate, parseLocalDateString } from "@/shared/utils/date"; import { formatFinancialDateLabel } from "@/shared/utils/financial-dates"; import { cn } from "@/shared/utils/ui"; type EventModalProps = { open: boolean; day: CalendarDay | null; onClose: () => void; onCreate: (date: string) => void; }; const EventCard = ({ children, type, }: { children: ReactNode; type: CalendarEvent["type"]; }) => { const style = EVENT_TYPE_STYLES[type]; return (
{children}
); }; const DATE_FORMAT: Intl.DateTimeFormatOptions = { day: "2-digit", month: "2-digit", year: "numeric", }; const renderLancamento = ( event: Extract, ) => { const isReceita = event.transaction.transactionType === "Receita"; return (
{event.transaction.name} {event.transaction.categoriaName}
); }; const renderBoleto = (event: Extract) => { const isPaid = Boolean(event.transaction.isSettled); const dueDateLabel = formatFinancialDateLabel( event.transaction.dueDate, "Vence em", DATE_FORMAT, ); const paymentDateLabel = isPaid ? formatFinancialDateLabel( event.transaction.boletoPaymentDate, "Pago em", DATE_FORMAT, ) : null; return (
{event.transaction.name}
{dueDateLabel && ( {dueDateLabel} )} {paymentDateLabel && ( {paymentDateLabel} )}
{isPaid ? "Pago" : "Pendente"}
); }; const renderCard = (event: Extract) => { const paymentDateLabel = event.card.isPaid ? formatFinancialDateLabel(event.card.paymentDate, "Pago em", DATE_FORMAT) : null; return (
Vencimento Fatura — {event.card.name} {paymentDateLabel && ( {paymentDateLabel} )} {event.card.isPaid ? "Pago" : (event.card.status ?? "Fatura")}
{event.card.totalDue !== null ? ( ) : null}
); }; const renderInstallment = ( event: Extract, ) => { const isReceita = event.transaction.transactionType === "Receita"; return (
{event.transaction.name} {event.installmentCount}x parcelas
por parcela
); }; const SECTION_LABELS: Record = { transaction: "Lançamentos", installment: "Parcelas", boleto: "Boletos", card: "Faturas", }; const renderEvent = (event: CalendarEvent) => { switch (event.type) { case "transaction": return renderLancamento(event); case "installment": return renderInstallment(event); case "boleto": return renderBoleto(event); case "card": return renderCard(event); default: return null; } }; export function EventModal({ open, day, onClose, onCreate }: EventModalProps) { const formattedDate = !day ? "" : friendlyDate(parseLocalDateString(day.date)); const handleCreate = () => { if (!day) return; onClose(); onCreate(day.date); }; const hasEvents = Boolean(day?.events.length); const grouped = day ? { transaction: day.events.filter((e) => e.type === "transaction"), installment: day.events.filter((e) => e.type === "installment"), boleto: day.events.filter((e) => e.type === "boleto"), card: day.events.filter((e) => e.type === "card"), } : null; return ( (!value ? onClose() : null)}> {formattedDate} {hasEvents ? "Lançamentos e vencimentos cadastrados para este dia." : "Nenhum lançamento encontrado para este dia."}
{hasEvents && grouped ? ( (["transaction", "installment", "boleto", "card"] as const) .filter((type) => grouped[type].length > 0) .map((type) => (

{SECTION_LABELS[type]}

{grouped[type].map((event) => (
{renderEvent(event)}
))}
)) ) : (

Nenhum lançamento registrado para este dia.

)}
); }