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:
- Basic fee: {{estimatedFees.basicFee | currency}}
- Counterfoil Fee: {{estimatedFees.counterFoilFee | currency}}
- Continuation sheet fee:
{{estimatedFees.continuationSheetFee | currency}}
- Expedited fee: {{estimatedFees.expeditedFee | currency}}
- Shipping fee: {{estimatedFees.shippingFee | currency}}
- {{bondPremiumLabel}}: {{estimatedFees.bondPremium | currency}}
- Cargo Premium: {{estimatedFees.cargoPremium | currency}}
- LDI Premium: {{estimatedFees.ldiPremium | currency}}
Additional Charges:
- Security Amount: {{estimatedFees.securityAmount | currency}}
- Insured Value: {{estimatedFees.insuredValue | currency}}
Total: {{total | currency}}
`,
styles: [`
li {
margin-bottom: 8px;
}
.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 validFees = Object.values(this.estimatedFees).filter(
(fee): fee is number => typeof fee === 'number'
);
this.total = validFees.reduce((total, fee) => total + fee, 0);
}
onClose(): void {
this.dialogRef.close();
}
}