import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { takeUntil } from 'rxjs/operators';
import { UtilsService } from '../../../oneit/services/utils.service';
import { BaseComponent } from '../../base/base.component';
import { ApplicantJobModel } from '../../models/applicant-job.model';
import { JobService } from '../../services/job.service';

@Component({
    selector: 'app-jobs',
    templateUrl: './jobs-list.component.html',
    styleUrls: ['./jobs-list.component.scss']
})

export class JobsListComponent extends BaseComponent implements OnInit {

    assocs = [];
    includeCompleted = false;
    applicantJobs: ApplicantJobModel[] = [];

    constructor(
        private utilsService: UtilsService,
        private jobsService: JobService,
        private router: Router
    ) {
        super();
    }

    ngOnInit(): void {
        this.getJobApplicants();
    }

    getJobApplicants(): void {
        this.isLoading = true;
        this.jobsService.getApplicantTalentJobs({
            IncludeCompleted: this.includeCompleted,
            OnlyForLoggedin: true
        }, this.assocs)
            .pipe(takeUntil(this.componentInView))
            .subscribe(response => {
                this.isLoading = false;
                this.applicantJobs = response;
            }, err => {
                this.isLoading = false;
                this.utilsService.handleError(err);
            })
    }

    goToHome(): void {
        this.router.navigate(['/my-career-web/dashboard/home']);
    }

}
