import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {LoanModel} from '../models/loan.model';
import {LoanSingleService} from '../service/loan.single.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-loan-card',
  templateUrl: './loan-card.component.html',
  styleUrls: ['./loan-card.component.scss']
})
export class LoanCardComponent implements OnInit {
  @Input() LoanId = '';
  @Input() ShowCloseButton = false;
  @Input() public Loan: LoanModel = new LoanModel({});
  @Output() Selected: EventEmitter<LoanModel> = new EventEmitter<LoanModel>();
  @Output() Closed: EventEmitter<LoanModel> = new EventEmitter<LoanModel>();
  constructor(private lss: LoanSingleService, private router: Router) { }

  ngOnInit(): void {
    if ( this.LoanId !== undefined &&  this.LoanId !== ''  &&  this.Loan.Id === ''){
      this.lss.getLoan(this.LoanId).subscribe( resp => {
        this.Loan.Response = resp;
      });
    }
  }

  public onClick(): void {
    this.Selected.emit(this.Loan);
  }

  public onClose(): void{
    this.Closed.emit(this.Loan);
  }

  public gotoLoan(id: string): void {
    this.router.navigate(['/edit-loan', id]);
  }

}
