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

import { FundService } from '../../../services/fund.service';
import { LoginService } from '../../../services/login.service';

import { FundRecordData } from '../../../structures/fund-record-data';

import * as Globals from '../../../globals';

@Component({
  selector: 'app-my-fund-page',
  templateUrl: './my-fund-page.component.html',
  styleUrls: ['./my-fund-page.component.scss']
})
export class MyFundPageComponent implements OnInit {

  title = Globals.pages.myFund.text;

  myFund: FundRecordData[] | null = null;
  actualFundSum : number = 0;

  fundSavingStatus: number = Globals.savingStatus.none;

  userId: number = 0;

  constructor(private fundService: FundService, private loginService: LoginService) {

    var userData = this.loginService.getActualUserData();
    if(userData?.instructor?.ID != undefined) {
      this.userId = userData.instructor.ID;
    }

    this.fundService.fundRecords$.subscribe((data: FundRecordData[] | null) => {
      if(data != null) {
        this.myFund = data?.filter(fundRecord => { return fundRecord.personId == this.userId; });
        this.actualFundSum = 0;
        data?.forEach(fundRecord => { this.actualFundSum += fundRecord.amount; });   
      } else {
        this.myFund = null;
      }
    })
    this.fundService.actualizeMyFund();

    this.fundService.saveStatusReset();
    this.fundService.fundRecordSaveStatus$.subscribe( val => this.fundSavingStatus = val );
  }

  ngOnInit(): void {
  }

}
