import { Component, inject } from '@angular/core'; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; import { MatDividerModule } from '@angular/material/divider'; import { MatCheckboxModule } from '@angular/material/checkbox'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { ApiErrorHandlerService } from '../../core/services/common/api-error-handler.service'; import { NavigationService } from '../../core/services/common/navigation.service'; import { NotificationService } from '../../core/services/common/notification.service'; import { StorageService } from '../../core/services/common/storage.service'; import { CarnetService } from '../../core/services/carnet/carnet.service'; import { Fees } from '../../core/models/carnet/fee'; import { finalize } from 'rxjs'; import { ShippingService } from '../../core/services/carnet/shipping.service'; import { Shipping } from '../../core/models/carnet/shipping'; @Component({ selector: 'app-terms-conditions', imports: [MatCardModule, MatButtonModule, MatDividerModule, MatCheckboxModule, CommonModule, FormsModule], templateUrl: './terms-conditions.component.html', styleUrl: './terms-conditions.component.scss' }) export class TermsConditionsComponent { isLoading: boolean = false; currentDate = new Date(); hasReadAllTerms = false; currentApplicationDetails: { headerid: number, applicationName: string, applicationType: string } | null = null; changeInProgress: boolean = false; estimatedFees: Fees = {}; needCheckOut: boolean = false; bondPremiumLabel: string = 'Bond Premium'; total: number = 0; private notificationService = inject(NotificationService); private errorHandler = inject(ApiErrorHandlerService); private storageService = inject(StorageService); private navigationService = inject(NavigationService); private carnetService = inject(CarnetService); private shippingService = inject(ShippingService); constructor() { this.currentApplicationDetails = this.storageService.get<{ headerid: number, applicationName: string, applicationType: string }>('currentapplication') } ngOnInit(): void { if (this.currentApplicationDetails?.headerid) { this.carnetService.getEstimatedFees(this.currentApplicationDetails?.headerid).subscribe({ next: (data: Fees) => { if (data) { this.estimatedFees = data; if (this.estimatedFees.securityType === 'B') { this.bondPremiumLabel = 'Bond Premium'; } else if (this.estimatedFees.securityType === 'C') { this.bondPremiumLabel = 'Cash Deposit'; } else if (this.estimatedFees.securityType === 'G') { this.bondPremiumLabel = 'Refundable claim deposit'; } this.calculateTotal(); } }, error: (error: any) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to get estimated fees'); this.notificationService.showError(errorMessage); console.error('Error getting estimated fees:', error); } }); this.getPaymentType(this.currentApplicationDetails?.headerid); } } onAccept(): void { if (this.needCheckOut) { this.navigationService.navigate(['checkout']); } else { this.changeInProgress = true; this.carnetService.submitApplication(this.currentApplicationDetails?.headerid!).pipe(finalize(() => { this.changeInProgress = false; })).subscribe({ next: () => { this.notificationService.showSuccess('Application submitted successfully'); this.storageService.removeItem('currentapplication'); this.navigationService.navigate(["home"]); }, error: (error) => { let errorMessage = this.errorHandler.handleApiError(error, 'Failed to submit application'); this.notificationService.showError(errorMessage); console.error('Error submitting the application', error); } }); } } onDecline(): void { this.storageService.removeItem('currentapplication'); let route: string = 'edit-carnet'; if (this.currentApplicationDetails?.applicationType === 'duplicate') { route = 'duplicate-carnet'; } else if (this.currentApplicationDetails?.applicationType === 'extend') { route = 'extend-carnet'; } else if (this.currentApplicationDetails?.applicationType === 'additional') { route = 'additional-sets'; } this.navigationService.navigate([route, this.currentApplicationDetails?.headerid], { state: { isEditMode: true }, queryParams: { applicationname: this.currentApplicationDetails?.applicationName, return: 'shipping' } }) } getPaymentType(headerid: number): void { this.isLoading = true; this.shippingService.getShippingData(headerid).pipe( finalize(() => { this.isLoading = false; }) ).subscribe({ next: (shippingData: Shipping) => { if (!shippingData) { // do nothing if empty return; } this.needCheckOut = shippingData.paymentMethod !== 'ACH' && shippingData.paymentMethod !== 'INV'; }, error: (error: any) => { console.error('Error loading shipping data', error); this.errorHandler.handleApiError(error, 'Failed to load shipping data'); } }); } calculateTotal(): void { const validFees = Object.values(this.estimatedFees).filter( (fee): fee is number => typeof fee === 'number' ); this.total = validFees.reduce((total, fee) => total + fee, 0); } }