import { Component, OnInit } from '@angular/core';
import { FormGroup, Validators } from '@angular/forms';
import { PayerEditComponent } from './payer-edit.component';

@Component({
  selector: 'page-payer-create-b2b',
  templateUrl: 'payer-edit-b2b.component.html',
  styleUrls: ['../address.component.scss'],
})
export class PayerEditB2BComponent extends PayerEditComponent {
  async initForm() {
    const fb = this.fb;
    let payerDTO = await this.payer$.toPromise();
    let isDefault = await this.isDefault$.toPromise();

    this.control = fb.group({
      gender: fb.control(payerDTO?.gender || undefined, [Validators.required, Validators.min(1)]),
      title: fb.control(payerDTO?.title),
      firstName: fb.control(payerDTO?.firstName, [Validators.required]),
      lastName: fb.control(payerDTO?.lastName, [Validators.required]),
      organisation: fb.group({
        name: fb.control(payerDTO?.organisation?.name, [Validators.required]),
        department: fb.control(payerDTO?.organisation?.department),
        vatId: fb.control(payerDTO?.organisation?.vatId),
      }),
      address: fb.group({
        street: fb.control(payerDTO?.address?.street, [Validators.required]),
        streetNumber: fb.control(payerDTO?.address?.streetNumber, [Validators.required]),
        zipCode: fb.control(payerDTO?.address?.zipCode, [Validators.required]),
        city: fb.control(payerDTO?.address?.city, [Validators.required]),
        info: fb.control(payerDTO?.address?.info),
        country: fb.control(payerDTO?.address?.country || 'DEU', [Validators.required]),
      }),
      isDefault: fb.control(isDefault),
    });
    this.cdr.markForCheck();
  }
}
