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

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

  public formGroupUserDate: FormGroup;
  public arr_tables: any = [];
  public arr_columns: any = [];
  constructor(
    public dialogRef: MatDialogRef<GETBFOUSERSDATESComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {
    // Se obtienen los datos de las tablas seleccionadas con sus columnas
    this.arr_tables = data.data_table;
    this.formGroupUserDate = this.createFormUserDate();
  }

  ngOnInit(): void {
  }

  // Creación de formulario
  private createFormUserDate() {
    return new FormGroup({
      table: new FormControl('', Validators.required),
      user_reg: new FormControl({value:'', disabled:true}),
      date_reg: new FormControl({value:'', disabled:true}),
      user_mod: new FormControl({value:'', disabled:true}),
      date_mod: new FormControl({value:'', disabled:true}),
      timestamp: new FormControl({value:'', disabled:true}),
      where: new FormControl({value:'', disabled:true}),
    });
  }

  // Se inicializan las columnas según su tabla seleccionada
  public selectedTable(){
    // Se obtiene la información de la tabla (nombre de la tabla y columnas)
    let table = this.table?.value;
    // Se inicializa la variable de las columnas
    this.arr_columns = table.columns;

    // Se habilitan los demás campos
    this.user_reg?.enable();
    this.date_reg?.enable();
    this.user_mod?.enable();
    this.date_mod?.enable();
    this.timestamp?.enable();
    this.where?.enable();
  }

  public save(){
    this.table?.setValue(this.table?.value.table);
    return this.formGroupUserDate.value;
  }

  get table() {
    return this.formGroupUserDate.get('table');
  }
  get user_reg() {
    return this.formGroupUserDate.get('user_reg');
  }
  get date_reg() {
    return this.formGroupUserDate.get('date_reg');
  }
  get user_mod() {
    return this.formGroupUserDate.get('user_mod');
  }
  get date_mod() {
    return this.formGroupUserDate.get('date_mod');
  }
  get timestamp() {
    return this.formGroupUserDate.get('timestamp');
  }
  get where() {
    return this.formGroupUserDate.get('where');
  }
}
