import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {UploadAnalysisModel} from '../../models/upload.analysis.model'; import {PayInModel} from '../../models/pay-in.model'; import {CellCloseEvent} from '@progress/kendo-angular-grid'; import {PayInConnectiveModel} from '../../models/pay-in-connective.model'; import {DataResult, GroupDescriptor, process} from '@progress/kendo-data-query'; @Component({ selector: 'app-lender-connective-income', templateUrl: './lender-connective-income.component.html', styleUrls: ['./lender-connective-income.component.scss'] }) export class LenderConnectiveIncomeComponent implements OnInit { private Lender = 'Connective'; @Input() public analysis = new UploadAnalysisModel({}); @Input() newPayInUpdate: PayInModel; @Output() Selected: EventEmitter = new EventEmitter(); public data: PayInConnectiveModel[] = []; private payInCandidates: PayInModel[] = []; public groups: GroupDescriptor[] = [{ field: 'Lender' }]; public gridView: DataResult; constructor() { } ngOnInit(): void { this.analysis.Connective.forEach(v => { v.matchedPayIn = this.matchPayIn(v); this.data.push(new PayInConnectiveModel(v)); }); this.gridView = process(this.data, { group: this.groups }); } ngOnChanges(changes): void { // Todo: build super class for common features if (changes.newPayInUpdate){ const piNew = changes.newPayInUpdate.currentValue as PayInModel; this.data.forEach(v => { // remove old matches if (v.matchedPayIn !== -1 && v.matchedPayIn.Id === piNew.Id){ v.matchedPayIn = -1; } // add new matches if ( this.isMatch(v, changes.newPayInUpdate.currentValue) ){ v.matchedPayIn = changes.newPayInUpdate.currentValue; } }); } } public onCellClick(event: CellCloseEvent): void{ this.Selected.emit(event.dataItem.asPayIn()); } private matchPayIn(v: PayInConnectiveModel): PayInModel | -1 { const idx = this.payInCandidates.findIndex( pi => this.isMatch(v, pi) ); if ( idx === -1 ) { return -1; } return this.payInCandidates[idx]; } private isMatch( v: PayInConnectiveModel, pi: PayInModel): boolean { if ( v === null || pi === null ){ return false; } return v.isMatch(pi); } @Input() set payIn(pis: PayInModel[]) { this.payInCandidates = []; pis.forEach( v => this.payInCandidates.push(new PayInModel(v))); this.data.forEach( v => { v.matchedPayIn = this.matchPayIn(v); }); } get PayIn(): PayInModel[] { return this.payInCandidates; } }