import { Component, EventEmitter, Injector, ViewChild, Output } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { UpdateAddressTypeInput, 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: './edit-address-type-modal.component.html',
    selector: 'updateAddressTypeModal',
})
export class UpdateAddressTypeModalComponent extends AppComponentBase {

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

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

     

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

    show(addressTypeId: number): void {
        this.active = true;
        
        this._addressTypeService.getAddressType(addressTypeId,undefined,undefined,undefined,undefined).subscribe(result =>{
            this.addressType.name = result.items[0].name;
            this.addressType.id = result.items[0].id;

            this.modal.show();
        });
        
    }

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

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

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