import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { NoMeasuresOrderDetailsField } from '../no-measures-order-details.component';
import { FormControl, Validators } from '@angular/forms';

@Component({
    selector: 'app-data-selector',
    templateUrl: './data-selector.component.html',
    styleUrl: './data-selector.component.css',
    standalone: false
})
export class DataSelectorComponent implements OnInit {
  tagData: string;
  valueData: string | null;
  orderData: NoMeasuresOrderDetailsField[];
  orderFormData: NoMeasuresOrderDetailsField[];
  currentFieldValue: FormControl;

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
  ) {
    this.tagData = '';
    this.valueData = null;
    this.orderData = [];
    this.orderFormData = [];
    this.currentFieldValue = new FormControl({value: '', disabled: true});
  }

  ngOnInit(): void {
    this.tagData = this._data.keyData;
    this.valueData = this._data.valueData;
    this.orderData = this._data.orderData;
    this.orderFormData = this._data.orderFormData;

    if(this.valueData == null){
      this.currentFieldValue.setValue('Ninguno');
    }else{
      this.currentFieldValue.setValue(this.valueData);
    }
  }

  newValueChange(value: string){
    if(value == 'OTR'){
      this.currentFieldValue.setValue('');
      this.currentFieldValue.setValidators(Validators.required);
      this.currentFieldValue.enable();
    }else{
      this.currentFieldValue.setValue(value);
      this.currentFieldValue.clearValidators();
      this.currentFieldValue.disable();

      this.valueData = value;
    }
  }

  valueInput(value: string){
    this.valueData = value;
  }
}
