import {Component, OnInit, ChangeDetectionStrategy, Output, EventEmitter, Input} from '@angular/core';
import {createQueryStringFromObject} from '../../../../../shared/util/func';
import {TestingCenterService} from '../../../../../core/service/TestingCenter/testing-center.service';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';
import {TestingFieldService} from '../../../../../core/service/settings/testing-field.service';
import {TestingInformationService} from '../../../../../core/service/testing-information/testing-information.service';

@Component({
  selector: 'app-report-signature-examination',
  templateUrl: './report-signature-examination.component.html',
  styleUrls: ['./report-signature-examination.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportSignatureExaminationComponent implements OnInit {
  @Input() TestcenterLists: any = [];
  saveTestcenterLists: any = [];
  isUseTestCenterList = false;
  @Input() TestFieldLists: any = [];
  @Input() TestRoomLists: any = [];
  @Input() SubjectLists: any = [];
  @Input() AcademicYear = [];
  @Input() AcademicsemesterMock = [];
  sectionsFake = [
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 1,
      "is_check": false,
      "start_time": "09:00"
    },
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 2,
      "is_check": false,
      "start_time": "12:00"
    },
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 3,
      "is_check": false,
      "start_time": "15:00"
    },
    {
      "status_id": 1,
      "testing_center_period_uid": null,
      "period_id": 4,
      "is_check": false,
      "start_time": "18:00"
    }
  ];
  PeriodLists: any = [];
  @Input() TestingInformationLists = [];
  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  curSub;
  query = {
    testing_information_uid: null,
    // academic_year_uid: null,
    // academic_semester_uid: null,
    branch_uid: null,
    testing_field_uid: null,
    testing_center_uid: null,
    period_id: null,
    test_date: null,
  };
  constructor(
    private testcenterSV: TestingCenterService,
    private testingFieldSV: TestingFieldService,
    private testingInforSV: TestingInformationService,
    private swSV: SweetalertService,
  ) { }

  ngOnInit(): void {
  }

  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){
    console.log(object);
    for (const key in object) {
      console.log(key)
      console.log(object[key])
      if (!object[key]) {
        this.swSV.errText('กรุณาใส่ข้อมูลให้ครบถ้วน');
        return true;
      }
    }
    return false;
  }

  clear(){
    this.query = {
      testing_information_uid: null,
      // academic_year_uid: null,
      // academic_semester_uid: null,
      branch_uid: null,
      testing_field_uid: null,
      testing_center_uid: null,
      period_id: null,
      test_date: null,
    };
    this.testcenterSV.getAll().subscribe(
      result => {
        this.TestcenterLists = result;
      }
    );
  }

  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){
    console.log(this.TestcenterLists);
    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
      }
    )
  }

  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;
    // }
    this.testingInforSV.get(testinforList.testing_information_uid).subscribe(
      result => {
        const dataInfor: any = result;
        this.isUseTestCenterList = true;
        this.TestcenterLists = this.TestcenterLists.filter(item1 =>
          !!dataInfor.testing_rooms.find(item2 => item1.branch_uid === item2.branch_uid)
        );
      }
    );
  }

}
