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


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

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


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

    active = false;
    saving = false;

    tagType: CreateOrEditTagTypeDto = new CreateOrEditTagTypeDto();



    constructor(
        injector: Injector,
        private _tagTypesServiceProxy: TagTypesServiceProxy
    ) {
        super(injector);
    }

    show(tagTypeId?: number): void {

        if (!tagTypeId) {
            this.tagType = new CreateOrEditTagTypeDto();
            this.tagType.id = tagTypeId;

            this.active = true;
            this.modal.show();
        } else {
            this._tagTypesServiceProxy.getTagTypeForEdit(tagTypeId).subscribe(result => {
                this.tagType = result.tagType;


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

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

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