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 { Resume } from 'app/models/Resume';
import { Authentication } from 'app/services/Authentication';

@Component({
  selector: 'main-resumes',
  templateUrl: './resumes.component.html',
  styleUrls: [ './resumes.component.css' ],
  imports: [ FormsModule, CommonModule, RouterModule ]
})
export class ResumesComponent {

  public myResumes: Resume[] = [];
  public currentResume: Resume | null = null;

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

  ngOnInit(){
    this.http.get<Resume[]>("api/resume").subscribe({
      next: data => {
        this.myResumes = data;
      },
      error: err => {
        console.log("Error fetching resumes: " + err.error);
      }
    });
  }

  changeSelectedResume(ResumeID: number){
    this.http.get<Resume>("api/resume?ResumeID=" + ResumeID).subscribe({
      next: data => {
        this.currentResume = data;
      },
      error: err => {
        console.log("Error fetching resume ID: " + err.error);
      }
    });
  }

}