/** * Copyright since 2025 Mifos Initiative * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { Component, Input, OnChanges, SimpleChanges, ViewChild } from '@angular/core'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; import { NomineeFormComponent } from 'app/shared/nominee-form/nominee-form.component'; import { DormancyAlertPanelComponent } from 'app/shared/dormancy-alert-panel/dormancy-alert-panel.component'; import { MatSlideToggle } from '@angular/material/slide-toggle'; import { FormsModule } from '@angular/forms'; @Component({ selector: 'mifosx-fixed-deposit-account-currency-step', templateUrl: './fixed-deposit-account-currency-step.component.html', styleUrls: ['./fixed-deposit-account-currency-step.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS, NomineeFormComponent, DormancyAlertPanelComponent, MatSlideToggle, FormsModule ] }) export class FixedDepositAccountCurrencyStepComponent implements OnChanges { @Input() fixedDepositsAccountProductTemplate: any; @Input() primaryClientId: number | null = null; @ViewChild(NomineeFormComponent) nomineeFormComp: NomineeFormComponent; nominees: any[] = []; pendingNominee: any = null; showNomineeForm = false; nomineeError = ''; autoDebitFacility = false; nominationFacility = false; ngOnChanges(changes: SimpleChanges): void { if (changes['fixedDepositsAccountProductTemplate']?.currentValue) { const pt = changes['fixedDepositsAccountProductTemplate'].currentValue; this.autoDebitFacility = !!pt.autoDebitFacility; this.nominationFacility = !!pt.nominationFacility; } } get totalShare(): number { return this.nominees.reduce((sum, n) => sum + (+n.percentageShare || 0), 0); } get remainingShare(): number { return 100 - this.totalShare; } get canAddMore(): boolean { return this.totalShare < 100; } onNomineeChange(event: any): void { this.pendingNominee = event; this.nomineeError = ''; } addNominee(): void { const data = this.nomineeFormComp?.currentNomineeData ?? this.pendingNominee; if (!data?.nomineeClientId) { this.nomineeError = 'Please select a nominee before adding.'; return; } if (this.nominees.some((n) => n.nomineeClientId === data.nomineeClientId)) { this.nomineeError = 'This person is already added as a nominee.'; return; } const newTotal = this.totalShare + (+data.percentageShare || 0); if (newTotal > 100) { this.nomineeError = `Total share cannot exceed 100%. Remaining: ${this.remainingShare}%`; return; } this.nominees.push({ ...data }); this.pendingNominee = null; this.showNomineeForm = false; this.nomineeError = ''; } removeNominee(index: number): void { this.nominees.splice(index, 1); } get currencyStepData() { return { autoDebitFacility: this.autoDebitFacility, nominationFacility: this.nominationFacility }; } }