import {Component, Input, OnDestroy, OnInit} from '@angular/core';
import {Employee, EmployeeGroup, EmployeePermission} from "../../../shared/model/employee";
import {GroupService} from "../../services/group.service";
import {Observable, Subject} from "rxjs";
import {takeUntil} from "rxjs/operators";
import {AccountService} from "../../../accounts/services/account.service";

@Component({
  selector: 'cre-employees-list',
  templateUrl: './employees-list.component.html',
  styleUrls: ['./employees-list.component.sass']
})
export class EmployeesListComponent implements OnInit, OnDestroy {
  @Input() group: EmployeeGroup
  employees$: Observable<Employee[]> | null
  columns = ['id', 'firstName', 'lastName', 'edit']

  private _destroy$ = new Subject<boolean>()

  constructor(
    private accountService: AccountService,
    private groupService: GroupService
  ) {
  }

  ngOnInit(): void {
    this.employees$ = this.groupService.getEmployeesForGroup(this.group.id).pipe(takeUntil(this._destroy$))
  }

  ngOnDestroy(): void {
    this._destroy$.next(true)
    this._destroy$.complete()
  }

  get canEditEmployee(): boolean {
    return this.accountService.hasPermission(EmployeePermission.EDIT_EMPLOYEE)
  }
}
