import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { Router, ActivatedRoute, RouterModule } from '@angular/router';
import { Title } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { Authentication } from 'app/services/Authentication';
import { Application } from 'app/models/Application';
import { TranslationService } from 'app/services/translations';

@Component({
  selector: 'App-Viewer',
  templateUrl: './appviewer.component.html',
  styleUrls: [ './appviewer.component.css' ],
  imports: [ FormsModule, CommonModule, RouterModule ]
})
export class AppViewerComponent {
  public ErrorMsg: string = "";

  public List: Application[] = [];

  constructor( private http: HttpClient, private router: Router, private route: ActivatedRoute, private title: Title, public auth: Authentication, public strings: TranslationService  ) {
    this.title.setTitle("Applications | BoredCareers");
    if (!auth.isLoggedIn){
      router.navigate(["/"]);
    }
  };

  ngOnInit(){
    this.route.queryParams.subscribe(params => {
      const JobListingID = params['JobID'] ? +params['JobID'] : null;
      if (JobListingID !== null){
        this.http.get<Application[]>("api/application?JobListingID=" + JobListingID).subscribe({
          next: data => {
            this.List = data;
          },
          error: err => {
            this.ErrorMsg = err.error;
          }
        });
      }
    });
  }

  viewResume(app: Application) {
    window.open('/resumes/viewer?ResumeID=' + app.resumeID, '_blank');
  }

}