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


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

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


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

    active = false;
    saving = false;

    routeScheduleDetail: CreateOrEditRouteScheduleDetailDto = new CreateOrEditRouteScheduleDetailDto();

            effectivityDate: Date;


    constructor(
        injector: Injector,
        private _routeScheduleDetailsServiceProxy: RouteScheduleDetailsServiceProxy
    ) {
        super(injector);
    }

    show(routeScheduleDetailId?: number): void {
this.effectivityDate = null;

        if (!routeScheduleDetailId) {
            this.routeScheduleDetail = new CreateOrEditRouteScheduleDetailDto();
            this.routeScheduleDetail.id = routeScheduleDetailId;
            this.routeScheduleDetail.estimatedTimeOfArrival = moment().startOf('day');
            this.routeScheduleDetail.cutOff = moment().startOf('day');

            this.active = true;
            this.modal.show();
        } else {
            this._routeScheduleDetailsServiceProxy.getRouteScheduleDetailForEdit(routeScheduleDetailId).subscribe(result => {
                this.routeScheduleDetail = result.routeScheduleDetail;

                if (this.routeScheduleDetail.effectivityDate) {
					this.effectivityDate = this.routeScheduleDetail.effectivityDate.toDate();
                }

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

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

			
        if (this.effectivityDate) {
            if (!this.routeScheduleDetail.effectivityDate) {
                this.routeScheduleDetail.effectivityDate = moment(this.effectivityDate).startOf('day');
            }
            else {
                this.routeScheduleDetail.effectivityDate = moment(this.effectivityDate);
            }
        }
        else {
            this.routeScheduleDetail.effectivityDate = null;
        }
            this._routeScheduleDetailsServiceProxy.createOrEdit(this.routeScheduleDetail)
             .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();
    }
}
