import { Component, OnInit } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';
import { DropDownOption } from '../../../entity/dropDownOption';
import { DomainMemberRelationship, PastoralDomain } from '../../../entity/PastoralDomain';
import { ArrayUtils } from '../../../utilities/array-utils';
import { first } from "rxjs/operators"
import { FamilyMember } from '../../../entity/Member';
import { DomainMemberShipService } from '../../../services/crudServices/domain-member-ship.service';

@Component({
  selector: 'ngx-assign-member-cell-group',
  templateUrl: './assign-member-cell-group.component.html',
  styleUrls: ['./assign-member-cell-group.component.scss']
})
export class AssignMemberCellGroupComponent implements OnInit {
  processing: boolean = false;
  members: FamilyMember[];
  data: DomainMemberRelationship[] = [];
  cellGroups: PastoralDomain[];
  cellGroupOptions: DropDownOption[] = [];
  groupId: string = null;
  constructor(
    private domainMemberShipService: DomainMemberShipService,
    private dlgRef: NbDialogRef<AssignMemberCellGroupComponent>
  ) {

  }
  ngOnInit(): void {

    this.cellGroupOptions = ArrayUtils.ToDropDownOptions(this.cellGroups, c => c.id, c => c.name);
  }

  close() {
    this.dlgRef.close();
  }
  update() {
    if (this.processing == false) {

      this.processing = true;
      this.data = this.members.map(m => new DomainMemberRelationship(this.groupId, m.id));
      this.domainMemberShipService.assignCellGroups(this.data).pipe(first()).subscribe(result => {
        this.processing = false;
        this.dlgRef.close(true);
      });
    }
  }
}

