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-getbfo-addcondition',
  templateUrl: './getbfo-addcondition.component.html',
  styleUrls: ['./getbfo-addcondition.component.css']
})
export class GetbfoAddconditionComponent implements OnInit {

  public formGroup: FormGroup
  constructor(@Inject(MAT_DIALOG_DATA) public data: Array<any>) { 
    this.formGroup = this.createFormGroup();
   }

  ngOnInit(): void {
  }

  public dataSelect(column:string, table:string, type:string){
    type === 'cond1' ? this.cond1?.setValue( { table: table, column: column} ) : this.cond2?.setValue( { table: table, column: column} );
  }

  private createFormGroup(){
    return new FormGroup({
      isCond2Column: new FormControl(false, Validators.required),
      isCond2Request: new FormControl(false, Validators.required),
      cond1: new FormControl('', Validators.required),
      ope: new FormControl('', Validators.required),
      cond2: new FormControl('', Validators.required)
    });
  }

  public checkCond2(ope:number){
    if(ope === 1){
      if(!this.isCond2Column?.value) {
        this.isCond2Request?.setValue(false);
        this.cond2?.enable();
        this.cond2?.setValue('');
      }
    }else{
      if(!this.isCond2Request?.value) {
        this.isCond2Column?.setValue(false);
        this.cond2?.setValue('');
        this.cond2?.disable();
      }else{
        this.cond2?.enable();
        this.cond2?.setValue('');
      }
    }
  }

  get isCond2Column(){ return this.formGroup.get('isCond2Column');}
  get isCond2Request(){ return this.formGroup.get('isCond2Request');}
  get cond1(){ return this.formGroup.get('cond1');}
  get ope(){ return this.formGroup.get('ope');}
  get cond2(){ return this.formGroup.get('cond2');}

}
