import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import{environment} from 'src/environments/environment';
import { ApiRequestService } from '../api/api-request.service';
import { HealthCheckup } from 'src/app/models/fnd/health_checkup';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class HealthCheckupService {

  baseUrl = 'HealthCheckup'
  constructor(private _http: HttpClient,
    private apiRequest: ApiRequestService) { }

  getHealthCheckups(): Observable<HealthCheckup[]> {
    return this.apiRequest.get(`${this.baseUrl}/HealthCheckup`);
  }

  getHealthCheckupById(id: number): Observable<HealthCheckup> {
    return this.apiRequest.get(`${this.baseUrl}/HealthCheckup/${id}`);
  }

  saveHealthCheckup(data: string, files: File[]): Observable<any> {
    const formData: FormData = new FormData();
    formData.append('data',  JSON.stringify(data));

    if (files && files.length > 0) {
      // console.log("if Condition",files);
      // for (const file of files) {
      //   console.log("if Condition",file);
      // formData.append('files', file, file.name);
      // }
      for (let i = 0; i < files.length; i++) {
        formData.append('files', files[i]);
              console.log("if Condition",files[i]);
      }
    }

    return this.apiRequest.postFormData(`${this.baseUrl}/HealthCheckup`, formData);
  }

  updateHealthCheckup(id: number, data: HealthCheckup): Observable<HealthCheckup> {
    return this.apiRequest.put(`${this.baseUrl}/HealthCheckup/${id}`, data);
  }

  deleteHealthCheckup(id: number): Observable<any> {
    return this.apiRequest.delete(`${this.baseUrl}/HealthCheckup/${id}`);
  }

  //redeploy
  public getFileContent(healthId: number): Observable<any> {
    console.log(healthId)
    const url = `HealthCheckupfile/readeploy/${healthId}`;
    return this.apiRequest.get(url);
  }

  //files

  getAllHealthCheckupFiles(healthId: number): Observable<any> {
    return this.apiRequest.get(`HealthCheckupfile/HealthCheckupfile/${healthId}`);
  }

  readFile(fileId: number): Observable<any> {
    return this.apiRequest.get(`HealthCheckupfile/readfile/${fileId}`);
  }

//checkup
  getHealthCheckup(jobtype: string): Observable<any> {
    const url = `${this.baseUrl}/healthcheckup?jobtype=${jobtype}`;
    return this.apiRequest.get(url);
  }
}
