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

@Injectable({
  providedIn: 'root'
})
export class DocumentService {
  url = environment.url
  createStatus: boolean = true
  constructor(private http: HttpClient) { }
  getListDoc(status?: string): Observable<DocumentModel[]> {
    if (status) {
      return this.http.get<DocumentModel[]>(this.url + "document-center/upload/lists?status=" + status)
    } else {
      return this.http.get<DocumentModel[]>(this.url + "document-center/upload/lists")
    }
  }
  getListExcelContent(status?: string): Observable<DocumentContentModel[]> {
    return this.http.get<DocumentContentModel[]>(this.url + "document-center/content/lists")
  }
  getDocById(logId: string): Observable<DocumentModel> {
    return this.http.get<DocumentModel>(this.url + "document-center/upload/" + logId)
  }
  downloadFile(logId: string, lang: string): Observable<any> {
    return this.http.get(this.url + "document-center/files/download/" + logId + '?lang=' + lang, { responseType: 'blob' })
  }

  downloadFileContent(logId: string, lang: string): Observable<any> {
    return this.http.get(this.url + "document-center/files/download/content/" + logId + '?lang=' + lang, { responseType: 'blob' });
  }
  createDoc(model: DocumentModel): Observable<any> {
    let body : any = model
    if (body.group.groupId=='') {
      body.group = undefined
    }
    return this.http.post(this.url + 'document-center', body)
  }
  deleteExcel(model: DocumentModel): 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 + 'document-center/upload', option)
  }
  deleteExcelContent(model: DocumentContentModel): Observable<any> {
    let body = {
      docId: model.docId
    }
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<any>(this.url + 'document-center/content', option)
  }
  approve(model: DocumentModel): Observable<any> {
    let body = {
      logId: model.logId,
      status: model.status
    }
    return this.http.put<any>(this.url + "document-center/approve", body)
  }

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