import { AfterViewInit, ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { MatDialog, MatTableDataSource } from '@angular/material';
import { ApiService } from '../../../../core/api/api.service';
import { ModalFiltrosServiciosAsesorComponent } from './modal-filtros-servicios-asesor/modal-filtros-servicios-asesor.component';
import { ServiciosAsesorModel } from './servicios-asesor.model';

@Component({
	selector: 'kt-servicios-asesor',
	templateUrl: './servicios-asesor.component.html',
	styleUrls: ['./servicios-asesor.component.scss']
})
export class ServiciosAsesorComponent implements OnInit, AfterViewInit {

	titulo = 'Reporte de servicios otorgados asesor';

	displayedColumns: string[] = ['numero_solicitud', 'fecha_servicio', 'hora_servicio', 'servicio', 'costo', 'tipo_servicio', 'estatus_servicio', 'nombre_cliente', 'colonia_cliente', 'vehiculo', 'duracion_servicio', 'litraje'];
	dataSource: MatTableDataSource<ServiciosAsesorModel>;

	//Loadings
	isLoading = false;
	showMessageWait = false;
	showMessageSetFilters = true;

	//Paginator, Searcher
	page = '1';
	perPage = '10';
	total = 0;
	sortBy = 'denominacion';
	order = 'asc';
	pages = [10, 25, 50, 100];
	deleted = '0';

	data = null;
	totalServicios = 0;
	totalDuracion = '00hr 00m 00s'

	constructor(private api: ApiService, private ref: ChangeDetectorRef, private dialog: MatDialog) {
		this.dataSource = new MatTableDataSource<ServiciosAsesorModel>();
	}

	ngOnInit() {
	}

	getInformation(excel = false){
		if(this.data){
			this.isLoading = true;
			this.showMessageWait = true;

			let url = excel? 'servicios/asesores/excel' : 'servicios/asesores';

			this.api.reportesService.getInformation(url, this.data, this.page, this.perPage, this.deleted).subscribe(res => {

				this.isLoading = false;
				this.showMessageWait = false;

				if(excel){
					if (res.nombre_archivo) {
						document.location.href = this.api.reportesService.baseUrlStorage + 'servicios_asesores/' + res.nombre_archivo;
					}
				}else{
					this.totalServicios = 0;
					this.totalDuracion = '00h 00m 00s';
					this.dataSource.data = [];

					this.dataSource.data = res.data;
					this.total = res.total;
					this.totalServicios = res.total;
					this.totalDuracion = res.total_duracion;
				}

				this.ref.detectChanges();
			}, () => {
				this.isLoading = false;
				this.showMessageWait = false;
			});
		}
	}

	filters(){
		const dialogRef = this.dialog.open(ModalFiltrosServiciosAsesorComponent, {
			data: this.data,
			disableClose: true
		});

		dialogRef.afterClosed().subscribe(data => {
			if(data){
				this.showMessageSetFilters = false;
				this.data = data;
				this.dataSource.data = [];
				this.titulo = 'Reporte de servicios otorgados (' + data.user + ')';
				this.getInformation();
			}
		});
	}

	sort(event) {
		this.order = event.direction;
		this.sortBy = event.active;
		this.getInformation();
	}

	paginator(event) {
		let nextPage = event.pageIndex + 1;
		this.page = nextPage.toString();
		this.perPage = event.pageSize.toString();
		this.getInformation();
	}

	ngAfterViewInit() {
		this.filters();
	}
}
