import {Component, OnInit, ChangeDetectionStrategy, Output, EventEmitter, Input} from '@angular/core';
import {formatDate} from '@angular/common';
import {createQueryStringFromObject} from '../../../../../../shared/util/func';
import {ActivatedRoute, Router} from '@angular/router';
import {tap} from 'rxjs/operators';

@Component({
  selector: 'app-notification-check-exam-search',
  templateUrl: './notification-check-exam-search.component.html',
  styleUrls: ['./notification-check-exam-search.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class NotificationCheckExamSearchComponent implements OnInit {
  @Output() search = new EventEmitter();
  @Input() testingInfoData: any = [];
  @Input() subjectListSend: any = [];
  query = {
    testing_information_uid: null,
    subject_uid: null,
    start_date: null,
    end_date: null,
  };
  private curSub: any;
  constructor(
    private router: Router,
    private activeRouter: ActivatedRoute,
  ) { }

  ngOnInit(): void {
    this.activeRouter.params.pipe(
      tap(x => {
        this.query.testing_information_uid = x.id;
      }),
    ).subscribe();
  }

  onSearch(){
    if(this.query.start_date) this.query.start_date = formatDate(this.query.start_date, 'yyyy-MM-dd', 'en-US');
    if(this.query.end_date) this.query.end_date = formatDate(this.query.end_date, 'yyyy-MM-dd', 'en-US');
    let query = createQueryStringFromObject(this.query);
    this.search.emit(query);
  }

  displayTestInfor(uid){
    if (uid && this.subjectListSend) return this.subjectListSend.find(x => x.subject_uid === uid)?.subject_name_th
    if (uid && !this.subjectListSend) return this.curSub?.subject_name_th
  }

  clear(){
    this.query = {
      testing_information_uid: this.query.testing_information_uid,
      subject_uid: null,
      start_date: null,
      end_date: null,
    };
  }

}
