import { Component, EventEmitter, inject, Input, Output, ViewChild } from '@angular/core'; import { AngularMaterialModule } from '../../shared/module/angular-material.module'; import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms'; import { PhonePipe } from '../../shared/pipes/phone.pipe'; import { CommonModule } from '@angular/common'; import { MatPaginator, MatPaginatorIntl } from '@angular/material/paginator'; import { MatSort } from '@angular/material/sort'; import { MatTableDataSource } from '@angular/material/table'; import { UserPreferences } from '../../core/models/user-preference'; import { NotificationService } from '../../core/services/common/notification.service'; import { ApiErrorHandlerService } from '../../core/services/common/api-error-handler.service'; import { ContactService } from '../../core/services/holder/contact.service'; import { Contact } from '../../core/models/holder/contact'; import { CustomPaginator } from '../../shared/custom-paginator'; import { MatDialog } from '@angular/material/dialog'; import { ConfirmDialogComponent } from '../../shared/components/confirm-dialog/confirm-dialog.component'; import { NavigationService } from '../../core/services/common/navigation.service'; import { StorageService } from '../../core/services/common/storage.service'; import { finalize } from 'rxjs'; @Component({ selector: 'app-contacts', imports: [AngularMaterialModule, ReactiveFormsModule, PhonePipe, CommonModule], templateUrl: './contacts.component.html', styleUrl: './contacts.component.scss', providers: [{ provide: MatPaginatorIntl, useClass: CustomPaginator }], }) export class ContactsComponent { @Input() isEditMode: boolean = false; @Input() holderid: number = 0; @Input() userPreferences: UserPreferences = {}; @Output() hasContacts = new EventEmitter(); @ViewChild(MatPaginator) paginator!: MatPaginator; @ViewChild(MatSort) sort!: MatSort; displayedColumns: string[] = ['firstName', 'lastName', 'title', 'phone', 'mobile', 'email', 'actions']; dataSource = new MatTableDataSource(); contactForm: FormGroup; isEditing = false; currentContactId: number | null = null; isLoading = false; changeInProgress = false; showForm = false; showInactiveContacts = false; contacts: Contact[] = []; currentApplicationDetails: { headerid: number, applicationName: string } | null = null; contactReadOnlyFields: any = { lastChangedDate: null, lastChangedBy: null, isInactive: null, inactivatedDate: null }; private fb = inject(FormBuilder); private dialog = inject(MatDialog); private contactService = inject(ContactService); private notificationService = inject(NotificationService); private errorHandler = inject(ApiErrorHandlerService); private navigationService = inject(NavigationService); private storageService = inject(StorageService); constructor() { this.contactForm = this.fb.group({ firstName: ['', [Validators.required, Validators.maxLength(50)]], lastName: ['', [Validators.required, Validators.maxLength(50)]], middleInitial: ['', [Validators.maxLength(1)]], title: ['', [Validators.required, Validators.maxLength(100)]], phone: ['', [Validators.required, Validators.pattern(/^[0-9\-\(\)]{10,15}$/)]], mobile: ['', [Validators.required, Validators.pattern(/^[0-9\-\(\)]{10,15}$/)]], fax: ['', [Validators.pattern(/^[0-9\-\(\)]{10,15}$/)]], email: ['', [Validators.required, Validators.email, Validators.maxLength(100)]], }); this.currentApplicationDetails = this.storageService.get<{ headerid: number, applicationName: string }>('currentapplication') } ngOnInit(): void { if (this.holderid > 0) { this.loadContacts(); } } ngAfterViewInit() { this.dataSource.paginator = this.paginator; this.dataSource.sort = this.sort; } loadContacts(): void { this.isLoading = true; this.contactService.getContactsById(this.holderid).pipe(finalize(() => { this.isLoading = false; })).subscribe({ next: (contacts: Contact[]) => { this.contacts = contacts; this.renderContacts(); }, error: (error: any) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to load contacts'); this.notificationService.showError(errorMessage); console.error('Error loading contacts:', error); } }); } addNewContact(): void { this.showForm = true; this.isEditing = false; this.currentContactId = null; this.contactForm.reset(); } editContact(contact: Contact): void { this.showForm = true; this.isEditing = true; this.currentContactId = contact.holdercontactid; this.contactForm.patchValue({ firstName: contact.firstName, lastName: contact.lastName, middleInitial: contact.middleInitial, title: contact.title, phone: contact.phone, mobile: contact.mobile, fax: contact.fax, email: contact.email }); this.contactReadOnlyFields.lastChangedDate = contact.lastUpdatedDate ?? contact.dateCreated; this.contactReadOnlyFields.lastChangedBy = contact.lastUpdatedBy ?? contact.createdBy; this.contactReadOnlyFields.isInactive = contact.isInactive; this.contactReadOnlyFields.inactivatedDate = contact.inactivatedDate; } saveContact(): void { if (this.contactForm.invalid && !(this.holderid > 0)) { this.contactForm.markAllAsTouched(); return; } const contactData: Contact = this.contactForm.value; const saveObservable = this.isEditing && (this.currentContactId! > 0) ? this.contactService.updateContact(this.currentContactId!, contactData) : this.contactService.createContact(this.holderid, contactData); this.changeInProgress = true; saveObservable.pipe(finalize(() => { this.changeInProgress = false; })).subscribe({ next: () => { this.notificationService.showSuccess(`Contact ${this.isEditing ? 'updated' : 'added'} successfully`); this.loadContacts(); this.cancelEdit(); this.hasContacts.emit(true); }, error: (error) => { let errorMessage = this.errorHandler.handleApiError(error, `Failed to ${this.isEditing ? 'update' : 'add'} contact`); this.notificationService.showError(errorMessage); console.error('Error saving contact:', error); } }); } toggleShowInactiveContacts(): void { this.showInactiveContacts = !this.showInactiveContacts; this.renderContacts(); } renderContacts(): void { if (this.showInactiveContacts) { this.dataSource.data = this.contacts; } else { this.dataSource.data = this.contacts.filter(contact => !contact.isInactive); } } cancelEdit(): void { this.showForm = false; this.isEditing = false; this.currentContactId = null; this.contactForm.reset(); } inactivateContact(contactId: number): void { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '350px', data: { title: 'Confirm Inactivation', message: 'Are you sure you want to inactivate this contact?', confirmText: 'Yes', cancelText: 'Cancel' } }); dialogRef.afterClosed().subscribe(result => { if (result) { this.contactService.inactivateHolderContact(contactId).subscribe({ next: () => { this.notificationService.showSuccess('Contact inactivated successfully'); this.loadContacts(); }, error: (error) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to inactivate contact'); this.notificationService.showError(errorMessage); console.error('Error inactivating contact:', error); } }); } }); } reactivateContact(contactId: number): void { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '350px', data: { title: 'Confirm Reactivation', message: 'Are you sure you want to reactivate this contact?', confirmText: 'Yes', cancelText: 'Cancel' } }); dialogRef.afterClosed().subscribe(result => { if (result) { this.contactService.reactivateHolderContact(contactId).subscribe({ next: () => { this.notificationService.showSuccess('Contact reactivated successfully'); this.loadContacts(); }, error: (error) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to reactivate contact'); this.notificationService.showError(errorMessage); console.error('Error reactivating contact:', error); } }); } }); } goBackToCarnetApplication(): void { this.storageService.removeItem('currentapplication') this.navigationService.navigate(["edit-carnet", this.currentApplicationDetails?.headerid], { state: { isEditMode: true }, queryParams: { applicationname: this.currentApplicationDetails?.applicationName, return: 'holder' } }) } }