import { Component, OnInit } from '@angular/core';
import { IPluginComponent } from '@shared/plugins/interfaces/plugin-component';
import { UntypedFormBuilder, Validators } from '@angular/forms';

@Component({
  selector: 'app-order-product',
  templateUrl: './order-product.component.html',
  styleUrls: ['./order-product.component.scss']
})
export class OrderProductComponent implements OnInit, IPluginComponent {
  orderProdForm = this.formBuilder.group({
    cpanel_license_ip: ['', Validators.required]
  });
  public data: {
    cpanel_license_ip: string;
    valid: boolean;
  };

  constructor(private formBuilder: UntypedFormBuilder) { }

  updateData() {
    this.data.valid = this.orderProdForm.valid;
    if (this.data.valid) {
      this.data.cpanel_license_ip = this.orderProdForm.value.cpanel_license_ip;
    }
  }

  ngOnInit(): void {
    if (this.data?.cpanel_license_ip) {
      this.orderProdForm.patchValue(this.data);
    }
    this.updateData();
    this.orderProdForm.valueChanges.subscribe(value => {
      this.updateData();
    });
  }

}
