"use client"; import { Badge } from "@/components/ui/badge"; import { Checkbox } from "@/components/ui/checkbox"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import type { EligibleInstallment } from "@/lib/installments/anticipation-types"; import { formatCurrentInstallment } from "@/lib/installments/utils"; import { cn } from "@/lib/utils/ui"; import { format } from "date-fns"; import { ptBR } from "date-fns/locale"; import MoneyValues from "@/components/money-values"; interface InstallmentSelectionTableProps { installments: EligibleInstallment[]; selectedIds: string[]; onSelectionChange: (ids: string[]) => void; } export function InstallmentSelectionTable({ installments, selectedIds, onSelectionChange, }: InstallmentSelectionTableProps) { const toggleSelection = (id: string) => { const newSelection = selectedIds.includes(id) ? selectedIds.filter((selectedId) => selectedId !== id) : [...selectedIds, id]; onSelectionChange(newSelection); }; const toggleAll = () => { if (selectedIds.length === installments.length && installments.length > 0) { onSelectionChange([]); } else { onSelectionChange(installments.map((inst) => inst.id)); } }; const formatPeriod = (period: string) => { const [year, month] = period.split("-"); const date = new Date(Number(year), Number(month) - 1); return format(date, "MMM/yyyy", { locale: ptBR }); }; const formatDate = (date: Date | null) => { if (!date) return "—"; return format(date, "dd/MM/yyyy", { locale: ptBR }); }; if (installments.length === 0) { return (

Nenhuma parcela elegível para antecipação encontrada.

Todas as parcelas desta compra já foram pagas ou antecipadas.

); } return (
0 } onCheckedChange={toggleAll} aria-label="Selecionar todas as parcelas" /> Parcela Período Vencimento Valor {installments.map((inst) => { const isSelected = selectedIds.includes(inst.id); return ( toggleSelection(inst.id)} > e.stopPropagation()}> toggleSelection(inst.id)} aria-label={`Selecionar parcela ${inst.currentInstallment}`} /> {formatCurrentInstallment( inst.currentInstallment ?? 0, inst.installmentCount ?? 0 )} {formatPeriod(inst.period)} {formatDate(inst.dueDate)} ); })}
{selectedIds.length > 0 && (
{selectedIds.length}{" "} {selectedIds.length === 1 ? "parcela selecionada" : "parcelas selecionadas"} Total:{" "} selectedIds.includes(inst.id)) .reduce((sum, inst) => sum + Number(inst.amount), 0)} />
)}
); }