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

@Component({
  selector: 'app-report-separated-by-exam-center',
  templateUrl: './report-separated-by-exam-center.component.html',
  styleUrls: ['./report-separated-by-exam-center.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportSeparatedByExamCenterComponent implements OnInit {
  isUseTestCenterList = false;
  @Input() TestingInformationLists = [];
  @Input() SubjectLists = [];
  @Input() TestcenterLists = [];
  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  query = {
    testing_information_uid: null,
    subject_uid: null,
    branch_uid: null,
    from_date: null,
    to_date: null,
  };
  private curSub: any;
  private curSub2: any;
  constructor(
    private cdr: ChangeDetectorRef,
    private swSV: SweetalertService,
    private testingInforSV: TestingInformationService,
  ) { }

  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
  }

  displaySubject(uid){
    if (uid && this.SubjectLists) return this.SubjectLists.find(x => x.subject_uid === uid)?.subject_name_th
    if (uid && !this.SubjectLists) return this.curSub2?.subject_name_th
  }

  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;
  }

  chooseInfor(uidInformation){
    const testinforList = this.TestingInformationLists.find((member) => {
      return member.testing_information_uid === uidInformation;
    });
    this.testingInforSV.get(uidInformation).subscribe(
      result => {
        console.log(result);
        const data:any = result;
        this.SubjectLists = data.testing_subjects;
      }
    );
    this.setQuery(testinforList);
  }

  setQuery(testinforList){
    console.log(testinforList);
    this.testingInforSV.get(testinforList.testing_information_uid).subscribe(
      result => {
        const dataInfor: any = result;
        this.isUseTestCenterList = true;
        this.TestcenterLists = dataInfor.testing_rooms.filter((thing, i, arr) =>
          arr.findIndex(t => t.branch_uid === thing.branch_uid) === i
        );
        this.cdr.detectChanges()
      }
    );
  }

  clear(){
    this.query = {
      testing_information_uid: null,
      subject_uid: null,
      branch_uid: null,
      from_date: null,
      to_date: null,
    };
  }

}
