import { formatDate } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatGridTileHeaderCssMatStyler } from '@angular/material/grid-list';
import { throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { BaseList } from 'src/app/core/base/base-list';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { ChangeTestingRoomAndDateService } from 'src/app/core/service/testing-register/change-testing-room-and-date.service';
import { CheckAvailableRoomAndPeriodService } from 'src/app/core/service/testing-register/check-available-room-and-period.service';
import {TestingInformationService} from '../../../../../core/service/testing-information/testing-information.service';
import {createQueryStringFromObject} from '../../../../../shared/util/func';
@Component({
  selector: 'app-change-exam-day-dialog',
  templateUrl: './change-exam-day-dialog.component.html',
  styleUrls: ['./change-exam-day-dialog.component.scss'],
  // changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChangeExamDayDialogComponent extends BaseList implements OnInit {
  seatLists: any = [];
  testingDate;
  query = {
    testing_room_code: '',
    period_id: '',
  };
  PeriodLists: any  = [];
  RoomLists: any = [];
  testingInfoData;
  currentDate = new Date()
  constructor(
    public dialogRef: MatDialogRef<ChangeExamDayDialogComponent>,
    private fb: FormBuilder,
    private testInforSV: TestingInformationService,
    private swSV: SweetalertService,
    private checkAvlRoomAndPeriodSV: CheckAvailableRoomAndPeriodService,
    @Inject(MAT_DIALOG_DATA) public data,
    private cdr: ChangeDetectorRef,
    private changeTestingRoomAndDateSV: ChangeTestingRoomAndDateService
  ) {
    super()
   }

  ngOnInit(): void {
    // console.log(this.data.data.testing_information_uid)
    this.testInforSV.get(this.data.data.testing_information_uid).pipe(
      tap(x => this.testingInfoData = x),
      tap(x => this.cdr.detectChanges()),
    ).subscribe();
  }

  search(){
    if (this.testingDate != null){
      // const date = formatDate(this.query.testing_date, 'yyyy-MM-dd', 'en-US');
      this.testingDate = formatDate(this.testingDate, 'yyyy-MM-dd', 'en-US');
      if (this.query.period_id != null || this.query.testing_room_code != null){
        const query = createQueryStringFromObject(this.query);
        this.checkAvlRoomAndPeriodSV.checkAvalroomWithQuery(this.data.data,query,this.testingDate).pipe(
          tap((x:any) => {
            if(x.length == 0){
              this.swSV.errText('ไม่มีห้องในวันที่ค้นหา')
            }else{
              this.seatLists = [];
              this.seatLists = this.updateMatTable(x);
              this.cdr.detectChanges();
            }
          })
        ).subscribe();
      }else {
        this.checkAvlRoomAndPeriodSV.checkAvalroom(this.data.data).pipe(
          tap((x:any) => {
            if(x.length == 0){
              this.swSV.errText('ไม่มีห้องในวันที่ค้นหา')
            }else{
              this.seatLists = [];
              this.seatLists = this.updateMatTable(x);
              this.cdr.detectChanges();
            }
          })
        ).subscribe();
      }
    }else {
      return this.swSV.errText('กรุณาใส่วันที่')
    }

  }

  select(data){
    const date = formatDate(this.testingDate, 'yyyy-MM-dd', 'en-US');
    const obj = {
      testing_information_uid : this.data.data.testing_information_uid,
      testing_register_uid: this.data.data.testing_register_uid,
      testing_subject_selected_uid: this.data.data.testing_subject_selected_uid,
      new_branch_uid: data.branch_uid,
      new_testing_field_uid: data.testing_field_uid,
      new_testing_center_uid: data.testing_center_uid,
      new_testing_date: date,
      new_period_id: data.period_id
    }
    this.changeTestingRoomAndDateSV.changeRoomAndDate(obj).pipe(
      tap(x => this.swSV.saveSuccess('ย้ายวันที่สอบสำเร็จ')),
      tap(x => this.dialogRef.close()),
      catchError(err => {
        this.swSV.errText(err.error)
        return throwError(err)
      })
    ).subscribe()
  }

}
