import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ResourcesService } from '../../../../services/resources.service';
import { EncService } from '../../../../services/enc.service';

@Component({
    selector: 'app-no-measures-order-validation',
    templateUrl: './no-measures-order-validation.component.html',
    styleUrl: './no-measures-order-validation.component.css',
    standalone: false
})
export class NoMeasuresOrderValidationComponent implements OnInit {
  idOrder: string;
  formGroup: FormGroup;

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _resourcesService: ResourcesService,
    private _encService: EncService,
    private _dialogRef: MatDialogRef<NoMeasuresOrderValidationComponent>,
  ) {
    this.idOrder = '';
    this.formGroup = new FormGroup({
      validatedByControl: new FormControl('', Validators.required),
      recordControl: new FormControl('', Validators.required),
      validation: new FormControl(''),
    });
  }

  ngOnInit(): void {
    this.idOrder = this._data.idOrder;
  }

  async validateOrder(validation: string){
    if(this.formGroup.invalid){
      this._resourcesService.openSnackBar('Por favor ingrese todos los campos obligatorios');
      return;
    }

    this.formGroup.controls['validation'].setValue(validation);
    let formValue = this.formGroup.getRawValue();
    let formStr = JSON.stringify(formValue);
    let formEnc = await this._encService.encrypt(formStr);

    this._dialogRef.close(formEnc);
  }
}
