import { Component, Input, OnInit } from '@angular/core';
import { IFilterOption } from '@shared/ui-api/interfaces/route-config/filter-option';
import { FilteringService } from '@shared/ui-api/filtering.service';

@Component({
  selector: 'app-objects-list-filtering-date',
  templateUrl: './objects-list-filtering-date.component.html',
  styleUrls: ['./objects-list-filtering-date.component.scss']
})
export class ObjectsListFilteringDateComponent implements OnInit {
  @Input() filterOption: IFilterOption;
  dateChoice: Date;
  greaterThanFiltering: boolean;

  constructor(
    private filterService: FilteringService
  ) { }

  applyFiltering() {
    let parsedField = this.filterOption.field;
    if (this.greaterThanFiltering === true) {
      parsedField = parsedField + '__gte';
      this.dateChoice.setHours(0);
      this.dateChoice.setMinutes(0);
      this.dateChoice.setSeconds(0);
      this.dateChoice.setMilliseconds(0);
    } else {
      parsedField = parsedField + '__lte';
      this.dateChoice.setHours(23);
      this.dateChoice.setMinutes(59);
      this.dateChoice.setSeconds(59);
      this.dateChoice.setMilliseconds(999);
    }
    parsedField = parsedField + '__date'; // required format by custom-made filter in fleio django app
    this.filterService.addFilter(parsedField, this.dateChoice.valueOf().toString(), this.filterOption?.resetQueryParams);
  }

  ngOnInit() {
  }

}
