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 } 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: 'updateOrderAddressModal', templateUrl: './update-order-address-modal.component.html' }) export class UpdateOrderDetailAddressModalComponent extends AppComponentBase implements OnInit{ @ViewChild('updateOrderAddressModal', { 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= ""; 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; addressLine2: string; constructor( injector: Injector, private _orderAppService: OrderServiceProxy, private _addressesAppService: AddressesServiceProxy ) { super(injector); } ngOnInit(){ } show(actionType:number, addressId:number, locationId:number,contactId: number){ this.actionType = actionType console.log(actionType) console.log(addressId) this.active= true; this.locationId = locationId this.addressId = addressId this.contactId = contactId this._addressesAppService.getAddressForEdit(addressId) .pipe() .subscribe(result =>{ let that = this; console.log(result) this.completeAddress = result.address.addressLine1 + ", " + result.city + ", " + (result.state == null ? "" : result.state + ", ") + result.postalCodeValue + ", " + result.country this.latitude = result.geocodeLatitude this.longitude = result.geocodeLongitude this.addressTypeName = result.addressTypeName this.addressLine1 = result.address.addressLine1 this.city = result.city this.country = result.country this.state = result.state this.postal_code = result.postalCodeValue this.addressLine2 = result.address.addressLine2 // console.log(this.compleAddressPickup) $('#address_pickup').val(that.completeAddress); }) this.modal.show(); } onShown(): void{ let that = this $('#address_detail').val(that.completeAddress); } save(): void { //console.log(this.input); this.saving = true; // this.createOnDemandOrderInput.formattedAddress_pickup = this.format_address_pickup; // this.createOnDemandOrderInput.addressLine1_pickup = this.addressLine1_pickup; // this.createOnDemandOrderInput.city_pickup = this.city_pickup; // this.createOnDemandOrderInput.country_pickup = this.country_pickup; // this.createOnDemandOrderInput.state_pickup= this.state_pickup; // this.createOnDemandOrderInput.postalcode_pickup = this.postal_code_pickup; // this.createOnDemandOrderInput.latitude_pickup = address.geometry.location.lat(); // this.createOnDemandOrderInput.longitude_pickup = address.geometry.location.lng(); // this.createOnDemandOrderInput.addressType_pickup = address.types[0]==null ? "" : address.types[0]; let orderAddressInput = new UpdateOrderAddressInput() orderAddressInput.addressId = this.addressId orderAddressInput.addressLine1 = this.addressLine1 orderAddressInput.city = this.city orderAddressInput.country = this.country orderAddressInput.state = this.state orderAddressInput.postalCodeValue = this.postal_code orderAddressInput.latitude = this.latitude orderAddressInput.longitude = this.longitude orderAddressInput.locationId = this.locationId orderAddressInput.formattedAddress = this.completeAddress orderAddressInput.addressTypeName = this.addressTypeName orderAddressInput.contactId = this.contactId orderAddressInput.addressLine2 = this.addressLine2; console.log(orderAddressInput) this._addressesAppService.updateOrderAddressContact(orderAddressInput) .pipe(finalize(() => { this.saving = false;})) .subscribe(result => { this.notify.info(this.l('SavedSuccessfully')); this.modalSave.emit(orderAddressInput); this.close(); // setTimeout(function(){ // location.reload() // }, 2000); }) // this._orderAppService.editOrderContactDetail(this.inputValue) // .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(); } 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]; // this.createOnDemandOrderInput.formattedAddress_pickup = this.format_address_pickup; // this.createOnDemandOrderInput.addressLine1_pickup = this.addressLine1_pickup; // this.createOnDemandOrderInput.city_pickup = this.city_pickup; // this.createOnDemandOrderInput.country_pickup = this.country_pickup; // this.createOnDemandOrderInput.state_pickup= this.state_pickup; // this.createOnDemandOrderInput.postalcode_pickup = this.postal_code_pickup; // this.createOnDemandOrderInput.latitude_pickup = address.geometry.location.lat(); // this.createOnDemandOrderInput.longitude_pickup = address.geometry.location.lng(); // this.createOnDemandOrderInput.addressType_pickup = 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",""); } } }