import { formatDate } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, Inject, ChangeDetectorRef } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
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 { TestingCenterService } from 'src/app/core/service/TestingCenter/testing-center.service';

@Component({
  selector: 'app-view-seat',
  templateUrl: './view-seat.component.html',
  styleUrls: ['./view-seat.component.scss'],
  // changeDetection: ChangeDetectionStrategy.OnPush
})
export class ViewSeatComponent extends BaseList implements OnInit {
  availableSeatList: any = [];
  query = {
    from_date: null,
    to_date: null,
    testing_center_uid: null
  }
  constructor(
    public dialogRef: MatDialogRef<ViewSeatComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private testingCenterSV: TestingCenterService,
    private cdr: ChangeDetectorRef,
    private swSV: SweetalertService
  ) {
    super()
   }

  ngOnInit(): void {
    this.query.testing_center_uid = this.data
  }

  search(){
    this.query.from_date = formatDate(this.query.from_date,'yyyy-MM-dd','en-US')
    this.query.to_date = formatDate(this.query.to_date,'yyyy-MM-dd','en-US')
    this.testingCenterSV.getAvailableSeat(this.query).pipe(
      tap((x:any) => {
        if(x.length == 0) return this.swSV.errText('ไม่มีข้อมูล')
      }),
      tap(x => this.availableSeatList = this.updateMatTable(x)),
      tap(x => this.cdr.detectChanges()),
      catchError(err => {
        this.swSV.errText(err.error.description)
        return throwError(err)
      })
    ).subscribe()
  }

}
