"use client"; import { useState } from "react"; import { useParams, useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Calendar } from "@/components/ui/calendar"; import { CalendarIcon } from "lucide-react"; import { format } from "date-fns"; import TiptapEditor from "@/components/ui/tiptap-editor"; import { relatoriosApi, Report } from "@/services/relatoriosApi"; import { toast } from "@/hooks/use-toast"; // Definindo o tipo para a criação de um relatório, omitindo o 'id' que é gerado pelo banco. type ReportInput = Omit; export default function NovoLaudoPage() { const router = useRouter(); const params = useParams(); const patientId = params.id as string; const [formData, setFormData] = useState>({ order_number: "", exam: "", diagnosis: "", conclusion: "", cid_code: "", content_html: "", content_json: {}, status: "draft", requested_by: "", due_at: new Date().toISOString(), hide_date: false, hide_signature: false, }); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const handleInputChange = (e: React.ChangeEvent) => { const { id, value } = e.target; setFormData((prev: Partial) => ({ ...prev, [id]: value })); }; const handleSelectChange = (id: string, value: string) => { setFormData((prev: Partial) => ({ ...prev, [id]: value })); }; const handleCheckboxChange = (id: string, checked: boolean) => { setFormData((prev: Partial) => ({ ...prev, [id]: checked })); }; const handleDateChange = (date: Date | undefined) => { if (date) { setFormData((prev: Partial) => ({ ...prev, due_at: date.toISOString() })); } }; const handleEditorChange = (html: string, json: object) => { setFormData((prev: Partial) => ({ ...prev, content_html: html, content_json: json })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); setError(null); try { const laudoData = { ...formData, patient_id: patientId, } as ReportInput; await relatoriosApi.create(laudoData); toast({ title: "Sucesso!", description: "Laudo criado com sucesso." }); router.push(`/doctor/pacientes/${patientId}/laudos`); } catch (error: any) { const errorMessage = error.message || "Não foi possível criar o laudo. Tente novamente."; setError(errorMessage); toast({ title: "Erro ao criar laudo", description: errorMessage, variant: "destructive" }); } finally { setIsSubmitting(false); } }; return (
Criar Novo Laudo
{error &&

{error}

}