import { Component, Input, AfterContentChecked } from '@angular/core';

@Component({
    imports: [],
    selector: 'app-history-table',
    templateUrl: './history-table.component.html',
    styleUrls: [],
})
export class HistoryTableComponent implements AfterContentChecked {
    public seriesColTotal: number[] = [];
    public grandTotal: number = 0;

    constructor() {}

    @Input() headers: string[] = [];
    @Input() names: string[] = [];
    @Input() values: number[] = [];
    @Input() rows: Array<Array<number>> = [];
    @Input() colors: string[] = [];

    /*ngOnChanges() {
        //this._loadHistoryTable();
        //console.log('onChanges');
    }*/

    ngAfterContentChecked() {
        if (this.rows !== undefined && this.rows.length) {
            this._loadHistoryTable();
        }
    }

    private _loadHistoryTable(): void {
        const values: Array<number> = this.rows[0].map(() => 0);
        this.seriesColTotal = [...values];
        this.rows.forEach((row: number[], rowIndex: number) => {
            row.forEach((value: number, index: number) => {
                this.seriesColTotal[index] = this.seriesColTotal[index] + this.rows[rowIndex][index];
            });
            this.grandTotal = this.seriesColTotal.reduce((partialSum, a) => partialSum + a, 0);
        });
    }
}
