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

@Component({
  selector: 'app-report-exam-result',
  templateUrl: './report-exam-result.component.html',
  styleUrls: ['./report-exam-result.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportExamResultComponent implements OnInit {
  @Input() SubjectLists: any = [];
  @Input() PeriodLists = [];
  @Input() TestingInformationLists = [];
  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  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"
    }
  ];
  curSub;
  curSub2;
  query = {
    subject_uid: null,
    testing_information_uid: null
  };
  constructor(
    private swSV: SweetalertService,
    private testingInformationSV: TestingInformationService,
    private cdr: ChangeDetectorRef
  ) { }

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

  onSearch(){
    let checkNULL = false;
    checkNULL = this.checkFunction(this.query);
    if (checkNULL === false){
      console.log(this.query);
      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,
      testing_information_uid: 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
  }
  displaySubject(uid){
    if (uid && this.SubjectLists) return this.SubjectLists.find(x => x.subject_uid === uid)?.subject_name_th
    if (uid && !this.SubjectLists) return this.curSub2?.subject_name_th
  }

  chooseInfor(uidInformation){
    console.log(uidInformation);
    this.testingInformationSV.get(uidInformation).subscribe(
      result => {
        console.log(result);
        const data:any = result;
        this.SubjectLists = data.testing_subjects;
      }
    );
    const testinforList = this.TestingInformationLists.find((member) => {
      return member.testing_information_uid === uidInformation;
    });
    this.setQuery(testinforList);
  }

  setQuery(testinforList){
    this.cdr.detectChanges()
    // console.log(this.SubjectLists);
  }
}
