import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { AddressesServiceProxy, CreateOrEditAddressDto, UpdateAddressInput } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { AddressPostalCodeLookupTableModalComponent } from './address-postalCode-lookup-table-modal.component'; import { AddressGeocodeLookupTableModalComponent } from './address-geocode-lookup-table-modal.component'; import { AddressAddressTypeLookupTableModalComponent } from './address-addressType-lookup-table-modal.component'; import { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; @Component({ selector: 'createOrEditAddressModal', templateUrl: './create-or-edit-address-modal.component.html' }) export class CreateOrEditAddressModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild(AddressFormComponent, {static:false}) addressForm; @ViewChild('addressPostalCodeLookupTableModal', { static: true }) addressPostalCodeLookupTableModal: AddressPostalCodeLookupTableModalComponent; @ViewChild('addressGeocodeLookupTableModal', { static: true }) addressGeocodeLookupTableModal: AddressGeocodeLookupTableModalComponent; @ViewChild('addressAddressTypeLookupTableModal', { static: true }) addressAddressTypeLookupTableModal: AddressAddressTypeLookupTableModalComponent; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; address: CreateOrEditAddressDto = new CreateOrEditAddressDto(); address2: UpdateAddressInput = new UpdateAddressInput(); postalCodeValue = ''; geocodeLatitude = ''; addressTypeName : any; postalCodeId : number; postalCodeCountry : string; constructor( injector: Injector, private _addressesServiceProxy: AddressesServiceProxy ) { super(injector); } show(addressId?: number): void { if (!addressId) { this.address = new CreateOrEditAddressDto(); this.address.id = addressId; this.postalCodeValue = ''; this.geocodeLatitude = ''; this.addressTypeName = ''; this.active = true; this.modal.show(); } else { this._addressesServiceProxy.getAddressForEdit(addressId).subscribe(result => { this.address = result.address; this.postalCodeId = this.address.postalCodeId; this.postalCodeValue = result.postalCodeValue; this.geocodeLatitude = result.geocodeLatitude; this.addressTypeName = result.addressTypeName; this.active = true; this.modal.show(); }); } } save(): void { this.saving = true; this.address = this.addressForm.address this.address.postalCodeId = Number((document.getElementById('postalSelectInput')).value); this.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value); this._addressesServiceProxy.createOrEdit(this.address) .pipe(finalize(() => { this.saving = false;})) .subscribe(( ) => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } openSelectPostalCodeModal() { this.addressPostalCodeLookupTableModal.id = this.address.postalCodeId; this.addressPostalCodeLookupTableModal.displayName = this.postalCodeValue; this.addressPostalCodeLookupTableModal.show(); } openSelectGeocodeModal() { this.addressGeocodeLookupTableModal.id = this.address.geocodeId; this.addressGeocodeLookupTableModal.displayName = this.geocodeLatitude; this.addressGeocodeLookupTableModal.show(); } openSelectAddressTypeModal() { this.addressAddressTypeLookupTableModal.id = this.address.addressTypeId; this.addressAddressTypeLookupTableModal.displayName = this.addressTypeName; this.addressAddressTypeLookupTableModal.show(); } setPostalCodeIdNull() { this.address.postalCodeId = null; this.postalCodeValue = ''; } setGeocodeIdNull() { this.address.geocodeId = null; this.geocodeLatitude = ''; } setAddressTypeIdNull() { this.address.addressTypeId = null; this.addressTypeName = ''; } getNewPostalCodeId() { this.address.postalCodeId = this.addressPostalCodeLookupTableModal.id; this.postalCodeValue = this.addressPostalCodeLookupTableModal.displayName; } getNewGeocodeId() { this.address.geocodeId = this.addressGeocodeLookupTableModal.id; this.geocodeLatitude = this.addressGeocodeLookupTableModal.displayName; } getNewAddressTypeId() { this.address.addressTypeId = this.addressAddressTypeLookupTableModal.id; this.addressTypeName = this.addressAddressTypeLookupTableModal.displayName; } close(): void { this.active = false; this.modal.hide(); } }