import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { RouteTypesServiceProxy, CreateOrEditRouteTypeDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as moment from 'moment';
declare var $: any;

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

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


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

    active = false;
    saving = false;

    routeType: CreateOrEditRouteTypeDto = new CreateOrEditRouteTypeDto();



    constructor(
        injector: Injector,
        private _routeTypesServiceProxy: RouteTypesServiceProxy
    ) {
        super(injector);
    }

    show(routeTypeId?: number): void {

        if (!routeTypeId) {
            this.routeType = new CreateOrEditRouteTypeDto();
            this.routeType.id = routeTypeId;

            this.active = true;
            this.modal.show();
        } else {
            this._routeTypesServiceProxy.getRouteTypeForEdit(routeTypeId).subscribe(result => {
                this.routeType = result.routeType;


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

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

			
            this._routeTypesServiceProxy.createOrEdit(this.routeType)
             .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();
    }
}
