import { ErrorStateMatcher } from '@angular/material/core';
import { FormControl, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Component, Inject, OnInit } from '@angular/core';

@Component({
  selector: 'app-end-order',
  templateUrl: './end-order.component.html',
  styleUrls: ['./end-order.component.css']
})
export class EndOrderComponent implements OnInit {
  idOrder: number;
  idReg: number;
  observation = new FormControl('', [Validators.required, Validators.minLength(50)]);
  errorMatcher = new ErrorStateMatcher();

  constructor(
    @Inject(MAT_DIALOG_DATA) private data: any,
    private _dialogRef: MatDialogRef<EndOrderComponent>
  ) { 
    this.idOrder = 0;
    this.idReg = 0;
  }

  ngOnInit(): void {
    this.idOrder = this.data.idOrder;
    this.idReg = this.data.idReg;
    this.observation.valueChanges.subscribe((_) => {
      this.observation.markAsTouched();
    })
  }

  closeEndDialog(){
    if(this.observation.invalid){
      this.observation.markAsTouched();
    }else{
      this._dialogRef.close(this.observation.value!);
    }
  }
}
