"use client"; import { useParams, useRouter } from "next/navigation"; import { useState } from "react"; 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 { reportsApi } from "@/services/reportsApi.mjs"; import DoctorLayout from "@/components/doctor-layout"; 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: {}, // Added for the JSON content from the editor status: "draft", requested_by: "", due_at: new Date(), hide_date: false, hide_signature: false, }); const [isSubmitting, setIsSubmitting] = useState(false); const handleInputChange = (e: React.ChangeEvent) => { const { id, value } = e.target; setFormData(prev => ({ ...prev, [id]: value })); }; const handleSelectChange = (id: string, value: string) => { setFormData(prev => ({ ...prev, [id]: value })); }; const handleCheckboxChange = (id: string, checked: boolean) => { setFormData(prev => ({ ...prev, [id]: checked })); }; const handleDateChange = (date: Date | undefined) => { if (date) { setFormData(prev => ({ ...prev, due_at: date })); } }; // Updated to handle both HTML and JSON from the editor const handleEditorChange = (html: string, json: object) => { setFormData(prev => ({ ...prev, content_html: html, content_json: json })); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); try { const laudoData = { ...formData, patient_id: patientId, due_at: formData.due_at.toISOString(), // Ensure date is in ISO format for the API }; await reportsApi.createReport(laudoData); // You can use a toast notification here for better user feedback // toast({ title: "Laudo criado com sucesso!" }); router.push(`/doctor/medicos/${patientId}/laudos`); } catch (error: any) { console.error("Failed to create laudo", error); // You can use a toast notification for errors // toast({ title: "Erro ao criar laudo", description: error.message, variant: "destructive" }); } finally { setIsSubmitting(false); } }; return (
Criar Novo Laudo