import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {SubjectsService} from '../../../../../core/service/settings/subjects.service';
import {Observable, throwError} from 'rxjs';
import {TestingRegisterService} from '../../../../../core/service/testing-register/testing-register.service';
import {VEligibleCandidatesService} from '../../../../../core/service/testing-register/v-eligible-candidates.service';
import {MatDialog} from '@angular/material/dialog';
import {DialogSyncComponent} from '../../presenter/dialog/dialog-sync/dialog-sync.component';
import {catchError, concatMap, tap} from 'rxjs/operators';
import {BranchService} from '../../../../../core/service/settings/branch.service';
import {TestingCenterService} from '../../../../../core/service/TestingCenter/testing-center.service';
import {TestingFieldService} from '../../../../../core/service/settings/testing-field.service';
import {TestingInformationService} from 'src/app/core/service/testing-information/testing-information.service';
import {
    EligibleCandidateService
} from '../../../../../core/service/report/eligible_candidate/eligible-candidate.service';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';

@Component({
    selector: 'app-candidate-list',
    templateUrl: './candidate-list.container.html',
    styleUrls: ['./candidate-list.container.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class CandidateListContainer implements OnInit {
    testinginformationuid;
    subjectList2 = [];
    subjectList = [];
    subjectList1$ = new Observable();
    subjectList3 = [];
    subjectList4 = [];
    testingInfoData;

    constructor(
        private router: Router,
        public activeRouter: ActivatedRoute,
        private VECandidateSV: VEligibleCandidatesService,
        private ReportEligibleCandidate: EligibleCandidateService,
        private subjectSV: SubjectsService,
        private branchSV: BranchService,
        private testingFieldSV: TestingFieldService,
        private testcenterSV: TestingCenterService,
        private dialog: MatDialog,
        private cdr: ChangeDetectorRef,
        private swSV: SweetalertService,
        private testingInfoSV: TestingInformationService
    ) {
    }

    ngOnInit(): void {
        this.activeRouter.params.pipe(
            tap(x => this.testinginformationuid = x.id),
            tap(x => {
                this.subjectList1$ = this.VECandidateSV.getAllByTestinUid(x.id);
            }),
            concatMap(x => this.testingInfoSV.get(this.testinginformationuid)),
            tap(x => this.testingInfoData = x)
        ).subscribe();
        this.subjectSV.getAll().subscribe(
            result => this.subjectList4 = result
        );
        this.branchSV.getAll().subscribe(
            result => this.subjectList = result
        );
        this.testingFieldSV.getAll().subscribe(
            result => {
                this.subjectList2 = result;
            }
        );
        this.testcenterSV.getAll().subscribe(
            result => this.subjectList3 = result
        );
    }

    search(query) {
        query += `&testing_information_uid=${this.testinginformationuid}`;
        if (query) return this.subjectList1$ = this.VECandidateSV.search(`?${query}`);
        if (!query) return this.subjectList1$ = this.VECandidateSV.getAllByTestinUid(this.testinginformationuid);
    }

    export(testInforUid) {
        this.ReportEligibleCandidate.getReportWithTestInforUid(testInforUid)
            .subscribe(
                res => {
                },
                error => {
                    this.swSV.errText('ไม่พบข้อมูล');
                });
    }

    // syncData(data){
    //   console.log(data)
    // }

    syncData(data) {
        // console.log(id);
        const dialogRef = this.dialog.open(DialogSyncComponent, {
            width: '500px',
            height: '340px',
        });
        dialogRef.afterClosed().pipe(
            tap(x => {
                this.VECandidateSV.getAll().pipe(
                    tap(y => this.subjectList1$ = this.VECandidateSV.getAll()),
                    tap(y => this.cdr.detectChanges())
                ).subscribe();
            }),
        ).subscribe();
    }

}
