import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { ExcelModel } from '../model/excel.model';
import { map, tap } from 'rxjs/operators';
import { ExcelContentModel } from '../model/excel-content.model';

@Injectable({
  providedIn: 'root'
})
export class ExcelService {
  url = environment.url
  createStatus: boolean = true
  constructor(private http: HttpClient) { }

  getListExcel(status?: string): Observable<ExcelModel[]> {
    if(status){
      return this.http.get<ExcelModel[]>(this.url + "excel-center/upload/lists?status="+status)
      }else{
      return this.http.get<ExcelModel[]>(this.url + "excel-center/upload/lists")
    }
  }
  getListExcelContent(status?: string): Observable<ExcelContentModel[]> {
    return this.http.get<ExcelContentModel[]>(this.url + "excel-center/content/lists")
  }
  getExcelById(logId:string): Observable<ExcelModel> {
    return this.http.get<ExcelModel>(this.url + "excel-center/upload/"+logId)
  }
  downloadFile(logId:string):Observable<any>{
    return this.http.get(this.url + "excel-center/files/download/"+logId,  { responseType: 'blob'  })
  }
  downloadFileContent(logId:string):Observable<any>{
    return this.http.get(this.url + "excel-center/files/download/content/"+logId, { responseType: 'blob'  });
  }

  createExcel(model: ExcelModel): Observable<any> {
    let body : any = model
    if (body.group.groupId=='') {
      body.group = undefined
    }
    return this.http.post(this.url + 'excel-center', body)
  }
  deleteExcel(model: ExcelModel): Observable<any> {
    let body = {
      logId: model.logId
    }
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<any>(this.url + 'excel-center/upload', option)
  }
  deleteExcelContent(model: ExcelContentModel): Observable<any> {
    let body = {
      excelId: model.excelId
    }
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<any>(this.url + 'excel-center/content', option)
  }
  approve(model: ExcelModel): Observable<any> {
    let body = {
      logId: model.logId,
      status: model.status
    }
    return this.http.put<any>(this.url + "excel-center/approve",body)
  }

  getCount(status?: string): Observable<number> {
    return this.http.get<number>(this.url + "excel-center/upload/count")
  }
  getCountContent(status?: string): Observable<number> {
    return this.http.get<number>(this.url + "excel-center/content/count")
  }
}
