import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { FleetServiceProxy, UpdateFleetInput } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

@Component({
  selector: 'editFleetModal',
  templateUrl: './edit-fleet-modal.component.html'
})
export class EditFleetModalComponent extends AppComponentBase {

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  @ViewChild('modal', {static: false})modal: ModalDirective;
 @ViewChild('nameInput', {static: false}) nameInput: ElementRef;

  fleet: UpdateFleetInput = new UpdateFleetInput();

  active: boolean = false;
  saving: boolean = false;

  constructor(
    injector: Injector,
    private _fleetService: FleetServiceProxy

  ) {
    super(injector);
  }

  show(fleetId): void {
    this.active = true;
    this._fleetService.getFleetForEdit(
        fleetId     
        ).subscribe((result)=> {
      this.fleet = result;
      this.modal.show();
    });

  }

  onShown(): void {
   // this.nameInput.nativeElement.focus();
  }

  save(): void {
    this.saving = true;
    this._fleetService.updateFleet(this.fleet)
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(this.fleet);
      });
    this.saving = false;
  }

  close(): void {
    this.modal.hide();
    this.active = false;
  }
}
