import { Component, EventEmitter, Injector, ViewChild, Output } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateAddressTypeInput, AddressTypeServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './create-address-type-modal.component.html',
    selector: 'createAddressTypeModal',
})
export class CreateAddressTypeModalComponent extends AppComponentBase {

    @ViewChild('createModal', {static: false}) modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    addressType:CreateAddressTypeInput = new CreateAddressTypeInput();

     

    constructor(
        injector: Injector,
        private _addressTypeService: AddressTypeServiceProxy
    ) {
        super(injector);
    }

    show() {
        this.active = true;
        this.addressType.name = null;
        this.modal.show();
    }

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

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

        this._addressTypeService.createAddressType(this.addressType)
            .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();
    }
}
