import { Component, OnInit } from '@angular/core';
import {Observable} from 'rxjs';
import {TypeOfLoansModel} from '../models/type-of-loans.model';
import {HttpClient} from '@angular/common/http';
import {AuthService} from '../service/auth.service';
import {map} from 'rxjs/operators';

@Component({
  selector: 'app-chart-type-of-loans',
  templateUrl: './chart-type-of-loans.component.html',
  styleUrls: ['./chart-type-of-loans.component.scss']
})
export class ChartTypeOfLoansComponent implements OnInit {
  public autoFit = true;
  public data: Observable<TypeOfLoansModel[]>;

  constructor(private auth: AuthService, private http: HttpClient) { }

  ngOnInit(): void {
    this.data = this.getTypeOfLoans();
  }

  public labelContent(e: any): string {
    return e.category;
  }

  public getTypeOfLoans(): Observable<TypeOfLoansModel[]> {
    return this.http.get<TypeOfLoansModel[]>(this.auth.getUrl('chart/type-of-loans')).pipe(
      map(
        input => {
          const ret = [];
          input.forEach( (value, index) =>{
            ret.push({
              Kind: value.Kind + '  ' + (value.Share * 100).toFixed(2) + '%',
              Count: value.Count,
              Share: value.Share,
              Amount: value.Amount
            });          });
          return ret;
        }
      )
    );
  }

}
