import {Component, OnInit, ChangeDetectionStrategy, Input, Output, EventEmitter} from '@angular/core';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';

@Component({
  selector: 'app-report-exam-total',
  templateUrl: './report-exam-total.component.html',
  styleUrls: ['./report-exam-total.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportExamTotalComponent implements OnInit {
  @Input() TestingInformationLists = [];
  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  query = {
    testing_information_uid: null,
  };
  private curSub: any;
  constructor(
    private swSV: SweetalertService,
  ) { }

  ngOnInit(): void {
  }

  displayTestInfor(uid){
    if (uid && this.TestingInformationLists) return this.TestingInformationLists.find(x => x.testing_information_uid === uid)?.testing_information_name
    if (uid && !this.TestingInformationLists) return this.curSub?.testing_information_name
  }

  onSearch(){
    let checkNULL = false;
    checkNULL = this.checkFunction(this.query);
    if (checkNULL === false){
      this.search.emit(this.query);
    }
  }

  onExcel(){
    let checkNULL = false;
    checkNULL = this.checkFunction(this.query);
    if (checkNULL === false){
      this.excel.emit(this.query);
    }
  }

  checkFunction(object){
    for (const key in object) {
      if (!object[key]) {
        this.swSV.errText('กรุณาใส่ข้อมูลให้ครบถ้วน');
        return true;
      }
    }
    return false;
  }

  clear(){
    this.query = {
      testing_information_uid: null
    };
  }

}
