import {Component, OnInit, ChangeDetectionStrategy, OnChanges, Input, ChangeDetectorRef} from '@angular/core';
import {BaseList, CustomeEventPage} from '../../../../core/base/base-list';
import {map, switchMap, tap} from 'rxjs/operators';
import {Observable} from 'rxjs';
import {TestingInformationService} from '../../../../core/service/testing-information/testing-information.service';
import {ActivatedRoute, Router} from '@angular/router';
import {formatDate} from '@angular/common';
import {DialogComponent} from '../../presenter/dialog/dialog.component';
import {TestingControlsService} from '../../../../core/service/testing-information/testing-controls.service';
import {MatDialog} from '@angular/material/dialog';

@Component({
  selector: 'app-change-exam-room-view',
  templateUrl: './change-exam-room-view.container.html',
  styleUrls: ['./change-exam-room-view.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ChangeExamRoomViewContainer extends BaseList implements OnInit{
  @Input() testingInformationList: any = [];
  testingInformation$ = new Observable();
  paginator;
  Id;
  testingInfoData;
  currentQueryPaging = '';
  currentQueryString = '';
  reload: boolean;
  constructor(
    private testinforSV: TestingInformationService,
    private testingControlSV: TestingControlsService,
    private cdr: ChangeDetectorRef,
    private router: Router,
    private dialog: MatDialog,
    public activeRouter: ActivatedRoute,
  ){
    super();
  }

  ngOnInit(): void {
    this.activeRouter.params.pipe(
      tap(x => x.id ? this.Id = x.id : this.Id = null),
      tap(x => {
        this.testinforSV.get(this.Id).pipe(
          tap(y => this.testingInfoData = y),
        ).subscribe();
      }),
    ).subscribe();
  }

  pageChange(event: CustomeEventPage){
    this.paginator = event.paginator;
    this.currentQueryPaging = event.queryStringPage;
    if (this.currentQueryString) {
      return this.testingInformation$ = this.testingControlSV.getNoRoomPage(`${event.queryStringPage}?${this.currentQueryString}`,this.Id)
    }else{
      return this.testingInformation$ = this.testingControlSV.getNoRoomPage(`${event.queryStringPage}`,this.Id)
    }
  }

  search(queryString){
    // if(query) return this.testingInformation$ = this.testingControlSV.getSearchForCountSeat(`?${query}`);
    // if(!query) return this.testingInformation$ = this.testingControlSV.getSearchForCountSeat(`?is_open=true`);

    this.currentQueryString = queryString
    if (queryString) {
      this.paginator.firstPage()
      return this.testingInformation$ = this.testingControlSV.query(`${this.currentQueryPaging}?${this.currentQueryString}`)
    }
    if (!queryString) {
      return this.testingInformation$ = this.testingControlSV.query(`${this.currentQueryPaging}`)
    }
  }

  changeRoom(x){
    console.log(x)
    const obj = {
      data : x.data,
      testing_information: x.TestInformation
    }
    // this.query.start_date = formatDate(this.query.start_date, 'yyyy-MM-dd', 'en-US');
    // this.query.end_date = formatDate(this.query.end_date, 'yyyy-MM-dd', 'en-US');
    const dialogRef = this.dialog.open(DialogComponent, {
      width: '500px',
      data: obj
    });
    //
    dialogRef.afterClosed().pipe(
      switchMap(x => {
        return this.testingControlSV.getNoRoomPage(`1?5`,this.Id).pipe(
          tap(x => this.cdr.detectChanges()))
      })
    ).subscribe()
  }

}
