import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {TestingInformationService} from "../../../../../core/service/testing-information/testing-information.service";
import {Router} from "@angular/router";
import {SweetalertService} from "../../../../../core/service/sweetalert/sweetalert";
import {Observable} from "rxjs";
import {concatMap, filter, tap} from "rxjs/operators";

@Component({
  selector: 'app-manage-exam-officer',
  templateUrl: './manage-exam-officer.container.html',
  styleUrls: ['./manage-exam-officer.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ManageExamOfficerContainer implements OnInit {
  subjectList$ = new Observable();

  constructor(
    private router: Router,
    private TestinforSV: TestingInformationService,
    private cdr: ChangeDetectorRef,
    private swSV: SweetalertService
  ) { }

  ngOnInit(): void {
    this.subjectList$ = this.TestinforSV.getAll();
  }

  add(){
    this.router.navigate(['manage-exam-officer/add']);
  }

  edit(id){

    this.router.navigate(['manage-exam-officer/edit', id]);
  }

  search(query){
    if(query) return this.subjectList$ = this.TestinforSV.search(`?${query}`);
    if(!query) return this.subjectList$ = this.TestinforSV.getAll();
  }

  delete(id){
    this.swSV.confirm().pipe(
      filter(x => x.isConfirmed),
      concatMap(x => {
        return this.TestinforSV.deleteData(id).pipe(
          tap(x => this.swSV.removeSuccess()),
          tap(x => this.subjectList$ = this.TestinforSV.getAll()),
          tap(x => this.cdr.detectChanges())
        );
      })
    ).subscribe();
  }



}
