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 { MyTeamService } from '../service/my-team.service';
import * as moment from 'moment';

@Component({
  selector: 'app-subordinate-leave',
  templateUrl: './subordinate-leave.component.html',
  styleUrls: ['./subordinate-leave.component.scss'],
})
export class SubordinateLeaveComponent implements OnInit {
  public startDate: any;
  public endDate: any;
  public list = [];
  public noData = true;

  constructor(
    public ts: TranslatorService,
    public common: CommonService,
    public myTeamService: MyTeamService,
  ) { }

  ngOnInit() {}

  subordinateLeaveList() {
    const Sdata = this.common.convertDateToJsonDateUpdated(this.startDate);
    const Edata = this.common.convertDateToJsonDateUpdated(this.endDate);
    this.myTeamService.getEmployeeSubordinatesLeave({
      P_DATE_FROM: Sdata,
      P_DATE_TO: Edata
    }, () => { }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      if (this.common.validResponse(result)) {
        this.list = result.GetSubordinatesLeavesTotalVacationsList;
        this.noData = false;
      }
    });
  }

  getDate(date) {
    const newDate = this.common.localizeDate(new Date(this.common.getDateTimeISOFromStringUpdated(date).split(' ')[0]));
    return newDate;
  }
  diffDays(date) {
    const date1 = new Date(this.common.getDateTimeISOFromStringUpdated(date.DATE_START).split(' ')[0]);
    const date2 = new Date(this.common.getDateTimeISOFromStringUpdated(date.DATE_END).split(' ')[0]);
    const diff = Math.abs(date1.getTime() - date2.getTime());
    const diffDays = Math.ceil(diff / (1000 * 3600 * 24));
    return diffDays;
  }

  checkDate() {
    if (this.startDate && this.endDate) {
      return false;
    } else { return true; }
  }
}
