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, UpdateTagTypeInput} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'updateTagTypeModal',
    templateUrl: './update-tag-types-modal.component.html'
})
export class UpdateTagTypeModalComponent extends AppComponentBase {

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

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

    active = false;
    saving = false;

    tagType: UpdateTagTypeInput = new UpdateTagTypeInput();

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

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

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

    save(): void {

        this.saving = true;
        this._tagTypeAppService.updateTagType(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();
    }
}
