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

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

  public MyJobListings: JobListing[] = [];
  public JobListingPage: JobListing[] = [];
  public ErrorMsg: string = "";

  public Page: number = 1;

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

    if (this.Page == 1){

    }

    http.get<JobListing[]>("api/joblisting?PageQuantity=" + 10 + "&Page=" + 1).subscribe({
      next: data => {
        this.JobListingPage = data;
      },
      error: err => {
        this.ErrorMsg = err.error;
      }
    });

  };

  RemoveJobListing( JobListingID: number ){
    this.http.delete("api/joblisting?JobListingID=" + JobListingID).subscribe({
      next: data => {
        window.location.reload();
      },
      error: err => {
        this.ErrorMsg = err.error;
      }
    });
  }

}