11import { Component , Input , Output , EventEmitter , OnInit , ViewChild , forwardRef , OnDestroy } from '@angular/core' ;
2- import { FormBuilder , FormGroup , AbstractControl , Validators , ControlValueAccessor , NG_VALUE_ACCESSOR , ValidatorFn } from '@angular/forms' ;
2+ import { FormBuilder , FormGroup , AbstractControl , Validators , ControlValueAccessor , NG_VALUE_ACCESSOR , ValidatorFn , NG_VALIDATORS , Validator , ValidationErrors } from '@angular/forms' ;
33import { PatientsService } from '@services/patients.service' ;
44import { Patient } from '@interfaces/patients' ;
55import { ThemePalette } from '@angular/material/core' ;
@@ -47,10 +47,15 @@ function validDateValidator(): ValidatorFn {
4747 provide : NG_VALUE_ACCESSOR ,
4848 useExisting : forwardRef ( ( ) => PatientFormComponent ) ,
4949 multi : true
50+ } ,
51+ {
52+ provide : NG_VALIDATORS ,
53+ useExisting : forwardRef ( ( ) => PatientFormComponent ) ,
54+ multi : true
5055 }
5156 ]
5257} )
53- export class PatientFormComponent implements OnInit , OnDestroy , ControlValueAccessor {
58+ export class PatientFormComponent implements OnInit , OnDestroy , ControlValueAccessor , Validator {
5459 @Input ( ) showObraSocial = false ;
5560 @Input ( ) appearance = 'fill' ;
5661 @Input ( ) layout = 'row' ; // 'row' or 'column'
@@ -79,6 +84,7 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
7984
8085 private onChange = ( value : any ) => { } ;
8186 private onTouched = ( ) => { } ;
87+ private onValidatorChange = ( ) => { } ;
8288 private subscriptions : Subscription = new Subscription ( ) ;
8389 private osRequestSubscription : Subscription | null = null ;
8490 private lastOsRequestKey = '' ;
@@ -135,6 +141,12 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
135141 }
136142 } ) ;
137143
144+ this . patientForm . statusChanges . subscribe ( ( ) => {
145+ if ( this . onValidatorChange ) {
146+ this . onValidatorChange ( ) ;
147+ }
148+ } ) ;
149+
138150 // Suscribirse a cambios en otraOS si está habilitado
139151 if ( this . showObraSocial ) {
140152 this . patientForm . get ( 'otraOS' ) ?. valueChanges . subscribe ( ( checked ) => {
@@ -386,7 +398,6 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
386398 this . patientFirstName . disable ( { onlySelf : true , emitEvent : false } ) ;
387399 this . patientNombreAutopercibido . disable ( { onlySelf : true , emitEvent : false } ) ;
388400 this . patientSex . disable ( { onlySelf : true , emitEvent : false } ) ;
389- this . patientFechaNac . disable ( { onlySelf : true , emitEvent : false } ) ;
390401 }
391402
392403 private enableFields ( ) : void {
@@ -408,13 +419,18 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
408419 this . patientSex . setValue ( patient . sex ) ;
409420
410421 // Configurar fechaNac según el ámbito y si el paciente tiene idMPI
411- this . showFechaNac = this . ambito === 'publico' && ! patient . idMPI ;
422+ this . showFechaNac = this . ambito === 'publico' ;
412423 this . updateFechaNacValidators ( ) ;
413424 this . patientFechaNac . setValue ( patient . fechaNac ) ;
414425
415426 // Deshabilitar campos después de autocompletar
416427 this . disableFields ( ) ;
417428
429+ // Desabilitar fecha de nacimiento para pacientes que ya tienen fecha registrada, independientemente del ámbito
430+ if ( patient . fechaNac ) {
431+ this . patientFechaNac . disable ( { onlySelf : true , emitEvent : false } ) ;
432+ }
433+
418434 // Cargar la obra social cuando se selecciona un paciente
419435 if ( this . showObraSocial && patient . dni ) {
420436 // Habilitar el campo otraOS para permitir cambiar la obra social
@@ -509,6 +525,10 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
509525 } else {
510526 this . patientForm . reset ( ) ;
511527 }
528+
529+ if ( this . onValidatorChange ) {
530+ this . onValidatorChange ( ) ;
531+ }
512532 }
513533
514534 registerOnChange ( fn : any ) : void {
@@ -519,12 +539,24 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
519539 this . onTouched = fn ;
520540 }
521541
542+ validate ( _ : AbstractControl ) : ValidationErrors | null {
543+ return this . patientForm && this . patientForm . valid ? null : { patientFormInvalid : true } ;
544+ }
545+
546+ registerOnValidatorChange ( fn : ( ) => void ) : void {
547+ this . onValidatorChange = fn ;
548+ }
549+
522550 setDisabledState ( isDisabled : boolean ) : void {
523551 if ( isDisabled ) {
524552 this . patientForm . disable ( ) ;
525553 } else {
526554 this . patientForm . enable ( ) ;
527555 }
556+
557+ if ( this . onValidatorChange ) {
558+ this . onValidatorChange ( ) ;
559+ }
528560 }
529561
530562 // Getters para acceso a controles
@@ -603,6 +635,10 @@ export class PatientFormComponent implements OnInit, OnDestroy, ControlValueAcce
603635 fechaNacControl . setValidators ( [ validDateValidator ( ) ] ) ;
604636 }
605637 fechaNacControl . updateValueAndValidity ( ) ;
638+
639+ if ( this . onValidatorChange ) {
640+ this . onValidatorChange ( ) ;
641+ }
606642 }
607643
608644 markAllFieldsTouched ( ) : void {
0 commit comments