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


@Component({
    selector: 'editRouteTemplateTypeModal',
    templateUrl: './edit-route-template-types-modal.component.html'
})
export class EditRouteTemplateTypeModalComponent extends AppComponentBase {

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

  @ViewChild('modal', {static: false})modal: ModalDirective;
  
  routeTemplateType: UpdateRouteTemplateTypeInput = new UpdateRouteTemplateTypeInput();

  active: boolean = false;
  saving: boolean = false;

  constructor(
    injector: Injector,
    private _routeTemplateTypeService: RouteTemplateTypeServiceProxy
  ) {
    super(injector);
  }

  show(routeTemplateTypeId): void {
    this.active = true;
    this._routeTemplateTypeService.getRouteTemplateType(routeTemplateTypeId, undefined, undefined, undefined, undefined).subscribe((result)=> {
      //this.timezone.value = result.items[0]['value'];
      //this.timezone.id = result.items[0]['id'];
       this.routeTemplateType.id = result.items[0]['id'];
       this.routeTemplateType.name = result.items[0]['name'];
       console.log(result.items);
       this.modal.show();
    });

  }

  onShown(): void {
   // this.nameInput.nativeElement.focus();
  }

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

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