import React from 'react'; import { Models } from '@/lib/appwrite'; import RefreshButton from './RefreshButton'; import ListHeader from './ListHeader'; import DataTable, { Column } from './DataTable'; import Pagination from './Pagination'; import TableActions from './TableActions'; import SearchBar from './SearchBar'; interface PagamentoListProps { pagamentos: Models.Document[]; loading: boolean; error: string | null; totalPagamentos: number; currentPage: number; pageSize: number; onEdit: (p: Models.Document) => void; onDelete: (id: string) => Promise; onRefresh: () => void; onPrevPage: () => void; onNextPage: () => void; onSearch: (term: string) => void; } type PagamentoDocument = Models.Document & { pedidos?: string[] | { $id?: string } | string | null; valor?: number | string; }; const PagamentoList: React.FC = ({ pagamentos, loading, error, totalPagamentos, currentPage, pageSize, onEdit, onDelete, onRefresh, onPrevPage, onNextPage, onSearch, }) => { const totalPages = Math.ceil(totalPagamentos / pageSize); const startItem = (currentPage - 1) * pageSize + 1; const endItem = Math.min(currentPage * pageSize, totalPagamentos); const [search, setSearch] = React.useState(''); const columns: Column[] = [ { key: 'pedidos', header: 'Pedido', render: row => { const pedidos = row.pedidos; if (Array.isArray(pedidos)) return pedidos.join(', '); if (pedidos && typeof pedidos === 'object') return pedidos.$id || 'N/A'; return pedidos ?? 'N/A'; }, }, { key: 'status', header: 'Status' }, { key: 'metodo', header: 'Método' }, { key: 'valor', header: 'Valor', render: row => Number(row.valor).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }) }, { key: '$id', header: 'ID' }, { key: '$createdAt', header: 'Data de Criação', render: row => new Date(row.$createdAt).toLocaleDateString('pt-BR') }, ]; const handleDelete = async (id: string) => { if (confirm('Tem certeza que deseja deletar este pagamento?')) { await onDelete(id); } }; return (
onSearch(search)} placeholder="Buscar status" /> {error && (
{error}
)} {loading ? (
Carregando pagamentos...
) : ( <> ( onEdit(pag)} onDelete={() => handleDelete(pag.$id)} /> )} />
{totalPagamentos > 0 ? ( <>Mostrando {startItem} - {endItem} de {totalPagamentos} pagamentos ) : ( 'Total de pagamentos: 0' )}
)}
); }; export default PagamentoList;