import {Component, OnInit, ChangeDetectionStrategy, Input, Output, EventEmitter, OnChanges, ChangeDetectorRef} from '@angular/core';
import { Router } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import {TestingCenterService} from '../../../../core/service/TestingCenter/testing-center.service';
import {BaseList} from '../../../../core/base/base-list';
import {catchError, concatMap, filter, tap} from 'rxjs/operators';
import {MatDialog} from '@angular/material/dialog';
import {SweetalertService} from '../../../../core/service/sweetalert/sweetalert';
import { ViewSeatComponent } from '../dialog/view-seat/view-seat.component';

@Component({
  selector: 'app-manage-test-center-list',
  templateUrl: './manage-test-center-list.component.html',
  styleUrls: ['./manage-test-center-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ManageTestCenterListComponent extends BaseList implements OnInit  {
  testingcenterList$ = new Observable();
  constructor(
    private testingCenter: TestingCenterService,
    private cdr: ChangeDetectorRef,
    private dialog: MatDialog,
    private swSV: SweetalertService
  ) {
    super();
  }

  ngOnInit(): void {
    this.testingcenterList$ = this.testingCenter.getAll();
  }
  search(query){
    if (query) { return this.testingcenterList$ = this.testingCenter.search(`?${query}`); }
    if (!query) { return this.testingcenterList$ = this.testingCenter.getAll(); }
  }

  viewAvailableSeat(testing_center_uid){
    const dialog = this.dialog.open(ViewSeatComponent, {
      width: '50%',
      data: testing_center_uid
    });
  }

  delete(id){
    this.swSV.confirm().pipe(
      filter(x => x.isConfirmed),
      concatMap(x => {
        return this.testingCenter.deleteData(id).pipe(
          tap(x => this.swSV.removeSuccess()),
          tap(x => this.testingcenterList$ = this.testingCenter.getAll()),
          tap(x => this.cdr.detectChanges()),
          catchError(err => {
            this.swSV.errText(err.error)
            return throwError(err)
          })
        );
      })
    ).subscribe();
  }
}
