import { ApiService } from './../../../../../core/api/api.service';
import {ChangeDetectorRef, Component, Inject, OnInit, ViewChild} 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-servicios',
	templateUrl: './modal-filtros-servicios.component.html',
	styleUrls: ['./modal-filtros-servicios.component.scss']
})
export class ModalFiltrosServiciosComponent implements OnInit {

	form: FormGroup;
	loading: boolean = false;
	sucursales = null;
	servicios = null;
	tiposServicios = null;
	estatusServicios = null;
	origenes = null;
	startDate = null;

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

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

	ngOnInit() {
		if(this.data){

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

			this.form.get('fecha_inicio').setValue(addDays(new Date(this.data.fecha_inicio+'T00:00:00.000Z'), 1));
			this.form.get('fecha_final').setValue(addDays(new Date(this.data.fecha_final+'T00:00:00.000Z'), 1));
		}
	}

	initLoginForm() {
		this.form = this.fb.group({
			sucursal: new FormControl({value: null, disabled: true}, [Validators.required]),
			fecha_inicio: new FormControl({value: null, disabled: false}, [Validators.required]),
			fecha_final: new FormControl({value: null, disabled: false}, [Validators.required]),
			servicio: new FormControl({value: 0, disabled: true}, [Validators.required]),
			tipo_servicio: new FormControl({value: 0, disabled: true}, [Validators.required]),
			estado_servicio: new FormControl({value: 0, disabled: true}, [Validators.required]),
			origen: new FormControl({value: 0, 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');
		this.dialogRef.close(data);
	}

	filtros(){
		this.api.reportesService.filtros('sucursales').subscribe(res => {
			this.sucursales = res.data;

			if(this.data)
				this.form.get('sucursal').setValue(this.data.sucursal);

			this.form.controls['sucursal'].enable();
		}, () => {

		});

		this.api.reportesService.filtros('servicios').subscribe(res => {
			this.servicios = res.data;

			if(this.data)
				this.form.get('servicio').setValue(this.data.servicio);

			this.form.controls['servicio'].enable();
		}, () => {

		});

		this.api.reportesService.filtros('tipos_servicio').subscribe(res => {
			this.tiposServicios = res.data;

			if(this.data)
				this.form.get('tipo_servicio').setValue(this.data.tipo_servicio);

			this.form.controls['tipo_servicio'].enable();
		}, () => {

		});

		this.api.reportesService.filtros('estatus_servicio').subscribe(res => {
			this.estatusServicios = res.data;

			if(this.data)
				this.form.get('estado_servicio').setValue(this.data.estado_servicio);

			this.form.controls['estado_servicio'].enable();
		}, () => {

		});

		this.api.reportesService.filtros('origenes').subscribe(res => {
			this.origenes = res.data;

			if(this.data)
				this.form.get('origen').setValue(this.data.origen);

			this.form.controls['origen'].enable();
		}, () => {

		});
	}

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