import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import { isNullOrUndefined } from 'util';
import { catchError } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { ResourceBase } from '../../../core/api/resource';
import { environment } from '../../../../environments/environment';

@Injectable({
	providedIn: 'root'
})
export class ReportesService extends ResourceBase {

	baseUrl = environment.API + 'reportes/';
	baseUrlStorage = environment.STORAGE + 'reportes/';

	constructor(public http: HttpClient) {
		super(http, '');
	}

	public getInformation(url: string, body: any, page = '1', perPage = '9999', deleted_at = '0'): Observable<any> {

		const params = new HttpParams()
			.set('page', isNullOrUndefined(page) ? '1' : page)
			.set('perPage', isNullOrUndefined(perPage) ? '10' : perPage)
			.set('deleted', isNullOrUndefined(deleted_at) ? '0' : deleted_at);

		const options = {params: params};

		return this.http.post(this.baseUrl + url, body, options).pipe(
			catchError(this.handleError('create'))
		);
	}

	public filtros(url: string, page: string = '1', perPage: string = '9999', deleted_at: string = '0'): Observable<any> {

		const params = new HttpParams()
			.set('page', isNullOrUndefined(page) ? '1' : page)
			.set('perPage', isNullOrUndefined(perPage) ? '10' : perPage)
			.set('deleted', isNullOrUndefined(deleted_at) ? '0' : deleted_at);

		const options = {params: params};

		return this.http.get(this.baseUrl + 'filtros/' + url, options).pipe(
			catchError(this.handleError('index'))
		);
	}

	public getChartReports($url){
		return this.http.get(this.baseUrl + 'reporte/' + $url).pipe(
			catchError(this.handleError('index'))
		);
	}
}
