import { NgxMatDatetimePicker } from '@angular-material-components/datetime-picker';
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'byte-operative-dashboard-filter',
  templateUrl: './operative-dashboard-filter.component.html',
  styleUrls: ['./operative-dashboard-filter.component.scss']
})
export class OperativeDashboardFilterComponent implements OnInit {

  form: FormGroup;

  channels = [
    {
      value: 'W',
      label: 'Whatsapp'
    },
    {
      value: 'F',
      label: 'Facebook'
    }
  ];

  endDateTime: Date;
  startDateTime: Date;
  selectedChannel: string;
  isLoading = false;

  @Input()
  header: string;

  @Input()
  headerDesc: string;

  @Input()
  icon = '<i class="fa fa-bar-chart-o"></i>';

  @Output()
  filtered: EventEmitter<any> = new EventEmitter();

  constructor(
    protected route: ActivatedRoute,
    protected router: Router) {
      this.selectedChannel = this.channels[0].value;
  }

  ngOnInit() {
    const title = this.route.snapshot.data['title'];
    const titleDesc = this.route.snapshot.data['titleDesc'];
    this.header = title;
    this.headerDesc = titleDesc;
    this.form = new FormGroup({
      id: new FormControl('', Validators.required),
      period: new FormControl('', Validators.required)
    });

    // cq: cambiar a fecha actual
    this.endDateTime = new Date();
    this.startDateTime = this.getStartDate(this.endDateTime);
    this.search();
  }

  search() {
    const filters = { startDateTime: this.startDateTime, endDateTime: this.endDateTime, channel: this.selectedChannel, rangeMinutes: 60 };
    this.filtered.emit(filters);
  }

  private getStartDate(endDate: Date): Date {
    return new Date(Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate(), 5, 0, 0));
  }

  keyup(event) {
    event.preventDefault();
  }

  clickEvent(event: NgxMatDatetimePicker<any>) {
    event.open();
  }
}
