import { Component, OnInit } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { HappinessBEST } from '../../../entity/HappinessGroup';
import { BestService } from '../../../services/crudServices/best.service';

@Component({
  selector: 'ngx-best-editor-dlg',
  templateUrl: './best-editor-dlg.component.html',
  styleUrls: ['./best-editor-dlg.component.scss']
})
export class BestEditorDlgComponent implements OnInit {
  isAdding: boolean = false;
  processing: boolean = false;
  data: HappinessBEST;
  constructor(
    private bestService: BestService,
    private dlgRef: NbDialogRef<BestEditorDlgComponent>
  ) {

  }
  ngOnInit(): void {
    if (this.isAdding) {
      this.data = { id: 'new' } as HappinessBEST;
    }
  }

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

      this.processing = true;

      let func = this.bestService.update(this.data);//this.isAdding ? this.bestService.create(this.data) : 
      func.pipe(first()).subscribe(result => {
        this.processing = false;
        if (result) {
          this.dlgRef.close(true);
        }
      });
    }
  }
}
