import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { Member } from '@overflow/commons-typescript/model/member';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { DomainMember } from '@overflow/commons-typescript/model/domain';

@Component({
  selector: 'of-member-profile',
  templateUrl: './member-profile.component.html',
})
export class MemberProfileComponent implements OnInit, OnDestroy {
  @Input() domainMember: DomainMember;
  @Output() modify = new EventEmitter<Member>();

  form: FormGroup;

  constructor(
    private formBuilder: FormBuilder,
  ) {
  }


  ngOnInit() {
    this.initForm();
  }

  ngOnDestroy() {
  }

  initForm() {
    this.form = this.formBuilder.group({
      'email': ['',
      [
        Validators.required,
        Validators.email
      ]],
      'name': ['', []],
      'companyName': ['', []],
      'phone': ['', []],
    });
  }

  profileFormSubmit() {
    const formValue = Object.assign({}, this.form.value);
    const member: Member = {
      email: this.domainMember.member.email,
      name: formValue.name,
      phone: formValue.phone,
      companyName: formValue.companyName,
    };
    this.modify.emit(member);
  }

}
