import { Component, ViewChild, Injector, Output, Input, EventEmitter, OnInit, AfterViewInit } from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { RouteStopsServiceProxy, CreateOrEditRouteStopDto, CreatePostalCodeInput, LocationServiceProxy, LocationNameDto, GetContactSettingsItemDto, CommonSettingsServiceProxy } from '@shared/service-proxies/service-proxies'; import { AddressServiceProxy, CreateAddressInput } from '@shared/service-proxies/service-proxies'; import { EmailServiceProxy, CreateEmailInput, AddressTypeServiceProxy, PostalCodeServiceProxy, } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; import { RouteStopContactLookupTableModalComponent } from './routeStop-contact-lookup-table-modal.component'; import { AddressFormComponent } from '@app/shared/layout/form/address-form.component'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; @Component({ selector: 'createOrEditRouteStopModal', templateUrl: './create-or-edit-routeStop-modal.component.html', styleUrls: ['./create-or-edit-routeStop-modal.component.less'], providers: [CommonSettingsServiceProxy] }) export class CreateOrEditRouteStopModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild('locationList', { static: true }) locationList: CommonLookupModalComponent; @ViewChild('routeStopContactLookupTableModal', { static: true }) routeStopContactLookupTableModal: RouteStopContactLookupTableModalComponent; @ViewChild(AddressFormComponent, { static: false }) addressForm; @Output() modalSave: EventEmitter = new EventEmitter(); @Input('selectedCountry') selectedCountry: string; @Input('selectedCity') selectedCity: string; @Input('selectedState') selectedState: string; @Input('postalCodeId') selectedPostalCodeId: number; @Input('postalCodeValue') selectedPostalCodeValue: string; active = false; saving = false; routeStop: CreateOrEditRouteStopDto = new CreateOrEditRouteStopDto(); address: CreateAddressInput = new CreateAddressInput(); postalCodeInput: CreatePostalCodeInput = new CreatePostalCodeInput(); contactAddress = ''; filteredLocations: LocationNameDto[]; constructor( injector: Injector, private _addressService: AddressServiceProxy, private _emailService: EmailServiceProxy, private _postalCodeServiceProxy: PostalCodeServiceProxy, private _routeStopsServiceProxy: RouteStopsServiceProxy, private _locationService: LocationServiceProxy, private _commonSettings: CommonSettingsServiceProxy ) { super(injector); } filteredCountries: any; filteredCities: any; filteredStates: any; filteredPostal: any; filteredAddressType: any; activeTabIndex: number = 0; selectedPostalCode: string; selectedPostalCodeA: string; selectedAddressType: any; postalCodeValue: any; postalCodeId: number; filterChange = false; stateFeature: boolean; eta: any; //locationId: number = 0; latitude: number; longitude: number; textLocation: string; isDropOff: boolean = false; performBlindCount: boolean = false; contactSetting: any; edit: any; isDropOffInput: GetContactSettingsItemDto = new GetContactSettingsItemDto(); performBlindCountInput: GetContactSettingsItemDto = new GetContactSettingsItemDto(); stopTypeInput: GetContactSettingsItemDto = new GetContactSettingsItemDto(); contactSettingInput: Array = []; stopTypes: Array<{ value: string; name: string }> = new Array(); stopTypeValue: string; _isLab: boolean ngOnInit() { this.stateFeature = abp.features.isEnabled('App.StateFeature'); this.locationListModalInit(); this._isLab = abp.features.isEnabled('App.RouteDetails'); // this.getStopTypes(); } locationListModalInit() { this.locationList.configure({ title: this.l('Select location'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = { filter, maxResultCount, skipCount }; input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._locationService.getPagedLocation( input.maxResultCount, input.skipCount, input.filter); } }); } onShown() { this.getStopTypes(); var that = this setTimeout(function () { $('#StopType').val(that.stopTypeValue); $('.kt-select2').select2(); }, 1500); $('.kt-select2').select2(); //this.filterLocation(); } show(routeStopId?: number): void { let today = new Date(); $(".select2-container").css("width", "100%"); this.spinnerService.show(); if (!routeStopId) { this.routeStop = new CreateOrEditRouteStopDto(); this.routeStop.id = routeStopId; this.address.addressLine1 = ''; this.address.addressLine2 = ''; this.active = true; this.modal.show(); this.spinnerService.hide(); if (this._isLab) { this.stopTypeValue = ""; } //this.locationId = 0; this.edit = false; this.eta = null; if (this._isLab) { this.isDropOff = false; } this.performBlindCount = false; this.initLocationField(); } else { this._routeStopsServiceProxy.getRouteStopForEdit(routeStopId).subscribe(result => { this.routeStop = result.routeStop; this.address = this.routeStop.address; //this.locationId = result.routeStop.locationId; //console.log(result.routeStop); this.initLocationField(); this.postalCodeId = result.routeStop.address.postalCodeId; // var eta = result.deliveryEta == null ? null : moment.parseZone(result.deliveryEta).format('h:mm a'); // let mondayEta = eta == null ? null : today.toDateString() + ' ' + eta; // this.eta = mondayEta == null ? null : moment(mondayEta).toDate(); this.edit = true; this.contactSetting = result.routeStop.contactSetting; if (this._isLab) { for (var val of this.contactSetting) { // if(val.code == 'IsDropOff') { if (val.code == 'StopType') { this.stopTypeValue = val.value; // if(val.value == 'true') { // this.isDropOff = true; // } // else { // this.isDropOff = false; // } } else if (val.code == 'PerformBlindCount') { if (val.value == 'true') { this.performBlindCount = true; } else { this.performBlindCount = false; } } } }else{ for (var val of this.contactSetting) { if (val.code == 'StopType') { this.stopTypeValue = val.value; } } } this._postalCodeServiceProxy.getPostalCodeEdit(this.postalCodeId) .pipe() .subscribe(result => { this.addressForm.formattedAddress = this.address.formattedAddress; this.addressForm.city = result.city; this.addressForm.state = result.state; this.addressForm.value = result.value; this.addressForm.country = result.country; this.addressForm.latitude = this.routeStop.latitude; this.addressForm.longitude = this.routeStop.longitude; this.postalCodeValue = result.country; this.spinnerService.hide(); }); this.active = true; this.modal.show(); }); } } save(): void { this.saving = true; var etaMoment = moment(this.eta).tz(localStorage.getItem('timeZoneId')); // this.routeStop.deliveryETA = null; // this.routeStop.deliveryETAString = this.eta == null ? undefined : new Date(etaMoment.format()).toLocaleTimeString('en-US', { // hour12: true, // hour: "numeric", // minute: "numeric", // second: "numeric" // }) as any; //this.routeStop.locationId = Number((document.getElementById('textLocation')).value) == 0 ? null : Number((document.getElementById('textLocation')).value); //this.routeStop.locationId this.address = this.addressForm.address; this.address.addressTypeId = Number((document.getElementById('addressTypeSelectInput')).value) == 0 ? null : Number((document.getElementById('addressTypeSelectInput')).value); this.address.formattedAddress = this.addressForm.formattedAddress; //this.routeStop.latitude = Number((document.getElementById('Address_GeocodeLatitude')).value); //this.routeStop.longitude = Number((document.getElementById('Address_GeocodeLongitude')).value); this.routeStop.latitude = this.addressForm.latitude; this.routeStop.longitude = this.addressForm.longitude; this.routeStop.address = this.address; this.postalCodeInput.city = this.addressForm.city; if (!this.stateFeature) { this.postalCodeInput.state = ""; } else { this.postalCodeInput.state = this.addressForm.state; } this.postalCodeInput.country = this.addressForm.country; this.postalCodeInput.value = this.addressForm.value; if (this.eta == null || this.routeStop.locationId == null) { // if (this.eta == null) { // this.saving = false; // (document.getElementById('deliveryEtaDiv')).style.color = "#FF3300"; // }else{ // (document.getElementById('deliveryEtaDiv')).style.color = "#646C9A"; // } if (this.routeStop.locationId == null) { this.saving = false; (document.getElementById('locationDiv')).style.color = "#FF3300"; } else { (document.getElementById('locationDiv')).style.color = "#646C9A"; } } if (this._isLab) { if (String((document.getElementById('StopType')).value) != "") { this.isDropOffInput.code = 'StopType'; this.isDropOffInput.value = $('#StopType').val().toString(); // console.log("if") // console.log(String((document.getElementById('StopType')).value)) } // else{ // console.log("else") // } this.performBlindCountInput.code = 'PerformBlindCount'; this.performBlindCountInput.value = this.performBlindCount.toString(); if (this.edit == true) { if (String((document.getElementById('StopType')).value) != "") { this.routeStop.contactSetting[1] = this.isDropOffInput; } // this.routeStop.contactSetting.push(this.isDropOffInput); this.routeStop.contactSetting[0]= this.performBlindCountInput; } else { if (String((document.getElementById('StopType')).value) != "") { this.contactSettingInput[0] = this.isDropOffInput; this.contactSettingInput[1] = this.performBlindCountInput; } else { // this.contactSettingInput[0] = this.isDropOffInput; this.contactSettingInput[0] = this.performBlindCountInput; } this.routeStop.contactSetting = this.contactSettingInput; } }else { if (this.edit == true) { this.stopTypeInput.code = 'StopType'; this.stopTypeInput.value = $('#StopType').val().toString(); if (String((document.getElementById('StopType')).value) != "") { for(var data in this.routeStop.contactSetting){ if(this.routeStop.contactSetting[data].code=="StopType"){ this.routeStop.contactSetting[data].value = this.stopTypeInput.value; } } } else { this.stopTypeInput.code = 'StopType'; this.stopTypeInput.value = $('#StopType').val().toString(); if (String((document.getElementById('StopType')).value) != "") { this.routeStop.contactSetting.push(this.stopTypeInput); } } } } if (this.routeStop.locationId != null) { this._postalCodeServiceProxy.createPostalCode(this.postalCodeInput) .pipe() .subscribe(result => { this.routeStop.address.postalCodeId = result; //console.log(this.routeStop); this._routeStopsServiceProxy.createOrEdit(this.routeStop) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); }); } } getAllDropDown() { this.clearFields(); let maxcount = 1000; let that = this; } clearFields() { this.routeStop.email = null; this.routeStop.contactPerson = null; this.routeStop.routeStop = null; this.routeStop.latitude = null; this.routeStop.longitude = null; this.address.addressLine1 = null; this.address.addressLine2 = null; this.address.postalCodeId = null; this.selectedPostalCodeValue = null; this.selectedPostalCode = null; this.selectedPostalCodeId = null; this.selectedCountry = null; this.selectedState = null; this.selectedCity = null; this.routeStop.latitude = null; this.routeStop.longitude = null; this.eta = null; this.edit = null; this.contactSetting = null; //this.isDropOffInput = null; this.clearLocation(); } close(): void { this.active = false; this.clearFields() this.modal.hide(); } filterCountries(postalCode): void { this._postalCodeServiceProxy.getDistinctCountry( postalCode, undefined ).subscribe(result => { this.filteredCountries = result.items; }); } initLocationField(): void { this._locationService.getLocationNames(undefined, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { this.filteredLocations = result; this.textLocation = this.routeStop.locationId != null ? this.filteredLocations.filter(c => c.id == this.routeStop.locationId)[0].name : ''; }); } getLocation(filter: string) { this.locationList.show(); this.locationList.filterText = filter; } clearLocation() { this.routeStop.locationId = null; this.textLocation = ''; } selectLocation(item) { var resultId = item.value var name = item.name this.routeStop.locationId = resultId; this.textLocation = name; } getStopTypes() { this.stopTypes = []; this._commonSettings.getCodesForStopType() .subscribe(result => { result.items.forEach(element => { if (this._isLab) { this.stopTypes.push({ value: element, name: element.replace(/([A-Z])/g, ' $1').trim() }); } else { if (element != 'MeetPoint') { this.stopTypes.push({ value: element, name: element.replace(/([A-Z])/g, ' $1').trim() }); } } // this.stopTypes.push({ value: element, name: element.replace(/([A-Z])/g, ' $1').trim() }); }); console.log(this.stopTypes) }) } }