import {Component, OnInit, ChangeDetectionStrategy, EventEmitter, Input, Output, ChangeDetectorRef} from '@angular/core';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import {TestingFieldService} from "../../../../../core/service/settings/testing-field.service";
import {TestingCenterService} from "../../../../../core/service/TestingCenter/testing-center.service";
import {TestingInformationService} from '../../../../../core/service/testing-information/testing-information.service';

@Component({
  selector: 'app-report-candidate-summary',
  templateUrl: './report-candidate-summary.component.html',
  styleUrls: ['./report-candidate-summary.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportCandidateSummaryComponent implements OnInit {
  @Input() SubjectLists: any = [];
  @Input() AcademicYear = [];
  @Input() AcademicsemesterMock = [];
  @Input() TestingInformationLists = [];
  @Input() TestcenterLists: any = [];
  @Input() TestRoomLists: any = [];
  @Input() TestFieldLists: any = [];
  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  isUseTestCenterList = false;

  curSub;
  query = {
    testing_information_uid: null,
    academic_year_code: null,
    academic_semester_code: null,
    branch_uid: null,
    testing_center_uid: null,
    room_name_th: null,
  };
  constructor(
    private swSV: SweetalertService,
    private testingFieldSV: TestingFieldService,
    private testcenterSV: TestingCenterService,
    private testingInformationSV: TestingInformationService,
    private cdr: ChangeDetectorRef

  ) { }

  ngOnInit(): void {
    console.log(this.TestRoomLists);
  }

  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,
      academic_year_code: null,
      academic_semester_code: null,
      branch_uid: null,
      testing_center_uid: null,
      room_name_th: null,
    };
  }

  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
  }

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

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

  chooseInfor(uidInformation){
    this.testingInformationSV.get(uidInformation).subscribe(
      result => {
        console.log(result);
        const data:any = result;
        this.SubjectLists = data.testing_subjects;
        this.cdr.detectChanges();
      }
    );
    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_code = academicyearList.academic_year_code;
    }
    if (AcademicsemesterList.academic_semester_uid !== null){
      this.query.academic_semester_code = AcademicsemesterList.academic_semester_code;
    }
    this.testingInformationSV.get(testinforList.testing_information_uid).subscribe(
      result => {
        const dataInfor: any = result;
        this.isUseTestCenterList = true;
        console.log(dataInfor)
        this.TestcenterLists = dataInfor.testing_rooms.filter((thing, i, arr) =>
          arr.findIndex(t => t.branch_uid === thing.branch_uid) === i
        );
        this.cdr.detectChanges()
      }
    );
  }

}
