import { Component, OnInit } from '@angular/core';
import { NbDialogRef } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { LineMessagingAccount } from '../../../entity/LineMessagingAccount';
import { LineMessagingAccountService } from '../../../services/crudServices/line-messaging-account.service';

@Component({
  selector: 'ngx-line-messaging-account-editor',
  templateUrl: './line-messaging-account-editor.component.html',
  styleUrls: ['./line-messaging-account-editor.component.scss']
})
export class LineMessagingAccountEditorComponent implements OnInit {
  isAdding: boolean = false;
  processing: boolean = false;
  data: LineMessagingAccount;
  constructor(
    private lineMessagingAccountService: LineMessagingAccountService,
    private dlgRef: NbDialogRef<LineMessagingAccountEditorComponent>
  ) {

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

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

      this.processing = true;

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

