import { Component, Injector, ViewChild, Output, EventEmitter } from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { ModalDirective } from "ngx-bootstrap";
import { UpdatePostalCodeInput, PostalCodeServiceProxy } from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";

@Component({

    selector: 'editPostalCodesModal',
    templateUrl: './edit-postal-codes-modal.component.html',
    animations: [appModuleAnimation()] 

})

export class EditPostalCodesModalComponent extends AppComponentBase{

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

    postalCodes: UpdatePostalCodeInput = new UpdatePostalCodeInput();

    constructor(

        injector: Injector,
        private _postalCodeService : PostalCodeServiceProxy

    ){

        super(injector)

    }
    saving= false;
    active= false;

    show(postalCodeId): void{
        this.active = true;
        this._postalCodeService.getPostalCodeEdit(
        postalCodeId)
            .subscribe((result)=> {
            // this.postalCodes = result

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

    close(): void{

        this.modal.hide()
        this.active=false

    }

    save(): void{

        this.saving = true
        this._postalCodeService.updatePostalCode(this.postalCodes)
            .pipe(finalize(() => {this.saving = false}))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close()
                this.modalSave.emit(this.postalCodes)
            })
        this.saving=false  
    }

}