import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { SubjectsService } from 'src/app/core/service/settings/subjects.service';
import {SweetalertService} from '../../../../core/service/sweetalert/sweetalert';
import {tap} from 'rxjs/operators';

@Component({
  selector: 'app-setting-exam',
  templateUrl: './setting-exam.container.html',
  styleUrls: ['./setting-exam.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SettingExamContainer implements OnInit {
  subjectList$ = new Observable()
  constructor(
    private router: Router,
    private subjectSV: SubjectsService,
    private swSV: SweetalertService,
    private cdr: ChangeDetectorRef,
  ) { }

  ngOnInit(): void {
    this.swSV.loadingStart();
    this.subjectList$ = this.subjectSV.getAll().pipe(
      tap(x => this.swSV.close()),
    );
  }

  add(){
    this.router.navigate(['setting-exam/add'])
  }

  edit(id){
    this.router.navigate(['setting-exam/edit',id])
  }

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

}
