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

@Component({
    selector: 'createTimeZoneModal',
    templateUrl: './create-timezones-modal.component.html'
})
export class CreateTimeZoneModalComponent extends AppComponentBase {

   @ViewChild('createModal', {static: false}) modal: ModalDirective;

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

    active: boolean = false;
    saving: boolean = false;
    timezone: CreateTimeZoneInput = new CreateTimeZoneInput();
    
    constructor(
        injector: Injector,
        private _timeZoneService: TimeZoneServiceProxy
        
    ) {
        super(injector);
    }

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

    onShown(): void {
        document.getElementById('valueInput').focus();
        this.timezone.value = null;
        
        
    }

    init(): void {
    
    }

    save(): void {
       
        this.saving = true;
        this._timeZoneService.createTimeZone(this.timezone)
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(this.timezone);
            });
        this.saving = false;
    }

    close(): void {
        this.active = false;
        this.modal.hide();

    }

    

}
