import { Component, OnInit } from '@angular/core';
import {LoanModel} from '../models/loan.model';
import {LoanSingleService} from '../service/loan.single.service';
import {AuthService} from '../service/auth.service';
import {SessionService} from '../service/session.service';
import {AppConfig} from '../app.config';

@Component({
  selector: 'app-client-loan-list',
  templateUrl: './client-loan-list.component.html',
  styleUrls: ['./client-loan-list.component.scss']
})
export class ClientLoanListComponent implements OnInit {
  Title: string;
  welcomeMessage: string;
  user: string;

  recentLoan: LoanModel;
  Item: string;
  Amount: number;
  Status: string;
  public Loans: LoanModel[] = [];

  constructor(private ls: LoanSingleService, private cfg: AppConfig, private ss: SessionService) { }

  ngOnInit(): void {
    this.ls.getLoanByClient(this.ss.loggedIn.User.Id).subscribe(
      resp => {
        this.Loans = [];
        resp.forEach( v => {
           const l = new LoanModel(v);
           l.Response = v;
           this.Loans.push(l);
        });
        if ( this.Loans.length > 0 ) {
          this.recentLoan = this.Loans[0];
        }
      }
    );
    this.clientHome();
  }

  public  clientHome(): void{
    this.Title = 'Apply new Loans';
    this.welcomeMessage = `SuperCredit is one of the fastest growing non-bank lender in Australia helping Australians to achieve their financial goals.`;
    this.user = 'your friendly team';
  }

  public goodDate(d: Date): boolean {
    const n = new Date();
    return d.getFullYear() + 10 > n.getFullYear(); // shouldn't be 10 years old.
  }

  private photoURL(peopleId: any): string {
    const url = this.cfg.getUrl('avatar/') + peopleId;
    return 'url("' + url + '")';
  }
}
