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

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

	baseUrl = environment.API;

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

	public index2(page?: string, searchText?: string, sortBy?: string, order?: string, perPage?: string, visible?: string, deleted_at?: string, fecha?: string, estatus_servicio?: string): Observable<any> {

		const params = new HttpParams()
			.set('sortBy', isNullOrUndefined(sortBy) ? '' : sortBy)
			.set('order', isNullOrUndefined(order) ? '' : order)
			.set('page', isNullOrUndefined(page) ? '1' : page)
			.set('perPage', isNullOrUndefined(perPage) ? '10' : perPage)
			.set('visible', isNullOrUndefined(visible) ? '1' : visible)
			.set('deleted', isNullOrUndefined(deleted_at) ? '0' : deleted_at)
			.set('query', searchText ? searchText : '')
			.set('fecha', fecha ? fecha : '')
			.set('estatus_servicio', estatus_servicio ? estatus_servicio : '');

		const options = {params: params};

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

	public updateLitraje(body: any, servicioProgresoId): Observable<any> {
		return this.http.post(this.baseUrl + 'atencionclientes/solicitud_servicios/' + servicioProgresoId + '/litraje', body).pipe(
			catchError(this.handleError('create'))
		);
	}

	public getQuiz(id){
		return this.http.get(this.baseUrl + 'atencionclientes/solicitud_servicios/'+ id +'/encuesta').pipe(
			catchError(this.handleError('index'))
		);
	}

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

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

		const options = {params: params};

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

	public formasPagos(url: string){
        return this.http.get(this.baseUrl + 'atencionclientes/solicitud_servicios/' + url).pipe(catchError(this.handleError('index')));
	}

	public clientesDomicilios(clienteID: string){
        return this.http.get(this.baseUrl + 'atencionclientes/solicitud_servicios/clientes/' + clienteID + '/domicilios').pipe(catchError(this.handleError('index')));
	}

	public origenes(url: string){
        return this.http.get(this.baseUrl + 'atencionclientes/solicitud_servicios/' + url).pipe(catchError(this.handleError('index')));
	}

	public filtrosServicios(url: string){
        return this.http.get(this.baseUrl + 'atencionclientes/solicitud_servicios/' + url).pipe(catchError(this.handleError('index')));
	}

	public validarDisponibilidad(body: any): Observable<any> {
		return this.http.post(this.baseUrl + 'atencionclientes/solicitud_servicios/validar_disponibilidad', body).pipe(
			catchError(this.handleError('create'))
		);
	}

	public deleteAuxiliar(id: number, auxiliarId): Observable<any> {
		return this.http.get(this.baseUrl + 'atencionclientes/solicitud_servicios/' + id + '/auxiliares/' + auxiliarId + '/remove?todos=0').pipe(
			catchError(this.handleError('index'))
		);
	}
}
