import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { TemplateFileModel, TemplateModel } from '../model/template.model';
import { ModuleModel } from '../model/module.model';
import { ExcelPortalModel, ExcelPortalGroupModel, ExcelPortalTagsModel } from '../model/excel-portal.model';
import { AlertModel } from '../model/alert.model';

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

  constructor(private http: HttpClient,
    private translateService: TranslateService
  ) { }

  getExcelReport(itemId: string): Observable<any> {
    return this.http.get(environment.url + "irapi/menuitem/" + itemId + '?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47')
  }
  dowloadExcelReport(fileName: string, varname: string, data: string): Observable<any> {
    return this.http.get(environment.url + "irapi/excel/export-to-excel?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47&fileName=" + fileName + "&param=__varname=" + varname + "|__screen=XLSOPTION|" + data, { responseType: 'blob' })
  }
  getExcelList(): Observable<TemplateModel[]> {
    return this.http.get<TemplateModel[]>(environment.url + "template/lists?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47")
  }
  getModuleList(): Observable<ModuleModel[]> {
    return this.http.get<ModuleModel[]>(environment.url + "template/lists/module?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47")
  }
  getExcelPortalList(): Observable<ExcelPortalModel[]> {
    return this.http.get<ExcelPortalModel[]>(environment.url + "excel-center/content/lists")
  }
  getExcelPortalgGroupList(): Observable<ExcelPortalGroupModel[]> {
    return this.http.get<ExcelPortalGroupModel[]>(environment.url + "portal-group/lists")
  }
  getExcelPortalTagsList(): Observable<ExcelPortalTagsModel[]> {
    return this.http.get<ExcelPortalTagsModel[]>(environment.url + "tag/lists")
  }

  postTemplate(body: {
    templateId: string,
    tname: string,
    ename: string,
    tdesc: string,
    edesc: string,
    module: string
  }) {
    return this.http.post(environment.url + "template?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47", body)
  }
  deleteTemplate(body: TemplateModel): Observable<AlertModel> {
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<AlertModel>(environment.url + "template?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47", option)
  }
  postTemplateFile(body: TemplateFileModel): Observable<AlertModel> {
    return this.http.post<AlertModel>(environment.url + "template-file?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47", body)
  }
  deleteTemplateFile(body: TemplateFileModel): Observable<AlertModel> {
    let option = {
      headers: new HttpHeaders({
        "Content-Type": "application/json",
      }),
      body: body
    }
    return this.http.delete<AlertModel>(environment.url + "template-file?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47", option)
  }
  downloadTemplateFile(fileName: string) {
    return this.http.get(environment.url + "template-file/download/excel/" + fileName + "?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47", { responseType: 'blob' })
  }
  printExcelReport(body: {
    fileName: string,
    paramObj: string
  }) {
    return this.http.post(environment.url + "template-file/export-to-excel?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47", body, { responseType: 'blob' })
  }
  getTemplateFile(templateid: string, filename: string): Observable<AlertModel> {
    return this.http.get<AlertModel>(environment.url + "template-file/" + templateid + "/" + filename + "?companyid=eb2f4f30-edaf-11ee-a69a-c7680edc0e47",)
  }
}
