import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { WorkflowService } from 'src/app/services/samwf/workflow.service';
import {
  MatDialog,
  MatDialogRef,
  MAT_DIALOG_DATA,
} from '@angular/material/dialog';

@Component({
  selector: 'app-workflow-form',
  templateUrl: './workflow-form.component.html',
  styleUrls: ['./workflow-form.component.css'],
})
export class WorkflowFormComponent implements OnInit {
  formGroup: FormGroup;
  modules: any[] = [];
  forms: any = [];

  constructor(
    public dialogRef: MatDialogRef<any>,
    @Inject(MAT_DIALOG_DATA) public data: any,
    private _samwfService: WorkflowService
  ) {
    this.modules = data.modules;
    this.forms = data.forms;
    console.log(this.forms);

    this.formGroup = this.createFormGroup();
  }

  private createFormGroup() {
    if (this.data.action == 'Registrar') {
      return new FormGroup({
        name: new FormControl('', Validators.required),
        description: new FormControl('', Validators.required),
        diagram: new FormControl([]),
        tasks: new FormControl([]),
        fields: new FormControl([]),
        id_module: new FormControl(''),
        module: new FormControl('', Validators.required),
        isAutomatic: new FormControl('', Validators.required),
        isAvailable: new FormControl(true),
        isForm: new FormControl(''),
        id_form: new FormControl(''),
      });
    } else {
      return new FormGroup({
        name: new FormControl(this.data.item.name, Validators.required),
        description: new FormControl(
          this.data.item.description,
          Validators.required
        ),
        diagram: new FormControl(this.data.item.diagram),
        tasks: new FormControl(this.data.item.tasks),
        fields: new FormControl(this.data.item.fields),
        module: new FormControl(
          this.modules[
            this.modules.findIndex(
              (object) => object.id == this.data.item.id_module
            )
          ].id,
          Validators.required
        ),
        id_module: new FormControl(
          this.modules[
            this.modules.findIndex(
              (object) => object.id == this.data.item.id_module
            )
          ].id
        ),
        isAutomatic: new FormControl(
          this.data.item.isAutomatic,
          Validators.required
        ),
        isAvailable: new FormControl(this.data.item.isAvailable),
        isCompleted: new FormControl(this.data.item.isCompleted),
        isStopped: new FormControl(this.data.item.isStopped),
        isForm: new FormControl(this.data.item.isForm),
        id_form: new FormControl(this.data.item.id_form),
      });
    }
  }

  sendData(dataForm: any) {
    console.log(dataForm);

    if (this.isForm?.value == false) {
      this.dialogRef.close(dataForm);
    } else {
      /* let positionForm = this.forms.findIndex((data: any) => {
        console.log(data.CODIGO_FORM + '==' + dataForm.id_form);

        return data.CODIGO_FORM == dataForm.id_form.CODIGO_FORM;
      });
      console.log(this.forms[positionForm].DATA_FORM.fields[0]);

      dataForm.form = this.forms[positionForm].DATA_FORM.fields[0].form;
      dataForm.id_form = dataForm.id_form.CODIGO_FORM; */
      this.dialogRef.close(dataForm);
    }
  }

  ngOnInit(): void {}

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

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

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

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

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

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

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

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

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

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