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


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

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


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

    active = false;
    saving = false;

    timeZone: CreateOrEditTimeZoneDto = new CreateOrEditTimeZoneDto();



    constructor(
        injector: Injector,
        private _timeZonesServiceProxy: TimeZonesServiceProxy
    ) {
        super(injector);
    }

    show(timeZoneId?: number): void {

        if (!timeZoneId) {
            this.timeZone = new CreateOrEditTimeZoneDto();
            this.timeZone.id = timeZoneId;

            this.active = true;
            this.modal.show();
        } else {
            this._timeZonesServiceProxy.getTimeZoneForEdit(timeZoneId).subscribe(result => {
                this.timeZone = result.timeZone;


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

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

			
            this._timeZonesServiceProxy.createOrEdit(this.timeZone)
             .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();
    }
}
