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

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

  @Input() subjectList2: any = [];
  @Input() subjectList: any = [];
  @Input() subjectList1: any = [];
  @Input() subjectList3: any = [];
  @Input() subjectList4: any = [];
  @Input() testingInfoData: any;
  @Output() add = new EventEmitter();
  @Output() search = new EventEmitter();
  branchList: any = [];
  testingFieldList: any = [];
  filteredTestingFieldList: any = [];
  filteredRoomList: any = [];
  roomList: any = [];
  statusMock = [{id: 1, name: 'ใช้งาน'}, {id: 2, name: 'ยกเลิก'}];
  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"
    }
  ];
  constructor(
    private testcenterSV: TestingCenterService,
    private testingFieldSV: TestingFieldService,
  ) { }
  query = {
    subject_uid: null,
    branch_uid: null,
    testing_field_uid: null,
    testing_center_uid: null,
    testing_datetime: null,
    period_id: null,
    student_code: null
  };
  ngOnInit(): void {

  }

  ngOnChanges(): void {
    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)
  }

  onAdd(){
    this.add.emit();
  }

  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
    };
    this.onSearch()
  }


}
