import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { EncService } from '../../../../services/enc.service';

@Component({
    selector: 'app-selector-option-edit',
    templateUrl: './selector-option-edit.component.html',
    styleUrl: './selector-option-edit.component.css',
    standalone: false
})
export class SelectorOptionEditComponent implements OnInit {
  valueControl: FormControl;
  labelControl: FormControl;

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _encService: EncService,
    private _dialogRef: MatDialogRef<SelectorOptionEditComponent>,
  ) {
    this.valueControl = new FormControl('', Validators.required);
    this.labelControl = new FormControl('', Validators.required);
  }

  ngOnInit(): void {
    let optionObj = JSON.parse(this._data.option);
    
    this.valueControl.setValue(optionObj.valueDec);
    this.labelControl.setValue(optionObj.label);
  }

  async save(){
    let dataObj = {
      value: this.valueControl.value,
      label: this.labelControl.value,
    };

    let dataStr = JSON.stringify(dataObj);
    let dataEnc = await this._encService.encrypt(dataStr);

    this._dialogRef.close(dataEnc);
  }
}
