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


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

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


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

    active = false;
    saving = false;

    routeSchedule: CreateOrEditRouteScheduleDto = new CreateOrEditRouteScheduleDto();



    constructor(
        injector: Injector,
        private _routeSchedulesServiceProxy: RouteSchedulesServiceProxy
    ) {
        super(injector);
    }

    show(routeScheduleId?: number): void {

        if (!routeScheduleId) {
            this.routeSchedule = new CreateOrEditRouteScheduleDto();
            this.routeSchedule.id = routeScheduleId;

            this.active = true;
            this.modal.show();
        } else {
            this._routeSchedulesServiceProxy.getRouteScheduleForEdit(routeScheduleId).subscribe(result => {
                this.routeSchedule = result.routeSchedule;


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

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

			
            this._routeSchedulesServiceProxy.createOrEdit(this.routeSchedule)
             .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();
    }
}
