'use client'; import * as React from 'react'; import { useParams } from 'next/navigation'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { Package, ShoppingCart, Users } from 'lucide-react'; import { getTenant, updateTenant, type TenantDetail } from '@/lib/api/tenants'; import { listPlanos } from '@/lib/api/planos'; import { marcarFaturaPaga } from '@/lib/api/faturas'; import type { Fatura } from '@/lib/api/faturas'; import { updateTenantSchema, type UpdateTenantFormValues } from '@/lib/validators/tenants'; import { ApiError } from '@/lib/api/client'; import { formatCurrency, formatDate } from '@/lib/format'; import { toast } from '@/lib/toast'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { DataTable, type DataTableColumn } from '@/components/ui/data-table'; import { FormField } from '@/components/ui/form-field'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Skeleton } from '@/components/ui/skeleton'; import { StatusPill } from '@/components/ui/status-pill'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { StatCard } from '@/components/domain/stat-card'; import { TenantStatusDialog } from '@/components/domain/tenant-status-dialog'; export default function TenantDetailPage() { const params = useParams<{ id: string }>(); const tenantId = params.id; const queryClient = useQueryClient(); const [statusDialogOpen, setStatusDialogOpen] = React.useState(false); const { data: tenant, isLoading } = useQuery({ queryKey: ['master', 'tenants', tenantId], queryFn: () => getTenant(tenantId), }); if (isLoading || !tenant) { return (
); } return (

{tenant.nome_fantasia || tenant.razao_social}

{tenant.email_contato}

Dados cadastrais Faturas Uso
queryClient.invalidateQueries({ queryKey: ['master', 'tenants', tenantId] })} />
); } function DadosCadastraisForm({ tenantId, tenant }: { tenantId: string; tenant: TenantDetail }) { const queryClient = useQueryClient(); const { data: planos } = useQuery({ queryKey: ['master', 'planos', { ativo: true }], queryFn: () => listPlanos({ ativo: true }), }); const form = useForm({ resolver: zodResolver(updateTenantSchema), defaultValues: { razao_social: tenant.razao_social, nome_fantasia: tenant.nome_fantasia ?? '', documento: tenant.documento ?? '', email_contato: tenant.email_contato, telefone: tenant.telefone ?? '', plano_id: tenant.plano_id, dia_vencimento_mensalidade: tenant.dia_vencimento_mensalidade, }, }); const mutation = useMutation({ mutationFn: (values: UpdateTenantFormValues) => updateTenant(tenantId, { ...values, nome_fantasia: values.nome_fantasia || undefined, documento: values.documento || undefined, telefone: values.telefone || undefined, }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['master', 'tenants', tenantId] }); queryClient.invalidateQueries({ queryKey: ['master', 'tenants'] }); toast.success('Dados do tenant atualizados.'); }, onError: (err) => { toast.error(err instanceof ApiError ? err.message : 'Não foi possível salvar as alterações.'); }, }); return ( Dados cadastrais
mutation.mutate(values))} >
(v === '' ? undefined : Number(v)), })} />
); } function FaturasTab({ faturas, tenantId }: { faturas: Fatura[]; tenantId: string }) { const queryClient = useQueryClient(); const marcarPagoMutation = useMutation({ mutationFn: (id: string) => marcarFaturaPaga(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['master', 'tenants', tenantId] }); toast.success('Fatura marcada como paga.'); }, onError: (err) => { toast.error(err instanceof ApiError ? err.message : 'Não foi possível marcar a fatura como paga.'); }, }); const columns: DataTableColumn[] = [ { id: 'referencia', header: 'Referência', primary: true, cell: (row) => row.referencia }, { id: 'valor', header: 'Valor', cell: (row) => formatCurrency(row.valor) }, { id: 'vencimento', header: 'Vencimento', cell: (row) => formatDate(row.vencimento) }, { id: 'status', header: 'Status', primary: true, cell: (row) => }, { id: 'pago_em', header: 'Pago em', cell: (row) => formatDate(row.pago_em) }, ]; return (

Últimas 5 faturas deste tenant.

row.id} emptyTitle="Nenhuma fatura gerada ainda" rowActions={(row) => row.status !== 'pago' ? ( ) : ( Paga ) } />
); }