/** Angular Imports */ import { Component , OnChanges , OnInit , Input , ViewChild } from '@angular/core' ; import { ActivatedRoute } from '@angular/router' ; import { MatTable } from '@angular/material/table' ; import { MatDialog } from '@angular/material/dialog' ; import { DatePipe } from '@angular/common' ; /** Custom Dialogs */ import { FormDialogComponent } from 'app/shared/form-dialog/form-dialog.component' ; import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-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' ; import { SelectBase } from 'app/shared/form-dialog/formfield/model/select-base' ; import { CheckboxBase } from 'app/shared/form-dialog/formfield/model/checkbox-base' ; import { DatepickerBase } from 'app/shared/form-dialog/formfield/model/datepicker-base' ; /** Custom Services */ import { SavingsService } from 'app/savings/savings.service' ; import { SettingsService } from 'app/settings/settings.service' ; /**
 * Savings Account Multi Row Data Tables
 */ @ Component ( { selector : 'mifosx-multi-row' , templateUrl : './multi-row.component.html' , styleUrls : [ './multi-row.component.scss' ] } ) export class MultiRowComponent implements OnInit , OnChanges { /** Data Object */ @ Input ( ) dataObject : any ; /** Data Table Name */ datatableName : string ; /** Data Table Columns */ datatableColumns : string [ ] = [ ] ; /** Data Table Data */ datatableData : any ; /** Account Id */ accountId : string ; /** Toggle button visibility */ showDeleteBotton : boolean ; /** Data Table Reference */ @ ViewChild ( 'dataTable' , { static : true } ) dataTableRef : MatTable < Element > ; param { ActivatedRoute } route param { DatePipe } datePipe param { SavingsService } savingsService param { MatDialog } dialog param { SettingsService } settingsService constructor ( private route , private datePipe , private savingsService , private dialog , private settingsService ) { this . accountId = this . route . parent . parent . snapshot . paramMap . get ( 'fixedDepositAccountId' ) ; } /**
     * Updates related variables on changes to dataObject.
     */ ngOnChanges ( ) { this . datatableColumns = this . dataObject . columnHeaders . map ( ( columnHeader ) => { return columnHeader . columnName ; } ) ; this . datatableData = this . dataObject . data ; this . showDeleteBotton = this . datatableData [ 0 ] ? true : false ; } /**
     * Fetches data table name from route params.
     * subscription is required due to asynchronicity.
     */ ngOnInit ( ) { this . route . params . subscribe ( ( routeParams ) => { this . datatableName = routeParams . datatableName ; } ) ; } /**
     * Adds a new row to the given multi row data table.
     */ add ( ) { let dataTableEntryObject = { locale : this . settingsService . language . code } ; const dateTransformColumns = [ ] ; const columns = this . dataObject . columnHeaders . filter ( ( column ) => { return ( ( column . columnName !== 'id' ) && ( column . columnName !== 'savings_account_id' ) ) ; } ) ; const formfields = columns . map ( ( column ) => { switch ( column . columnDisplayType ) { case 'INTEGER' : case 'STRING' : case 'DECIMAL' : case 'TEXT' : return new InputBase ( { controlName : column . columnName , label : column . columnName , value : '' , type : ( column . columnDisplayType === 'INTEGER' || column . columnDisplayType === 'DECIMAL' ) ? 'number' : 'text' , required : ( column . isColumnNullable ) ? false : true } ) ; case 'BOOLEAN' : return new CheckboxBase ( { controlName : column . columnName , label : column . columnName , value : '' , type : 'checkbox' , required : ( column . isColumnNullable ) ? false : true } ) ; case 'CODELOOKUP' : return new SelectBase ( { controlName : column . columnName , label : column . columnName , value : '' , options : { label : 'value' , value : 'id' , data : column . columnValues } , required : ( column . isColumnNullable ) ? false : true } ) ; case 'DATE' : { dateTransformColumns . push ( column . columnName ) ; dataTableEntryObject . dateFormat = this . settingsService . dateFormat ; return new DatepickerBase ( { controlName : column . columnName , label : column . columnName , value : '' , type : 'date' , required : ( column . isColumnNullable ) ? false : true } ) ; } case 'DATETIME' : { dateTransformColumns . push ( column . columnName ) ; dataTableEntryObject . dateFormat = 'yyyy-MM-dd HH:mm' ; return new DatepickerBase ( { controlName : column . columnName , label : column . columnName , value : '' , type : 'datetime-local' , required : ( column . isColumnNullable ) ? false : true } ) ; } } } ) ; const data = { title : 'Add ' + this . datatableName , formfields : formfields } ; const addDialogRef = this . dialog . open ( FormDialogComponent , { data } ) ; addDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . data ) { dateTransformColumns . forEach ( ( column ) => { response . data . value [ column ] = this . datePipe . transform ( response . data . value [ column ] , dataTableEntryObject . dateFormat ) ; } ) ; dataTableEntryObject = { ... response . data . value , ... dataTableEntryObject } ; this . savingsService . addSavingsDatatableEntry ( this . accountId , this . datatableName , dataTableEntryObject ) . subscribe ( ( ) => { this . savingsService . getSavingsDatatable ( this . accountId , this . datatableName ) . subscribe ( ( dataObject ) => { this . datatableData = dataObject . data ; this . dataTableRef . renderRows ( ) ; } ) ; } ) ; } } ) ; } /**
     * Deletes all rows of the given multi row data table.
     */ delete ( ) { const deleteDataTableDialogRef = this . dialog . open ( DeleteDialogComponent , { data : { deleteContext : `the contents of ${ this . datatableName }` } } ) ; deleteDataTableDialogRef . afterClosed ( ) . subscribe ( ( response ) => { if ( response . delete ) { this . savingsService . deleteDatatableContent ( this . accountId , this . datatableName ) . subscribe ( ( ) => { this . savingsService . getSavingsDatatable ( this . accountId , this . datatableName ) . subscribe ( ( dataObject ) => { this . datatableData = dataObject . data ; this . showDeleteBotton = false ; this . dataTableRef . renderRows ( ) ; } ) ; } ) ; } } ) ; } }