import { useState } from 'react'; import { useQuery, useQueryClient } from 'react-query'; import { formatCurrency } from '../../utils/format'; import { Dialog } from '@headlessui/react'; import { useForm } from 'react-hook-form'; import { FormInput, FormSelect } from '../forms'; import { PencilIcon, TrashIcon } from '@heroicons/react/24/outline'; import api from '../../services/api'; // Função auxiliar para retornar as cores do status const getStatusColor = (status: string) => { const colors = { PENDENTE: 'bg-yellow-100 text-yellow-800', PAGO: 'bg-green-100 text-green-800', VENCIDO: 'bg-red-100 text-red-800', CANCELADO: 'bg-gray-100 text-gray-800', PARCIALMENTE_PAGO: 'bg-blue-100 text-blue-800' }; return colors[status] || colors.PENDENTE; }; const TIPOS_CUSTA = [ { value: 'TAXA_ADMINISTRATIVA', label: 'Taxa Administrativa' }, { value: 'RECURSO', label: 'Recurso' }, { value: 'DILIGENCIA', label: 'Diligência' }, { value: 'CERTIDAO', label: 'Certidão' }, { value: 'COPIA', label: 'Cópia' }, { value: 'OUTROS', label: 'Outros' } ]; export function CustaAdministrativaPanel({ processoId }) { const [isModalOpen, setIsModalOpen] = useState(false); const [selectedCusta, setSelectedCusta] = useState(null); const { register, handleSubmit, reset, setValue } = useForm(); const queryClient = useQueryClient(); const { data: custas, isLoading } = useQuery( ['custas-administrativas', processoId], async () => { const response = await api.get(`/processos-administrativos/${processoId}/custas`); return response.data; } ); const handleEdit = (custa) => { setSelectedCusta(custa); Object.keys(custa).forEach(key => { if (key === 'vencimento' || key === 'dataPagamento') { setValue(key, custa[key] ? new Date(custa[key]).toISOString().split('T')[0] : ''); } else { setValue(key, custa[key]); } }); setIsModalOpen(true); }; const handleDelete = async (id) => { if (window.confirm('Tem certeza que deseja excluir esta custa?')) { try { await api.delete(`/processos-administrativos/${processoId}/custas/${id}`); queryClient.invalidateQueries(['custas-administrativas', processoId]); } catch (error) { console.error('Erro ao excluir custa:', error); } } }; const onSubmit = async (data) => { try { // Formatar as datas antes de enviar const formattedData = { ...data, vencimento: data.vencimento ? new Date(data.vencimento).toISOString() : null, dataPagamento: data.dataPagamento ? new Date(data.dataPagamento).toISOString() : null, valor: Number(data.valor) }; if (selectedCusta) { await api.put(`/processos-administrativos/${processoId}/custas/${selectedCusta.id}`, formattedData); } else { await api.post(`/processos-administrativos/${processoId}/custas`, formattedData); } queryClient.invalidateQueries(['custas-administrativas', processoId]); setIsModalOpen(false); setSelectedCusta(null); reset(); } catch (error) { console.error('Erro ao salvar custa:', error); } }; if (isLoading) return
Carregando...
; return (

Custas e Despesas

{custas?.map((custa) => ( ))}
Tipo Descrição Valor Status Vencimento Ações
{custa.tipo} {custa.descricao} {formatCurrency(custa.valor)} {custa.status} {custa.vencimento ? new Date(custa.vencimento).toLocaleDateString() : '-'}
{/* Modal de Custa */} { setIsModalOpen(false); setSelectedCusta(null); reset(); }}>
); }