import {Component, OnInit, ChangeDetectionStrategy, Output, EventEmitter, Input} from '@angular/core';
import {createQueryStringFromObject} from '../../../../../shared/util/func';
import {AcademicYearService} from '../../../../../core/service/settings/academic-year.service';
import {AcademicSemesterService} from '../../../../../core/service/settings/academic-semester.service';
import {TestingCenterService} from '../../../../../core/service/TestingCenter/testing-center.service';
import {TestingFieldService} from '../../../../../core/service/settings/testing-field.service';
import {SubjectsService} from '../../../../../core/service/settings/subjects.service';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';

@Component({
  selector: 'app-report-candidate-register-by-center',
  templateUrl: './report-candidate-register-by-center.component.html',
  styleUrls: ['./report-candidate-register-by-center.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportCandidateRegisterByCenterComponent implements OnInit {
  @Input() TestcenterLists: any = [];
  @Input() TestFieldLists: any = [];
  @Input() TestRoomLists: any = [];
  @Input() SubjectLists: any = [];
  @Input() AcademicYear = [];
  @Input() AcademicsemesterMock = [];

  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  query = {
    academic_year_uid: null,
    academic_semester_uid: null,
    branch_uid: null,
    testing_field_uid: null,
    testing_center_uid: null,
    subject_uid: null,
    from_date: null,
    to_date: null,
  };
  private curSub: any;
  constructor(
    private swSV: SweetalertService,
    private academicYearSV: AcademicYearService,
    private academicSemSV: AcademicSemesterService,
    private testcenterSV: TestingCenterService,
    private testingFieldSV: TestingFieldService,
    private subjectSV: SubjectsService,
  ) { }

  ngOnInit(): void {
    console.log(this.TestcenterLists);
    this.testcenterSV.getAll().subscribe(
      result => {
        this.TestRoomLists = result;
      }
    );
    this.subjectSV.getAll().subscribe(
      result => {
        this.SubjectLists = result;
      }
    );
  }

  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 = {
      academic_year_uid: null,
      academic_semester_uid: null,
      branch_uid: null,
      testing_field_uid: null,
      testing_center_uid: null,
      subject_uid: null,
      from_date: null,
      to_date: 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
  }

  filterTestingFiled(data){
    this.testingFieldSV.getByBrandID(data).subscribe(
      result => {
        const TestFieldLists = result;
        this.TestFieldLists = TestFieldLists
      }
    )
  }

  filterTestingRoom(data){
    this.testcenterSV.getByFiledID(data).subscribe(
      result => {
        const TestRoomLists = result;
        this.TestRoomLists = TestRoomLists
      }
    )
  }
}
