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


@Component({
    selector: 'createOrEditFleetModal',
    templateUrl: './create-or-edit-fleet-modal.component.html'
})
export class CreateOrEditFleetModalComponent extends AppComponentBase {

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


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

    active = false;
    saving = false;

    fleet: CreateOrEditFleetDto = new CreateOrEditFleetDto();



    constructor(
        injector: Injector,
        private _fleetsServiceProxy: FleetsServiceProxy
    ) {
        super(injector);
    }

    show(fleetId?: number): void {

        if (!fleetId) {
            this.fleet = new CreateOrEditFleetDto();
            this.fleet.id = fleetId;

            this.active = true;
            this.modal.show();
        } else {
            this._fleetsServiceProxy.getFleetForEdit(fleetId).subscribe(result => {
                this.fleet = result.fleet;


                this.active = true;
                this.modal.show();
            });
        }
    }

    save(): void {
            this.saving = true;

			
            this._fleetsServiceProxy.createOrEdit(this.fleet)
             .pipe(finalize(() => { this.saving = false;}))
             .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
             });
    }







    close(): void {

        this.active = false;
        this.modal.hide();
    }
}
