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-encuesta-general',
	templateUrl: './modal-filtros-encuesta-general.component.html',
	styleUrls: ['./modal-filtros-encuesta-general.component.scss']
})
export class ModalFiltrosEncuestaGeneralComponent implements OnInit {

	form: FormGroup;
	loading: boolean = false;
	sucursales = null;
	usuarios = [];
	months = [];
	years = [];

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

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

	ngOnInit() {}

	initLoginForm() {
		let sucursal = this.data? this.data.sucursal : null;
		let mes = this.data? this.data.mes : null;
		let year = this.data? this.data.year : null;
		let operador_id = this.data? this.data.operador_id : null;

		this.form = this.fb.group({
			sucursal: new FormControl({value: sucursal, disabled: true}, [Validators.required]),
			mes: new FormControl({value: mes, disabled: true}, [Validators.required]),
			year: new FormControl({value: year, disabled: true}, [Validators.required]),
			operador_id: new FormControl({value: operador_id, disabled: true}, [Validators.required]),
		});
	}

	setFilters(){
		let data = this.form.value;

		let user = this.searchUserById(data.operador_id);
		data.user = user.nombre + ' ' + user.apellido_paterno + ' ' + user.apellido_materno;

		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;
		});
	}

	getUsers(reOpen = false){
		let sucursal = this.form.value.sucursal;
		let mes = this.form.value.mes;
		let year = this.form.value.year;

		if(sucursal && mes && year){
			this.changeStatusInputs(false);
			this.loading = true;
			this.usuarios = [];

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

			this.api.reportesService.getInformation('filtros/asesores', this.form.value).subscribe(res => {
				this.usuarios = 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['mes'].enable();
			this.form.controls['year'].enable();
			this.form.controls['operador_id'].enable();
		}else{
			this.form.controls['sucursal'].disable();
			this.form.controls['mes'].disable();
			this.form.controls['year'].disable();
			this.form.controls['operador_id'].disable();
		}
	}

	searchUserById(valor){
		let index = this.usuarios.findIndex((user => valor == user.id));
		if(index != -1){
			return this.usuarios[index];
		}
		return false;
	}


	setStaticSelects(){
		this.months = [
			{
				id: 1,
				nombre: 'Enero'
			},
			{
				id: 2,
				nombre: 'Febrero'
			},
			{
				id: 3,
				nombre: 'Marzo'
			},
			{
				id: 4,
				nombre: 'Abril'
			},
			{
				id: 5,
				nombre: 'Mayo'
			},
			{
				id: 6,
				nombre: 'Junio'
			},
			{
				id: 7,
				nombre: 'Julio'
			},
			{
				id: 8,
				nombre: 'Agosto'
			},
			{
				id: 9,
				nombre: 'Septiembre'
			},
			{
				id: 10,
				nombre: 'Octubre'
			},
			{
				id: 11,
				nombre: 'Noviembre'
			},
			{
				id: 12,
				nombre: 'Diciembre'
			}
		];

		let startYear = 2018;
		let endYear = new Date().getFullYear() + 30;

		for(let i = startYear; i <= endYear ; i++){
			this.years.push({id: i, nombre: i});
		}

		this.form.controls['mes'].enable();
		this.form.controls['year'].enable();
	}
}
