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 { CreatePostalCodeInput, PostalCodeServiceProxy } from "@shared/service-proxies/service-proxies";
import { finalize } from "rxjs/operators";
import { PostalCodesComponent } from "./postal-codes.component";


@Component({

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

})

export class CreatePostalCodesModalComponent extends AppComponentBase{

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

    saving=false
    active=false
    name: ""
    postalcode: CreatePostalCodeInput = new CreatePostalCodeInput();

constructor(

    injector:Injector,
    private _postalCodeService : PostalCodeServiceProxy

){

    super(injector)

}

    show(): void{

        this.active=true;

        // this.postalcode.name=null

        this.modal.show();

    }

    close(): void{

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

    }

    save(): void {
        
        this.saving=true
        this._postalCodeService.createPostalCode(this.postalcode)
            .pipe(finalize(() => this.saving=false))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(this.postalcode) 
            })
    }
}