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 { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; import { ViewOrderComponent } from './view-order.component'; @Component({ selector: 'updateAddressModalComponent', templateUrl: './update-address-modal.component.html' }) export class UpdateOrderAddressModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild(AddressFormComponent, {static:false}) addressForm; @Output() modalSave: EventEmitter = new EventEmitter(); @Output() viewOrder = new EventEmitter(); active = false; saving = false; address: CreateOrEditAddressDto = new CreateOrEditAddressDto(); address2: UpdateAddressInput = new UpdateAddressInput(); postalCodeValue = ''; geocodeLatitude = ''; addressTypeName : any; postalCodeId : number; postalCodeCountry : string; router: any; 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.passParent(); this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } setPostalCodeIdNull() { this.address.postalCodeId = null; this.postalCodeValue = ''; } setGeocodeIdNull() { this.address.geocodeId = null; this.geocodeLatitude = ''; } setAddressTypeIdNull() { this.address.addressTypeId = null; this.addressTypeName = ''; } close(): void { this.active = false; this.modal.hide(); } passParent(){ this.viewOrder.emit(true); } }