import { Component, ViewChild, Injector, Output, EventEmitter, OnInit } from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { OrderServiceProxy, EditOrdeContactDetail, AddressesServiceProxy, AddressInput, CreateOrEditAddressDto, UpdateOrderAddressInput, AddressTypeServiceProxy, UpdateCustomerAddressInput, CustomerServiceProxy } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { Address } from 'ngx-google-places-autocomplete/objects/address'; import { ILatLng } from '@app/sprintship/fleet-management/driver-proximities/driver-proximities.directive'; import { GooglePlaceDirective } from 'ngx-google-places-autocomplete'; declare var $: any; @Component({ selector: 'updateCustomerAddressModal', templateUrl: './edit-customer-address.component.html' }) export class UpdateCustomerAddressModalComponent extends AppComponentBase implements OnInit { @ViewChild('modal', { static: true }) modal: ModalDirective; @ViewChild('placesRef', { static: true }) placesRef: GooglePlaceDirective; @ViewChild('placesRef1', { static: true }) placesRef1: GooglePlaceDirective; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; actionType: number; input: any; inputValue: EditOrdeContactDetail = new EditOrdeContactDetail(); phone: any; email: any; address: AddressInput = new AddressInput(); options = { componentRestriction: { // types: ['geocode'], // country: 'UA' }, // types: ['cities'], } completeAddress: string = ""; format_address = "" addressLine1 = ""; addressLine2: string; street_number = ""; route = ""; neighborhood = ""; sublocality = ""; place_id_ = "" placeExist = true; origin: ILatLng = { latitude: 38.889931, longitude: -77.009003 }; // New York City, NY, USA postal_code: any country: any state: any city: any place_id: any longitude: any latitude: any locationId: number addressTypeName: string = ""; formattedAddress: string = ""; addressId: number; contactId: number; addressTypeId: any; filteredAddressType: any; constructor( injector: Injector, private _orderAppService: OrderServiceProxy, private _addressesAppService: AddressesServiceProxy, private _addressTypeService: AddressTypeServiceProxy, private _customerRepository: CustomerServiceProxy ) { super(injector); } ngOnInit() { } show(addressId: number, contactId: any) { this.active = true; this.addressId = addressId; this.contactId = contactId this.spinnerService.show(); this._customerRepository.getCustomerAddressForEdit(contactId) .pipe() .subscribe(result => { let that = this; this.completeAddress = result.address.address.addressLine1 + ", " + result.address.city + ", " + (result.address.state == null ? "" : result.address.state + ", ") + result.address.postalCodeValue + ", " + result.address.country this.latitude = result.address.geocodeLatitude this.longitude = result.address.geocodeLongitude this.addressTypeName = result.address.addressTypeName; this.addressTypeId = result.address.address.addressTypeId; this.addressLine1 = result.address.address.addressLine1; this.addressLine2 = result.address.address.addressLine2; this.city = result.address.city this.country = result.address.country this.state = result.address.state this.postal_code = result.address.postalCodeValue; $('#address_pickup').val(that.completeAddress); this.spinnerService.hide(); }) this.modal.show(); } onShown(): void { let that = this $('#address_detail').val(that.completeAddress); $('.kt-select2').select2(); } save(): void { this.saving = true; // this.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value) == 0 // ? null : Number((document.getElementById('addressTypeSelectInput')).value); let customerAddressInput = new UpdateCustomerAddressInput() customerAddressInput.addressId = this.addressId customerAddressInput.addressLine1 = this.addressLine1; customerAddressInput.addressLine2 = this.addressLine2; customerAddressInput.city = this.city customerAddressInput.country = this.country customerAddressInput.state = this.state customerAddressInput.postalCodeValue = this.postal_code customerAddressInput.latitude = this.latitude customerAddressInput.longitude = this.longitude customerAddressInput.formattedAddress = this.completeAddress customerAddressInput.addressTypeName = this.addressTypeName customerAddressInput.contactId = this.contactId; // customerAddressInput.addressTypeId = this.addressTypeId; this._customerRepository.updateCustomerAddress(customerAddressInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(result => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } close(): void { this.active = false; this.modal.hide(); } public pickUpAddressChange(address: Address) { this.placeExist = true; $('#nextStep').removeAttr("disabled"); this.format_address = address.formatted_address; this.addressLine1 = ""; this.street_number = ""; this.route = ""; this.neighborhood = ""; this.sublocality = ""; this.place_id = address.place_id; let x: ILatLng = { longitude: address.geometry.location.lng(), latitude: address.geometry.location.lat(), } this.origin = x; for (var data in address.address_components) { for (var data2 in address.address_components[data].types) { if (address.address_components[data].types[data2] == "postal_code") { this.postal_code = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "country") { this.country = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "administrative_area_level_1") { this.state = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "locality" || address.address_components[data].types[data2] == "postal_town" || address.address_components[data].types[data2] == "administrative_area_level_3") { this.city = address.address_components[data].short_name; } if (address.address_components[data].types[data2] == "route") { this.route = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "street_number") { this.street_number = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "neighborhood") { this.neighborhood = address.address_components[data].long_name; } if (address.address_components[data].types[data2] == "sublocality") { this.sublocality = address.address_components[data].long_name; } } } this.street_number = this.street_number == undefined || this.route == "" || this.route == " " ? "" : this.street_number + " "; this.route = this.route == undefined || this.route == "" || this.route == " " ? "" : "" + this.route + ", "; this.neighborhood = this.neighborhood == undefined || this.neighborhood == "" || this.neighborhood == " " ? "" : "" + this.neighborhood + ", "; this.sublocality = this.sublocality == undefined || this.sublocality == "" || this.sublocality == " " ? "" : "" + this.sublocality + ", "; this.addressLine1 = this.street_number + this.route + this.sublocality + this.neighborhood; this.longitude = address.geometry.location.lng(); this.latitude = address.geometry.location.lat(); this.addressTypeName = address.types[0] == null ? "" : address.types[0]; } onSubmitPickup(): void { try { if (this.placesRef.place.address_components == undefined || this.placesRef.place.address_components == null) { this.placeExist = false; } else { this.placeExist = true; } } catch (e) { this.placeExist = false; } if (!this.placeExist) { $('#nextStep').attr("disabled", ""); } } }