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-company-jobs',
  templateUrl: './jobs.component.html',
  styleUrls: [ './jobs.component.css' ],
  imports: [ FormsModule, CommonModule, RouterModule ]
})
export class CompanyJobsComponent {

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

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

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

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

}