import {Component, OnInit, ChangeDetectionStrategy, Output, EventEmitter, Input, OnChanges} from '@angular/core';
import {createQueryStringFromObject} from '../../../../../shared/util/func';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';
import {TestingInformationService} from '../../../../../core/service/testing-information/testing-information.service';
import {AcademicYearService} from '../../../../../core/service/settings/academic-year.service';
import {AcademicSemesterService} from '../../../../../core/service/settings/academic-semester.service';

@Component({
  selector: 'app-report-candidate',
  templateUrl: './report-candidate.component.html',
  styleUrls: ['./report-candidate.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportCandidateComponent implements OnInit, OnChanges {
  @Input() TestingInformationLists: any = [];
  @Input() AcademicYear = [];
  @Input() AcademicsemesterMock = [];
  curSub;

  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  query = {
    testing_information_uid: null,
    academic_year_uid: null,
    academic_semester_uid: null,
    from_date: null,
    to_date: null,
  };
  constructor(
    private swSV: SweetalertService,

  ) { }

  ngOnInit(): void {
  }

  ngOnChanges(){
  }

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

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

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

  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
  }

  convert(str) {
    var date = new Date(str),
      mnth = ("0" + (date.getMonth() + 1)).slice(-2),
      day = ("0" + date.getDate()).slice(-2);
    return [date.getFullYear(), mnth, day].join("-");
  }

  chooseInfor(uidInformation){
    const testinforList = this.TestingInformationLists.find((member) => {
      return member.testing_information_uid === uidInformation;
    });
    this.setQuery(testinforList);
  }

  setQuery(testinforList){
    const academicyearList = this.AcademicYear.find((member) => {
      return member.academic_year_code === testinforList.academic_year_code;
    });
    const AcademicsemesterList = this.AcademicsemesterMock.find((member) => {
      return member.academic_semester_code === testinforList.academic_semester_code;
    });
    if (academicyearList.academic_year_uid !== null){
      this.query.academic_year_uid = academicyearList.academic_year_uid;
    }
    if (AcademicsemesterList.academic_semester_uid !== null){
      this.query.academic_semester_uid = AcademicsemesterList.academic_semester_uid;
    }
  }

}
