import { Component, OnInit } from "@angular/core";
import { CommonService } from "src/app/hmg-common/services/common/common.service";
import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service";
import { AuthenticationService } from "src/app/hmg-common/services/authentication/authentication.service";
import { AccrualService } from "../services/accrual.service";
import { AuthenticatedUser } from "src/app/hmg-common/services/authentication/models/authenticated-user";
@Component({
  selector: "app-home",
  templateUrl: "./home.component.html",
  styleUrls: ["./home.component.scss"]
})
export class HomeComponent implements OnInit {
  Sdate: any;
  emp_no: any;
  balance:any;
  constructor(
    public ts: TranslatorService,
    public cs: CommonService,
    public accrualService: AccrualService,
    public authService:AuthenticationService
  ) {}

  ngOnInit() {
    this.Sdate = new Date().toISOString();
   
    this.getUserDetails();
  }
  private getUserDetails(){
    this.authService.loadAuthenticatedUser().subscribe((user: AuthenticatedUser) => {
        if (user) {
          this.emp_no=user.EMPLOYEE_NUMBER;
          this.getAccrualBalance();
        }
      });
  }
  private getAccrualBalance() {
    if (this.Sdate) {
      let today = new Date(this.Sdate);
      let day = today.getDate();
      let month = today.getMonth() + 1;
      let year = today.getFullYear();
      let todayDate = month + "/" + day + "/" + year;
      let effectiveDate = todayDate;
      const request = {
        P_SELECTED_EMPLOYEE_NUMBER: this.emp_no,
        P_EFFECTIVE_DATE: effectiveDate
      };
      this.accrualService
        .getAccrualBalances(request)
        .subscribe((result: any) => {
          this.handleAccrualResult(result);
        });
    }
  }
  handleAccrualResult(result) {
    if (this.cs.validResponse(result)) {
      this.balance=result.GetAccrualBalancesList;
      }
  }
}
