/** Angular Imports */ import { Component , OnInit , OnChanges , Input } from '@angular/core' ; import { MatDialog } from '@angular/material/dialog' ; import { FormControl } from '@angular/forms' ; /** Custom Dialogs */ import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component' ; import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component' ; /** Custom Models */ import { FormfieldBase } from 'app/shared/form-dialog/formfield/model/formfield-base' ; import { InputBase } from 'app/shared/form-dialog/formfield/model/input-base' ; /**
 * Shares Account Charges Step
 */ @ Component ( { selector : 'mifosx-shares-account-charges-step' , templateUrl : './shares-account-charges-step.component.html' , styleUrls : [ './shares-account-charges-step.component.scss' ] } ) export class SharesAccountChargesStepComponent implements OnInit , OnChanges { /** Shares Account Product Template */ @ Input ( ) sharesAccountProductTemplate : any ; /** Shares Account Template */ @ Input ( ) sharesAccountTemplate : any ; /** Currency Code */ @ Input ( ) currencyCode : FormControl ; /** Charge Data */ chargeData : any = [ ] ; /** Charges Data Source */ chargesDataSource : { } [ ] = [ ] ; /** Component is pristine if there has been no changes by user interaction */ pristine = true ; /** For Edit Shares Account Form */ isChargesPatched = false ; /** Display columns for charges table */ displayedColumns : string [ ] = [ 'name' , 'chargeCalculationType' , 'amount' , 'chargeTimeType' , 'action' ] ; param { MatDialog } dialog constructor ( public dialog ) { } ngOnInit ( ) { this . currencyCode . valueChanges . subscribe ( ( ) => { if ( ! this . isChargesPatched && this . sharesAccountTemplate . charges ) { this . chargesDataSource = this . sharesAccountTemplate . charges ; this . isChargesPatched = true ; } else { this . chargesDataSource = [ ] ; } } ) ; } ngOnChanges ( ) { if ( this . sharesAccountProductTemplate ) { this . chargeData = this . sharesAccountProductTemplate . charges ; } } param { any } charge addCharge ( charge ) { this . chargesDataSource = this . chargesDataSource . concat ( [ charge . value ] ) ; charge . value = '' ; this . pristine = false ; } param { any } charge editCharge ( charge ) { const formfields = [ new InputBase ( { controlName : 'amount' , label : 'Amount' , value : charge . amount || charge . amountOrPercentage , type : 'number' , required : false , } ) , ] ; const data = { title : 'Edit Charge' , layout : { addButtonText : 'Confirm' } , formfields : formfields } ; const editNoteDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; editNoteDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { const newCharge = { ... charge , amount : response . data . value . amount } ; this . chargesDataSource . splice ( this . chargesDataSource . indexOf ( charge ) , 1 , newCharge ) ; this . chargesDataSource = this . chargesDataSource . concat ( [ ] ) ; } } ) ; this . pristine = false ; } param { any } charge deleteCharge ( charge ) { const deleteChargeDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `charge ${ charge . name }` } } ) ; deleteChargeDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . chargesDataSource . splice ( this . chargesDataSource . indexOf ( charge ) , 1 ) ; this . chargesDataSource = this . chargesDataSource . concat ( [ ] ) ; } } ) ; this . pristine = false ; } /**
     * Returns shares account charges
     */ get sharesAccountCharges ( ) { return { charges : this . chargesDataSource } ; } }