import { ApiService } from './../../../../../core/api/api.service';
import {ChangeDetectorRef, Component, Inject, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from "@angular/material/dialog";

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

	form: FormGroup;
	loading: boolean = false;
	sucursales = null;
	vehiculos = [];
	origenes = null;
	startDate = null;

	addDays = (date: Date, days: number): Date => {
		let result = new Date(date);
		result.setDate(result.getDate() + days);
		return result;
	};

	constructor(private fb: FormBuilder,
				public dialog: MatDialog,
				public dialogRef: MatDialogRef<ModalFiltrosCapacidadAprovechadaComponent>,
				private api: ApiService,
				private cdr: ChangeDetectorRef,
				@Inject(MAT_DIALOG_DATA) public data: any) {

		this.initLoginForm();
		this.filtros();
	}

	ngOnInit() {}

	initLoginForm() {
		let fechaInicio = this.data? this.addDays(new Date(this.data.fecha_inicio+'T00:00:00.000Z'), 1) : null;
		let fechaFinal = this.data? this.addDays(new Date(this.data.fecha_final+'T00:00:00.000Z'), 1) : null;
		let sucursal = this.data? this.data.sucursal : null;
		let vehiculo_id = this.data? this.data.vehiculo_id : null;

		this.form = this.fb.group({
			sucursal: new FormControl({value: sucursal, disabled: true}, [Validators.required]),
			fecha_inicio: new FormControl({value: fechaInicio, disabled: false}, [Validators.required]),
			fecha_final: new FormControl({value: fechaFinal, disabled: false}, [Validators.required]),
			vehiculo_id: new FormControl({value: vehiculo_id, disabled: true}, [Validators.required]),
		});
	}

	setFilters(){
		let data = this.form.value;
		data.fecha_inicio = new Date(data.fecha_inicio).toLocaleDateString('en-CA');
		data.fecha_final = new Date(data.fecha_final).toLocaleDateString('en-CA');

		let vehiculo = this.searchVehiculoById(data.vehiculo_id);
		data.vehiculo = vehiculo.num_economico;

		this.dialogRef.close(data);
	}

	filtros(){
		this.loading = true;
		this.api.reportesService.filtros('sucursales').subscribe(res => {
			this.form.controls['sucursal'].enable();

			this.sucursales = res.data;

			if(this.data)
				this.getUsers(true);

			this.loading = false;
		}, () => {
			this.loading = false;
		});
	}

	dateChanged(dateModal: any){
		if(dateModal){
			this.startDate = this.form.value.fecha_inicio;
			this.form.get('fecha_final').reset();
			dateModal.open();
		}

		this.getUsers();
	}

	getUsers(reOpen = false){
		let sucursal = this.form.value.sucursal;
		let fechaInicio = this.form.value.fecha_inicio;
		let fechaFinal = this.form.value.fecha_final;

		if(sucursal && fechaInicio && fechaFinal){
			this.changeStatusInputs(false);
			this.loading = true;
			this.vehiculos = [];

			if(!reOpen)
				this.form.get('vehiculo_id').reset();

			let data = this.form.value;
			data.fecha_inicio = new Date(data.fecha_inicio).toLocaleDateString('en-CA');
			data.fecha_final = new Date(data.fecha_final).toLocaleDateString('en-CA');

			this.api.reportesService.getInformation('servicios/capacidad/vehiculos', data).subscribe(res => {
				this.vehiculos = res.data;
				this.changeStatusInputs(true);
				this.loading = false;
				this.cdr.detectChanges();
			}, () => {
				this.changeStatusInputs(true);
				this.loading = false;
			});
		}
	}

	changeStatusInputs(enable){
		if(enable){
			this.form.controls['sucursal'].enable();
			this.form.controls['fecha_inicio'].enable();
			this.form.controls['fecha_final'].enable();
			this.form.controls['vehiculo_id'].enable();
		}else{
			this.form.controls['sucursal'].disable();
			this.form.controls['fecha_inicio'].disable();
			this.form.controls['fecha_final'].disable();
			this.form.controls['vehiculo_id'].disable();
		}
	}

	searchVehiculoById(valor){
		let index = this.vehiculos.findIndex((vehiculo => valor == vehiculo.id));
		if(index != -1){
			return this.vehiculos[index];
		}
		return false;
	}
}
