import { Component , Input , Output , EventEmitter } from '@angular/core' ; import { DataService } from '../../../services/data.service' ; @ Component ( { selector : 'budgets-list' , templateUrl : 'budgets-list.component.html' , styleUrls : [ 'budgets-list.component.scss' ] } ) export class BudgetsListComponent { @ Input ( ) set profile ( profile ) { if ( profile ) this . loadBudgets ( profile ) ; } ; @ Input ( ) type : string ; @ Input ( ) set selected ( year ) { if ( year !== this . selectedYear ) this . selectBudget ( year ) ; } @ Output ( ) select : EventEmitter < any > = new EventEmitter ( ) ; budgets : any [ ] = [ ] ; selectedYear : number ; currentBudget : any ; open : boolean ; maxAmount : number = 0 ; constructor ( private dataService ) { } loadBudgets ( profileId ) { this . dataService . getProfileBudgets ( profileId , { sort : "-year" } ) . then ( budgets => { this . budgets = budgets ; this . maxAmount = 0 ; this . budgets . forEach ( budget => this . maxAmount = Math . max ( this . maxAmount , this . getAmount ( budget ) , this . getBudgetAmount ( budget ) ) ) ; this . selectBudget ( this . selectedYear ) ; } ) ; } getBudgetAmount ( budget ) { switch ( this . type ) { case "expenditures" : return budget . budgetExpenditureAmount ; case "income" : return budget . budgetIncomeAmount ; default : return 0 ; } } getAmount ( budget ) { switch ( this . type ) { case "expenditures" : return budget . expenditureAmount ; case "income" : return budget . incomeAmount ; default : return 0 ; } } getType ( ) { switch ( this . type ) { case "expenditures" : return "výdaje" ; case "income" : return "příjmy" ; default : return "" ; } } selectBudget ( year ) { this . selectedYear = year ; if ( year && this . budgets ) { let budget ; this . budgets . some ( item => { if ( Number ( item . year ) === year ) { budget = item ; return true ; } return false ; } ) ; if ( budget ) { this . currentBudget = budget ; this . select . emit ( this . currentBudget ) ; return ; } } if ( this . budgets [ 0 ] ) { this . currentBudget = this . budgets [ 0 ] ; this . select . emit ( this . currentBudget ) ; return ; } this . currentBudget = null ; } }