import { Component, inject, Inject } from '@angular/core'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { AngularMaterialModule } from '../../shared/module/angular-material.module'; import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { CarnetService } from '../../core/services/carnet/carnet.service'; import { Fees } from '../../core/models/carnet/fee'; import { ApiErrorHandlerService } from '../../core/services/common/api-error-handler.service'; import { NotificationService } from '../../core/services/common/notification.service'; @Component({ selector: 'app-fees-dialog', imports: [AngularMaterialModule, CommonModule, FormsModule], template: `

Fees and Charges

The following fees apply:



`, styles: [` li { margin-bottom: 8px; display: flex; justify-content: space-between; } .total { font-weight: bold; margin-top: 12px; } `] }) export class FeesDialogComponent { estimatedFees: Fees = {}; headerid: number | null = null; bondPremiumLabel: string = 'Bond Premium'; total: number = 0; private carnetService = inject(CarnetService); private notificationService = inject(NotificationService); private errorHandler = inject(ApiErrorHandlerService); constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: { headerid: any } ) { this.headerid = data.headerid; } ngOnInit(): void { if (this.headerid && this.headerid > 0) { this.carnetService.getEstimatedFees(this.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); } }); } } calculateTotal(): void { const excludedProperties = ['securityAmount', 'insuredValue', 'securityType']; const validFees = Object.entries(this.estimatedFees) .filter(([key]) => !excludedProperties.includes(key)) .map(([, value]) => value) .filter((fee): fee is number => typeof fee === 'number'); this.total = validFees.reduce((total, fee) => total + fee, 0); } onClose(): void { this.dialogRef.close(); } }