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 { JobListing } from 'app/models/JobListing';
import { Company } from 'app/models/Company';

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

  public selectedJob: JobListing | null = null;
  public jobsCompany: Company | null = null;
  public ErrorMsg: string = "";

  constructor( private http: HttpClient, private router: Router, private route: ActivatedRoute, private title: Title, public auth: Authentication ) {
    this.title.setTitle("Jobs - Viewer | BoredCareers");
  };

  ngOnInit(){
    this.route.queryParams.subscribe(params => {
      const JobID = params['JobID'];
      if (JobID){
        this.http.get<JobListing>( "api/joblisting/" + JobID ).subscribe({
          next: data => {
            this.selectedJob = data;
            this.http.get<Company>("api/company?CompanyID=" + this.selectedJob.companyID).subscribe({
              next: data => {
                this.jobsCompany = data;
              },
              error: err => {
                this.ErrorMsg = err.ErrorMsg;
              }
            })
          },
          error: err => {
            this.ErrorMsg = err.error;
          }
        })
      }else{
        this.router.navigate(["/"]);
      }
      if (this.selectedJob != null){
        
      }
    });
  }

}