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

@Component({
  selector: 'app-candidate-list-search',
  templateUrl: './candidate-list-search.component.html',
  styleUrls: ['./candidate-list-search.component.scss'],
  // changeDetection: ChangeDetectionStrategy.OnPush
})
export class CandidateListSearchComponent implements OnInit,OnChanges {
  @Input() subjectList2: any = [];
  @Input() subjectList: any = [];
  @Input() subjectList1: any = [];
  @Input() subjectList3: any = [];
  @Input() subjectList4: any = [];
  @Input() testingInformationData: any
  @Output() add = new EventEmitter();
  @Output() search = new EventEmitter();
  @Output() export = new EventEmitter();
  branchList: any = [];
  testInforId;
  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,
    public activeRouter: ActivatedRoute,
  ) { }
  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.testingInformationData?.testing_rooms.filter((value,index,self) => self.findIndex(selfValue => selfValue.branch_name_th === value.branch_name_th) === index)
    this.roomList = this.testingInformationData?.testing_rooms.filter((value,index,self) => self.findIndex(selfValue => selfValue.testing_room_name_th === value.testing_room_name_th) === index)
    this.testingFieldList = this.testingInformationData?.testing_rooms.filter((value,index,self) => self.findIndex(selfValue => selfValue.testing_field_name_th === value.testing_field_name_th) === index)
  }

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

  filterTestingField(data){
    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))
  }

  onExport(){
     this.activeRouter.params.pipe(
         tap(x => this.export.emit(x.id)),
     ).subscribe();
  }

  onSearch(){
    const periodid = [];
    let textfree = '';
    if (this.query.testing_datetime != null){
      this.query.testing_datetime = formatDate(this.query.testing_datetime, 'yyyy-MM-dd', 'en-US');
    }
    if (this.query.period_id != null){
      periodid.push(this.query.period_id);
      for (let i = 0; i < periodid[0].length; i++) {
        console.log(periodid[0][i]);
        textfree += `&period_id=${periodid[0][i]}`;
      }
      // this.query.period_id = null;
    }
    const query = createQueryStringFromObject(this.query);
    this.search.emit(`${query}${textfree}`);
  }

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

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

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

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

  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,
    };
  }

}
