import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { FleetServiceProxy, CreateFleetInput } from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'createFleetModal',
    templateUrl: './create-fleet-modal.component.html'
})
export class CreateFleetModalComponent extends AppComponentBase {

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

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

    fleet: CreateFleetInput = new CreateFleetInput();

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


    constructor(
        injector: Injector,
        private _fleetService: FleetServiceProxy
    ) {
        super(injector);
    }

    show(): void {
        this.active = true;
        this.fleet = new CreateFleetInput();
        this.modal.show();
    }

    onShown(): void {
    }

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

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