import { formatDate } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, EventEmitter, Input, OnChanges, Output } from '@angular/core';
import { TestingFieldService } from 'src/app/core/service/settings/testing-field.service';
import { TestingCenterService } from 'src/app/core/service/TestingCenter/testing-center.service';
import { createQueryStringFromObject } from 'src/app/shared/util/func';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SearchComponent implements OnInit,OnChanges {

  @Input() testingInfoData: any;
  @Output() search = new EventEmitter();
  branchList: any = [];
  testingFieldList: any = [];
  filteredTestingFieldList: any = [];
  filteredRoomList: any = [];
  roomList: any = [];
  statusMock = [{id: 1, name: 'ใช้งาน'}, {id: 2, name: 'ยกเลิก'}];
  query = {
    subject_uid: null,
    branch_uid: null,
    testing_field_uid: null,
    testing_center_uid: null,
    testing_datetime: null,
    period_id: null,
    student_code: null,
    testing_information_uid: null
  };
  constructor(
    private testcenterSV: TestingCenterService,
    private testingFieldSV: TestingFieldService,
  ) { }

  ngOnInit(): void {

  }

  ngOnChanges(): void {
    this.query.testing_information_uid = this.testingInfoData.testing_information_uid
    this.branchList = this.testingInfoData?.testing_rooms.filter((value,index,self) => self.findIndex(selfValue => selfValue.branch_name_th === value.branch_name_th) === index)
    this.roomList = this.testingInfoData?.testing_rooms.filter((value,index,self) => self.findIndex(selfValue => selfValue.testing_room_name_th === value.testing_room_name_th) === index)
    this.testingFieldList = this.testingInfoData?.testing_rooms.filter((value,index,self) => self.findIndex(selfValue => selfValue.testing_field_name_th === value.testing_field_name_th) === index)
  }


  onSearch(){
    if (this.query.testing_datetime != null){
      this.query.testing_datetime = formatDate(this.query.testing_datetime, 'yyyy-MM-dd', 'en-US');
    }
    const query = createQueryStringFromObject(this.query);
    this.search.emit(query);
  }

  displayTestInfor(uid){
    if (uid && this.testingInfoData) return this.testingInfoData.testing_subjects.find(x => x.subject_uid === uid)?.subject_code
  }

  displayBrand(uid){
    if (uid && this.testingInfoData) return this.testingInfoData.testing_rooms.find(x => x.branch_uid === uid)?.branch_name_th
  }

  displayField(uid){
    if (uid && this.testingInfoData) return this.testingInfoData.testing_rooms.find(x => x.testing_field_uid === uid)?.testing_field_name_th
  }

  displayCenter(uid){
    if (uid && this.testingInfoData) return this.testingInfoData.testing_rooms.find(x => x.testing_center_uid === uid)?.testing_room_name_th
  }

  filterTestingField(data){
    this.filteredTestingFieldList = []
    this.filteredTestingFieldList = this.branchList.filter(x => data.branch_uid == x.branch_uid)
  }

  setRoom(data){
    this.filteredRoomList = this.roomList.filter(x => (x.branch_uid == data.branch_uid)&&(x.testing_field_uid == data.testing_field_uid))
  }

  clear(){
    this.query = {
      subject_uid: null,
      branch_uid: null,
      testing_field_uid: null,
      testing_center_uid: null,
      testing_datetime: null,
      period_id: null,
      student_code: null,
      testing_information_uid: this.testingInfoData.testing_information_uid
    };
    this.onSearch()
  }


}
