import { Component, OnInit } from '@angular/core';
import { ModalController, NavParams } from '@ionic/angular';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { TimeCardService } from 'src/app/time-card/service/time-card.service';

@Component({
  selector: 'app-select-period',
  templateUrl: './select-period.component.html',
  styleUrls: ['./select-period.component.scss'],
})
export class SelectPeriodComponent implements OnInit {
  public shiftTypesArray: any = [];
  public dateFrom: any;
  public dateTo: any;
  public selectedShiftType: any;
  public Select = '';

  public filtersArray: any = [
    {
      name: 'Excess',
      active: false,
      key: 'P_EXCESS_FLAG',
      value: ''
    },
    {
      name: 'Timeback',
      active: false,
      key: 'P_TIMEBACK_FLAG',
      value: ''
    },
    {
      name: 'Comp Off',
      active: false,
      key: 'P_COMP_OFF_FLAG',
      value: ''
    },
    {
      name: 'Non scheduled',
      active: false,
      key: 'P_NON_SCHEDULED_FLAG',
      value: ''
    },
    {
      name: 'Late In',
      active: false,
      key: 'P_LATE_IN_FLAG',
      value: ''
    },
    {
      name: 'Early Out',
      active: false,
      key: 'P_EARLY_OUT_FLAG',
      value: ''
    },
    {
      name: 'Shortage',
      active: false,
      key: 'P_SHORTAGE_FLAG',
      value: ''
    },
    {
      name: 'Missing Swipe',
      active: false,
      key: 'P_MISSING_SWIPE_FLAG',
      value: ''
    },
    {
      name: 'Work On Break',
      active: false,
      key: 'P_ACTUAL_WOB_SEC',
      value: ''
    },
    {
      name: 'Analyzed',
      active: false,
      key: 'P_ANALAYZED_FLAG',
      value: ''
    },
    {
      name: 'Approved Timeback',
      active: false,
      key: 'P_APPR_TIMEBACK_FLAG',
      value: ''
    },
  ];
  constructor(
    public modalController: ModalController,
    public common: CommonService,
    public timeCardService: TimeCardService,
    public navParams: NavParams) { }

  ngOnInit() {
    this.getShiftTypes();
    this.dateFrom = this.navParams.get('dateFrom');
    this.dateTo = this.navParams.get('dateTo');
  }

  public dismissModal() {
    const selectedFilters: any = {};
    for (const filter of this.filtersArray) {
      selectedFilters[filter.key] = filter;
    }
    this.modalController.dismiss({
      dismissed: true,
      keySelectedFilters: selectedFilters,
      selectedShiftType: this.selectedShiftType,
      dateFrom: this.dateFrom,
      dateTo: this.dateTo
    });
  }

  public changeFiltersSelection(i: any) {
    this.filtersArray[i].active = !this.filtersArray[i].active;
    if (this.filtersArray[i].active) {
      this.filtersArray[i].value = 'Y';
    } else {
      this.filtersArray[i].value = '';
    }
  }

  public getShiftTypes() {
    this.timeCardService.getShiftType().subscribe((result) => {
        if (this.common.validResponse(result)) {
              this.shiftTypesArray = result.GetShiftTypesList;
        }
    });
  }

}
