import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { Observable } from 'rxjs';
import { filter, switchMap, tap } from 'rxjs/operators';
import { CandidateTypeService } from 'src/app/core/service/enrollment-api/candidate-type.service';
import { StudentService } from 'src/app/core/service/student/student.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { UserService } from 'src/app/core/service/user/user.service';

@Component({
    selector: 'app-student-info',
    templateUrl: './student-info.container.html',
    styleUrls: ['./student-info.container.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class StudentInfoContainer implements OnInit {
    studentList$ = new Observable()
    candidateType$ = new Observable()
    constructor(
        private userSV: UserService,
        private candidateTypeSV: CandidateTypeService,
        private swSV: SweetalertService,
        private cdr: ChangeDetectorRef
    ) { }

    ngOnInit(): void {
        this.candidateType$ = this.candidateTypeSV.getAll()
    }

    search(query){
        if(query) this.studentList$ = this.userSV.search('?'+query)
    }

    deleteUser(id){
        this.swSV.confirm().pipe(
            filter(x => x.isConfirmed),
            switchMap(x => {
                return this.userSV.deleteData(id).pipe(
                    tap(x => this.swSV.removeSuccess()),
                    tap(x => this.studentList$ = this.userSV.search('?examinee_card=-')),
                    tap(x => this.cdr.detectChanges())
                )
            })
        ).subscribe()
    }


}
