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


@Component({
	selector: 'kt-modal-set-litraje',
	templateUrl: './modal-set-litraje.component.html',
	styleUrls: ['./modal-set-litraje.component.scss']
})
export class ModalSetLitrajeComponent implements OnInit {

	form: FormGroup;
	loading: boolean = false;
	inputLength = [].constructor(6);
	arrCodigo = [];
	keyAux;
	id = 0;

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

	ngOnInit() {
		this.initLoginForm()
	}

	initLoginForm() {
		this.form = this.fb.group({
			litraje: [this.data.litraje, [Validators.required, Validators.minLength(1), Validators.maxLength(6), Validators.pattern('^-?[0-9]\\d*(\\.\\d{1,2})?$')]]
		});
	}

	saveLitraje(){
		this.loading = true;
		this.api.solicitudesServicioService.updateLitraje({litraje: this.form.value.litraje}, this.data.progreso_solicitud_id).subscribe(res => {
			this.loading = false;
			this.dialogRef.close(true);
		}, ()=>{
			this.loading = false;
		});
	}

	onKeyUp(){
		this.form.get('litraje').setValue(this.form.value.litraje.replace(/[^0-9]/g, ''));
	}
}
