import {Component, OnInit, ChangeDetectionStrategy, Output, EventEmitter} from '@angular/core';
import {createQueryStringFromObject} from '../../../../../../shared/util/func';
import {formatDate} from '@angular/common';

@Component({
  selector: 'app-user-couter-search',
  templateUrl: './user-couter-search.component.html',
  styleUrls: ['./user-couter-search.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class UserCouterSearchComponent implements OnInit {

  @Output() add = new EventEmitter();
  @Output() search = new EventEmitter();
  query = {
    counter_close_date: null,
    counter_open_date: null
  };
  constructor() { }

  ngOnInit(): void {
  }

  onAdd(){
    this.add.emit();
  }

  onSearch(){
    if(this.query.counter_open_date) this.query.counter_open_date = formatDate(this.query.counter_open_date, 'yyyy-MM-dd', 'en-US');
    if(this.query.counter_close_date) this.query.counter_close_date = formatDate(this.query.counter_close_date, 'yyyy-MM-dd', 'en-US');
    const query = createQueryStringFromObject(this.query);
    this.search.emit(query) ;
  }

  clear(){
    this.query = {
      counter_close_date: null,
      counter_open_date: null
    };
    const query = createQueryStringFromObject(this.query);
    this.search.emit(query) ;
  }

}
