import { Component, ViewChild, Injector, Output, EventEmitter, Input, ViewContainerRef} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { PostalCodesServiceProxy, CreateOrEditPostalCodeDto, OrderStatusTypeServiceProxy, OrderStatusServiceProxy, OrderStatusDto, CreateOrderStatusInput, ControllerOrderPackagesListServiceProxy, AddBatchOrderStatusInput, NameValueDto, GetAllRouteStopsInput, RouteServiceProxy, RouteStopsServiceProxy, TimingServiceProxy, OrderStatusDescriptionListDto } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import {CalendarModule} from 'primeng/calendar'; import * as moment from 'moment'; import { ControllerOrderPackageDetailComponent } from '../order-packages/order-packages-detail.component'; import { ViewOrderStatusComponent } from './view-order-status.component' import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; import { interval } from 'rxjs'; import { ProgressBarService } from '@app/shared/layout/progressbar/progressbar.service'; declare var $: any; @Component({ selector: 'addBatchOrderStatusModal', templateUrl: './add-batch-order-status-modal.component.html', //styleUrls: ['./add-batch-order-status-modal.component.less'] }) export class AddBatchOrderStatusModalComponent extends AppComponentBase { @ViewChild('routeStopList', {static: true}) routeStopList: CommonLookupModalComponent; @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @Input() orderId:number; @Output() modalSave: EventEmitter = new EventEmitter(); @Output() statusEvent: EventEmitter = new EventEmitter(); @Input() myFunction:Function active = false; saving = false; orderStatusTypes: any value:Date orderStatusNote:string orderStatusLongitude: any orderStatusLatitude : any orderStatusTime:any orderIds: []; DateObject = Date currentDate = moment().format("MM/DD/YYYY HH:mm"); orderArray:Array = []; //batchOrders:any statusBoolean=false; routeStop : string; lat: any; long: any; stopId: any; showSearchOnStart : boolean; _ukLocale: any; dateTime : Date; orderStatusDefinitions: OrderStatusDescriptionListDto[]; orderStatus: CreateOrderStatusInput = new CreateOrderStatusInput(); constructor( injector: Injector, private _orderStatusAppService : OrderStatusServiceProxy, private _orderStatusTypeAppService : OrderStatusTypeServiceProxy, private _orderPackageListAppService : ControllerOrderPackagesListServiceProxy, private _routeStopsAppService: RouteStopsServiceProxy, private viewContainerRef: ViewContainerRef, private _timingAppService: TimingServiceProxy, private _progressBarService: ProgressBarService ) { super(injector); } // orderStatusUpdate(get: boolean){ // this.statusEvent.emit(get) // } ngOnInit(){ this.routeStopModalInit(); } getParentComponent() { return this.viewContainerRef[ '_data' ].componentView.component.viewContainerRef[ '_view' ].component } routeStopModalInit(){ this.routeStopList.configure({ title: this.l('Select stop'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?:number) => { let input = new GetAllRouteStopsInput(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._routeStopsAppService.showPagedRouteStop(input); } }); } passedData($event){ this.orderIds = $event; } onShown(): void{ this.showSearchOnStart = true; $('.kt-select2').select2(); } show(): void { console.log(this.orderIds); if(this.orderIds === undefined || this.orderIds.length == 0) { this.message.error('No orders selected'); } else { this.getOrderStatusType(); this.active = true; this.modal.show(); this.orderStatus.note = null this.dateTime = new Date(); this._ukLocale = abp.features.isEnabled('App.DateFormatFeature.UKLocale'); this._timingAppService.convertToTimezone(moment(this.dateTime),localStorage.getItem('timeZone')) .pipe().subscribe(result => { if(this._ukLocale && result){ this.orderStatusTime = result.format("DD/MM/YYYY h:mm a"); } else{ this.orderStatusTime = result.format("MM/DD/YYYY h:mm a"); } }) this.orderStatusLongitude = null; this.orderStatusLatitude = null; } } getOrderStatusType(): void{ this._orderPackageListAppService.getOrderDescriptionList() .pipe(finalize(() => this.spinnerService.hide())).subscribe(result => { this.orderStatusDefinitions=result.items; }); } save(): void { this.saving = true; this._timingAppService.convertToTimezone(moment(this.dateTime), "UTC") .pipe() .subscribe(result => { this.orderStatus.time = result; this.orderStatus.orderStatusTypeId=Number((document.getElementById('OrderStatusType')).value); // this.orderStatus.note=String((document.getElementById('OrderStatusNote')).value); this.orderStatus.latitude = Number(this.lat).valueOf(); this.orderStatus.longitude = Number(this.long).valueOf(); this.orderArray = []; let batchOrders = new AddBatchOrderStatusInput({ orderId: this.orderIds, orderStatusTypeId: this.orderStatus.orderStatusTypeId, orderStatusDescriptionId: this.orderStatus.orderStatusTypeId, latitude: Number.isNaN(this.orderStatus.latitude) ? 0 : this.orderStatus.latitude, longitude: Number.isNaN(this.orderStatus.longitude) ? 0 : this.orderStatus.longitude, userId:undefined, noteId:undefined, geocodeId:undefined, note: this.orderStatus.note, time: this.orderStatus.time, bgColor : "", action : "", jobId : "" }); this.runBatch(batchOrders); }); } async runBatch(params: AddBatchOrderStatusInput) { let runBatch = await this._orderPackageListAppService.createBatchOrderStatus(params).toPromise(); this.close(); this.notify.info("Order status updating in-progress. Please wait..."); if (runBatch.isLast) { this.toggle(true); params.action = "TRACK"; params.jobId = runBatch.jobId; let observable = interval(1500).subscribe(res => { this._orderPackageListAppService.createBatchOrderStatus(params) .pipe(finalize(() => { this.saving = false;})) .subscribe((result) => { if (result.result == "DONE") { this.toggle(false); this.modalSave.emit(null); this.orderIds.length = 0; observable.unsubscribe(); this.notify.info(this.l('SavedSuccessfully')); } }); }); } } toggle(toggle: boolean) { this._progressBarService.toggleProgressBar(toggle); } close(): void { this.active = false; this.showSearchOnStart = true; this.routeStop = ''; this.lat = ''; this.long = ''; this.stopId = undefined; this.modal.hide(); } // selectRouteStop(item:NameValueDto):void{ // //console.log(item) // } selectRouteStop(item){ let that = this; if(this.showSearchOnStart) { this._routeStopsAppService.getRouteStopById(item.value) .subscribe(result => { if(result.routeStop != undefined){ that.routeStop = result.contactName + ' - ' + result.contactAddress; that.stopId = result.routeStop.id; that.lat = result.lat; that.long = result.long; that.showSearchOnStart = false; } }); } } getRouteStop(routeStop : any){ this.routeStopList.show(); this.routeStopList.filterText = routeStop; } clearRouteStop(): void{ this.showSearchOnStart = true; this.routeStop = ''; this.lat = ''; this.long = ''; this.stopId = undefined; } }