import { Component, OnInit } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { HappinessWeek } from '../../entity/HappinessGroup';
import { HappinessGroupService } from '../../services/crudServices/happiness-group.service';
@Component({
  selector: 'ngx-happiness-week-editor',
  templateUrl: './happiness-week-editor.component.html',
  styleUrls: ['./happiness-week-editor.component.scss']
})
export class HappinessWeekEditorComponent implements OnInit {
  isAdding: boolean = false;
  processing: boolean = false;
  data: HappinessWeek;
  allWeeks: HappinessWeek[];
  constructor(
    private happinessGroupService: HappinessGroupService,
    private dlgRef: NbDialogRef<HappinessWeekEditorComponent>
  ) {

  }
  ngOnInit(): void {
    if (this.isAdding) {
      this.data = {} as HappinessWeek;
    }
  }

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

      this.processing = true;

      this.happinessGroupService.createOrUpdateBestWeek(this.data).pipe(first()).subscribe(result => {
        this.processing = false;
        this.dlgRef.close(true);
      });
    }
  }
}
