"use client" import { useState } from "react" 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 { Heart, AlertCircle } from "lucide-react" interface MedicalInfoSectionProps { formData: { phone?: string gender?: string address?: string emergencyContact?: string medicalHistory?: string allergies?: string currentMedications?: string } onInputChange: (e: React.ChangeEvent) => void userRole?: string } interface ValidationErrors { phone?: string emergencyContact?: string address?: string medicalHistory?: string allergies?: string currentMedications?: string } // Exportar función de validación para uso en el hook export const validateMedicalInfo = (formData: MedicalInfoSectionProps['formData']): { isValid: boolean; errors: ValidationErrors } => { const errors: ValidationErrors = {} // Validar teléfono if (formData.phone) { const phoneRegex = /^[\d\s()+-]+$/ if (!phoneRegex.test(formData.phone)) { errors.phone = "Solo se permiten números, espacios, paréntesis, guiones y +" } else { const digits = formData.phone.replace(/\D/g, '') if (digits.length > 0 && digits.length < 10) { errors.phone = "El número debe tener al menos 9 dígitos" } } if (formData.phone.length > 20) { errors.phone = "Máximo 20 caracteres" } } // Validar contacto de emergencia if (formData.emergencyContact && formData.emergencyContact.length > 100) { errors.emergencyContact = "Máximo 100 caracteres" } // Validar dirección if (formData.address && formData.address.length > 200) { errors.address = "Máximo 200 caracteres" } // Validar campos de texto if (formData.medicalHistory && formData.medicalHistory.length > 1000) { errors.medicalHistory = "Máximo 1000 caracteres" } if (formData.allergies && formData.allergies.length > 500) { errors.allergies = "Máximo 500 caracteres" } if (formData.currentMedications && formData.currentMedications.length > 1000) { errors.currentMedications = "Máximo 1000 caracteres" } return { isValid: Object.keys(errors).length === 0, errors } } export default function MedicalInfoSection({ formData, onInputChange, userRole }: MedicalInfoSectionProps) { const [errors, setErrors] = useState({}) // Solo mostrar para pacientes if (userRole !== "PATIENT") { return null } // Validación de teléfono: solo números, espacios, paréntesis, guiones y + const validatePhone = (value: string): string | undefined => { if (!value) return undefined const phoneRegex = /^[\d\s()+-]+$/ if (!phoneRegex.test(value)) { return "Solo se permiten números, espacios, paréntesis, guiones y +" } // Verificar que tenga al menos 7 dígitos const digits = value.replace(/\D/g, '') if (digits.length > 0 && digits.length < 7) { return "El número debe tener al menos 7 dígitos" } if (value.length > 20) { return "Máximo 20 caracteres" } return undefined } // Validación de contacto de emergencia const validateEmergencyContact = (value: string): string | undefined => { if (!value) return undefined if (value.length > 100) { return "Máximo 100 caracteres" } return undefined } // Validación de dirección const validateAddress = (value: string): string | undefined => { if (!value) return undefined if (value.length > 200) { return "Máximo 200 caracteres" } return undefined } // Validación de campos de texto largos const validateTextArea = (value: string, maxLength: number): string | undefined => { if (!value) return undefined if (value.length > maxLength) { return `Máximo ${maxLength} caracteres` } return undefined } const handleValidatedChange = (e: React.ChangeEvent) => { const { name, value } = e.target let error: string | undefined // Validar según el campo switch (name) { case 'phone': error = validatePhone(value) break case 'emergencyContact': error = validateEmergencyContact(value) break case 'address': error = validateAddress(value) break case 'medicalHistory': error = validateTextArea(value, 1000) break case 'allergies': error = validateTextArea(value, 500) break case 'currentMedications': error = validateTextArea(value, 1000) break } // Actualizar errores setErrors(prev => ({ ...prev, [name]: error })) // Solo llamar onChange si no hay error crítico (permitir seguir escribiendo) onInputChange(e) } return ( Información Médica y Personal

Esta información ayudará al chatbot a brindarte recomendaciones más precisas

{/* Información Personal */}

Datos de Contacto

{errors.phone && (
{errors.phone}
)}
{errors.emergencyContact && (
{errors.emergencyContact}
)}

Ejemplo: María López - +57 300 123 4567

{errors.address && (
{errors.address}
)}
{/* Información Médica */}

Información Médica