import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { IAMService } from '../../../../services/iam/iam.service';
import {
  MatDialog,
  MatDialogRef,
  MAT_DIALOG_DATA,
} from '@angular/material/dialog';
import { USERInterface } from 'src/app/interfaces/user-interface';
import { ENCService } from 'src/app/services/enc/enc.service';

@Component({
  selector: 'app-mcomun-form',
  templateUrl: './mcomun-form.component.html',
  styleUrls: ['./mcomun-form.component.css'],
})
export class MCOMUNFORMComponent implements OnInit {
  formGroup: FormGroup;
  usuario_session:USERInterface = JSON.parse(localStorage.getItem('TIMUSERENC')!);

  constructor(
    public dialogRef: MatDialogRef<any>,
    private _encService: ENCService,
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {
    this.formGroup = this.createFormGroup();
  }

  private createFormGroup() {
    if (this.data.action == 'Registrar') {
      return new FormGroup({
        nombre: new FormControl('', Validators.required),
        rfc_unidad: new FormControl('', Validators.required),
        rfc_empresa: new FormControl(
          this.usuario_session.RFCEMPRESA,
          [ Validators.minLength(12)]
        ),
        id_usuario_registra: new FormControl(
          this._encService.desencriptar(this.usuario_session.IDUSUARIO),
          Validators.required
        ),
      });
    } else {
      return new FormGroup({
        nombre: new FormControl(
          this.data.item.NOMBREUNIDAD,
          Validators.required
        ),
        rfc_unidad: new FormControl(
          { value: this.data.item.RFCUNIDAD, disabled: true },
          Validators.required
        ),
        rfc_empresa: new FormControl(
          this._encService.desencriptar(this.usuario_session.RFCEMPRESA),
          Validators.required
        ),
        id_usuario_registra: new FormControl(
          this._encService.desencriptar(this.usuario_session.IDUSUARIO),
          Validators.required
        ),
      });
    }
  }

  ngOnInit(): void {}

  get nombre() {
    return this.formGroup.get('nombre');
  }

  get rfc_unidad() {
    return this.formGroup.get('rfc_unidad');
  }

  get rfc_empresa() {
    return this.formGroup.get('rfc_empresa');
  }

  get id_usuario_registra() {
    return this.formGroup.get('id_usuario_registra');
  }

  
}
