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

@Component({
  selector: 'app-report-candidate-register-by-subject',
  templateUrl: './report-candidate-register-by-subject.component.html',
  styleUrls: ['./report-candidate-register-by-subject.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportCandidateRegisterBySubjectComponent implements OnInit {
  @Input() SubjectLists: any = [];
  @Input() AcademicYear = [];
  @Input() AcademicsemesterMock = [];
  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();

  query = {
    subject_uid: null,
    academic_year_uid: null,
    academic_semester_uid: null,
  };
  private curSub: any;
  constructor(
    private swSV: SweetalertService,
  ) { }

  ngOnInit(): void {
    console.log(this.SubjectLists)
  }

  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 = {
      subject_uid: null,
      academic_year_uid: null,
      academic_semester_uid: null,
    };
  }

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

}
