import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { MatDialog, MatTableDataSource } from '@angular/material';
import { ApiService } from '../../../../core/api/api.service';
import { CapacidadAprovechadaModel } from './capacidad-aprovechada.model';
import { ModalFiltrosCapacidadAprovechadaComponent } from './modal-filtros-capacidad-aprovechada/modal-filtros-capacidad-aprovechada.component';

@Component({
  selector: 'kt-capacidad-aprovechada',
  templateUrl: './capacidad-aprovechada.component.html',
  styleUrls: ['./capacidad-aprovechada.component.scss']
})
export class CapacidadAprovechadaComponent implements OnInit {

	titulo = 'Reporte de capacidad aprovechada';

	displayedColumns: string[] = ['numero_solicitud', 'fecha_servicio', 'hora_servicio', 'servicio', 'costo', 'tipo_servicio', 'estatus_servicio', 'colonia_cliente', 'nombre_asesor', 'nombre_auxiliar', 'duracion_servicio'];
	dataSource: MatTableDataSource<CapacidadAprovechadaModel>;

	//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;
	totalCosto = 0.00;
	totalDuracion = '00h 00m 00s';

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

	ngOnInit() {
	}

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

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

			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_capacidad/' + res.nombre_archivo;
					}
				}else{
					this.totalServicios = 0;
					this.totalCosto = 0.00;
					this.totalDuracion = '00h 00m 00s';
					this.dataSource.data = [];

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

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

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

		dialogRef.afterClosed().subscribe(data => {
			if(data){
				this.showMessageSetFilters = false;
				this.data = data;
				this.dataSource.data = [];
				this.titulo = 'Reporte de capacidad aprovechada (' + data.vehiculo + ')';
				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();
	}
}
