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

@Component({
    selector: 'createTagTypeModal',
    templateUrl: './create-tag-types-modal.component.html'
})
export class CreateTagTypeModalComponent extends AppComponentBase {

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

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

    active = false;
    saving = false;

    tagType: CreateTagTypeInput = new CreateTagTypeInput();

    constructor(
        injector: Injector,
        private _tagTypeAppService: TagTypeServiceProxy
    ) {
        super(injector);
    }

    show(): void {
        this.tagType = new CreateTagTypeInput();
        // console.log(this.orderStatusType);
        this.active = true;
        this.modal.show();
    }

    onShown(): void {
        // document.getElementById('Code').focus();
    }

    save(): void {
        this.saving = true;
        this._tagTypeAppService.createTagType(this.tagType)
            .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();
    }
}
