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


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

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


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

    active = false;
    saving = false;

    addressType: CreateOrEditAddressTypeDto = new CreateOrEditAddressTypeDto();



    constructor(
        injector: Injector,
        private _addressTypesServiceProxy: AddressTypesServiceProxy
    ) {
        super(injector);
    }

    show(addressTypeId?: number): void {

        if (!addressTypeId) {
            this.addressType = new CreateOrEditAddressTypeDto();
            this.addressType.id = addressTypeId;

            this.active = true;
            this.modal.show();
        } else {
            this._addressTypesServiceProxy.getAddressTypeForEdit(addressTypeId).subscribe(result => {
                this.addressType = result.addressType;


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

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

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