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

@Component({
  selector: 'app-dispatch-form',
  templateUrl: './dispatch-form.component.html',
  styleUrls: ['./dispatch-form.component.css']
})
export class DispatchFormComponent implements OnInit {

  public titleDialog:string;
  public formGroup: FormGroup;
  constructor(@Inject(MAT_DIALOG_DATA) public element: any) {

    this.titleDialog = element.typeForm === 'REG' ? 'Registro de Despacho' : 'Modificación de Despacho';
    this.formGroup = this.createFormGroup();
  }

  private createFormGroup(): FormGroup{
    return new FormGroup({
      despacho: new FormControl(this.element.typeForm === 'UPD' ? this.element.dataElement.DESPACHO : '', Validators.required),
      calle: new FormControl(this.element.typeForm === 'UPD' ? this.element.dataElement.CALLE : '', Validators.required),
      numeroExterior: new FormControl(this.element.typeForm === 'UPD' ? this.element.dataElement.NUMERO_EXTERIOR : '', Validators.required),
      numeroInterior: new FormControl(this.element.typeForm === 'UPD' ? this.element.dataElement.NUMERO_INTERIOR : '', Validators.required),
      codigoPostal: new FormControl(this.element.typeForm === 'UPD' ? this.element.dataElement.CODIGO_POSTAL : '', Validators.required),
      municipio: new FormControl(this.element.typeForm === 'UPD' ? this.element.dataElement.MUNICIPIO : '', Validators.required),
      entidad: new FormControl(this.element.typeForm === 'UPD' ? this.element.dataElement.ENTIDAD : '', Validators.required),
    });
  }


  ngOnInit(): void {
  }

  get despacho() { return this.formGroup.get('despacho') }
  get calle() { return this.formGroup.get('calle') }
  get numeroExterior() { return this.formGroup.get('numeroExterior') }
  get numeroInterior() { return this.formGroup.get('numeroInterior') }
  get codigoPostal() { return this.formGroup.get('codigoPostal') }
  get municipio() { return this.formGroup.get('municipio') }
  get entidad() { return this.formGroup.get('entidad') }

}
