import { Injector, Component, ViewChild, AfterViewInit, HostBinding, ElementRef, OnInit, Directive, HostListener, Input, ViewContainerRef } from "@angular/core"; import { AppComponentBase } from "@shared/common/app-component-base"; import { appModuleAnimation } from "@shared/animations/routerTransition"; import { LocalStorageService } from "@shared/utils/local-storage.service"; import { Router, ActivatedRoute } from "@angular/router"; import * as moment from 'moment'; import { LocatorServiceProxy, ShippingPackageServiceProxy, RouteTypeList, GetVehicleDestinationPointsResultDto, LocationServiceProxy, GeocodeInAddressListDto } from "@shared/service-proxies/service-proxies"; import { MapsAPILoader } from "@agm/core"; import { stringify } from "querystring"; import { Options } from "select2"; import { List } from "lodash"; import { TabsetComponent } from "ngx-bootstrap"; import { AppConsts } from "@shared/AppConsts"; @Component({ selector: 'laboratoryMapFlyOutComponent', templateUrl: 'laboratory-map-flyout.component.html', animations: [appModuleAnimation()], styleUrls: ['laboratory-map-flyout.component.less'], }) export class LaboratoryMapFlyOutComponent extends AppComponentBase implements AfterViewInit, OnInit{ @HostBinding('id') id = 'kt_quick_sidebar'; @HostBinding('class') classes = 'kt-quick-panel'; @HostBinding('attr.mQuickSidebarOffcanvas') @HostBinding('style.overflow') styleOverflow: any = 'hidden'; @ViewChild('locatorTab', { static: false }) staticTabs: TabsetComponent; remoteServiceBaseUrl: string = AppConsts.remoteServiceBaseUrl; mQuickSidebarOffcanvas: any; interval: any; reload = false; exampleData: Array; routeData: Array = []; holder: Array = []; options: Options ; value: string[]; current: string; deliveryCheck: boolean = true; pickupCheck: boolean = true; filterDate: any; @Input() filters: { locationFilter: number; statusFilter: number; typeFilter: number; startDate: moment.Moment; endDate: moment.Moment; } = {}; @Input() route: Array = []; data: any; points: any; status: Array = []; allStatus: boolean; @Input() active: number; @Input() activeF: number; @Input() activeD: number; select: Array = []; driver: Array = []; locations: any; locationsMap: any; listpick: Array = []; routeType: List; @Input() routeSel: Array = []; @Input() routeIds: Array = []; routeTypeIds: Array = []; recenter: number; todaysDate: string; locationFilter: number; @Input() locationName:string; locationsFilter: any[]; constructor( private viewContainerRef: ViewContainerRef, injector:Injector, private el: ElementRef, private _locationServiceProxy: LocationServiceProxy, private activatedRoute: ActivatedRoute, private router:Router, private _locatorAppService: LocatorServiceProxy ){ super(injector) } _isOpen: boolean; _pinned = false; timeId: any; ngOnInit(){ this.timeId = localStorage.getItem('timeZoneId'); moment.tz.setDefault(this.timeId); this._locatorAppService.getRouteType().subscribe((result)=>{ this.routeType = result.items; this.routeTypeIds = []; result.items.forEach(item => { this.routeTypeIds.push(item.id); }); }); let that = this; this.getLocationsOnLoad().then(function(result){ that.spinnerService.hide(); }); if(localStorage.getItem('operatingLocationId')!=null){ this.filters.locationFilter = parseInt(localStorage.getItem('operatingLocationId')); } if(this.routeIds[0] != 0 && this.routeIds[0] != undefined){ this.filters.startDate = moment(localStorage.getItem('loadDate')); this.filterDate = localStorage.getItem('loadDate'); }else{ this.todaysDate = moment(Date.now()).format(); var loadDate = moment(this.todaysDate).format('YYYY-MM-DD'); this.filters.startDate = moment(moment(this.todaysDate).format('YYYY-MM-DD')); this.filterDate = moment(moment(this.todaysDate).format('YYYY-MM-DD')); this.route = []; this.routeSel = []; this.routeIds = []; } $("#locationSelectInputMap").val(that.filters.locationFilter); $('#locationSelectInputMap').on('change', function(){ $("#locationSelectInputMap").val($(this).val()); that.filters.locationFilter = parseInt($("#locationSelectInputMap").val().toString()); }); this.routeClick(); } getLocationsOnLoad() { let that = this; return new Promise(function(resolve, reject){ that._locationServiceProxy.getLocation(undefined, undefined, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { $('#locationSelectInputMap').select2(); that.locations = result.items; that.locationName = that.locations['0']['name']; }); }); } ngAfterViewInit(): void{ this.mQuickSidebarOffcanvas = new KTOffcanvas(this.el.nativeElement, { overlay: true, baseClass: 'kt-quick-panel', closeBy: 'getting_started_close', toggleBy: 'getting_started_toggle' }); this.mQuickSidebarOffcanvas.events.push({ name: 'afterHide', handler: () => { if (this._pinned) { this.mQuickSidebarOffcanvas.show(); } else { this.isOpen = false; } } }, { name: 'afterShow', handler: () => { this.isOpen = true; } }); if(this.routeIds[0] != 0 && this.routeIds[0] != undefined){ this._locatorAppService.getRouteType().subscribe((result)=>{ this.routeTypeIds = []; this.routeType = result.items; result.items.forEach(item => { this.routeTypeIds.push(item.id); }); this.activeD = 1; this.plotRouteMarker(); }); }else{ let tmp = []; let tmp1 = []; this._locatorAppService.getActiveRoutes(moment.tz(this.filterDate, this.timeId),this.getParentComponent().locationId).subscribe(result => { result.items.forEach(item => { tmp.push(item.routeId); tmp1.push(item.userId); }); this.activeD = 0; this.routeSel = tmp; this.routeIds = tmp; this.route = tmp1; this.plotRouteMarker(undefined); }); } } set pinned(newValue: boolean) { if (newValue === this._pinned) { return; } this._pinned = newValue; } get pinned(): boolean { return this._pinned; } reversePinned(): void { this.pinned = !this.pinned; } set isOpen(newValue: boolean) { if (newValue === this._isOpen) { return; } this._isOpen = newValue; } get isOpen(): boolean { return this._isOpen; } activeDriver(){ this.route = []; this.routeSel = []; this.routeIds = []; let tmp = []; this._locatorAppService.getActiveRoutes(moment.tz(this.filterDate, this.timeId),this.getParentComponent().locationId).subscribe(result => { result.items.forEach(item => { this.route.push(item.userId); tmp.push(item.routeId); this.routeIds.push(item.routeId); this.activeD = 0; }); this.routeSel = tmp; }); } getParentComponent() { return this.viewContainerRef[ '_data' ].componentView.component.viewContainerRef[ '_view' ].component } loadFlyOut(x?): void{ if(x){ //this.routeClick(); this.active = 1; this.activeD = 0; this.activeF = 1; // this._locatorAppService.getRouteType().subscribe((result)=>{ // this.routeType = result.items; // result.items.forEach(item => { // this.routeTypeIds.push(item.id); // }); // this.plotRouteMarker(); // }); } this.options = { width: '300', multiple: true, tags: true }; // this._locatorAppService.getLaboratoryDriverFilter(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,undefined,undefined,undefined) // .subscribe((result)=>{ // this.driver = []; // this.data = result.items; // }); } routeClick(){ this.active = 1; this._locatorAppService.getLaboratoryDriverFilter(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,undefined,undefined,this.routeIds) .subscribe((result)=>{ this.data = result.items; }); } selectAll() { this.route = this.data.map(x => x.userId); this.routeIds = this.data.map(x => x.routeId); this.routeSel = this.data.map(x => x.routeId); this.getParentComponent().cd.detectChanges(); } unselectAll() { this.getParentComponent().markers = []; this.getParentComponent().latestLog = []; this.getParentComponent().delmarkers = []; this.getParentComponent().driverP = []; this.route = []; this.routeSel = []; this.routeIds = []; this.driver = []; this.activeF = 1; this.activeD = 0; this.getParentComponent().cd.detectChanges(); } clearAll() { this.getParentComponent().markers = []; this.getParentComponent().latestLog = []; this.getParentComponent().delmarkers = []; this.getParentComponent().driverP = []; this.route = []; this.routeSel = []; this.routeIds = []; this.driver = []; this.activeF = 1; this.activeD = 0; this.getParentComponent().cd.detectChanges(); } addRoute(event){ this.routeIds.push(event.routeId); this.route.push(event.userId); //this.routeSel.push(event.routeId); this.getParentComponent().cd.detectChanges(); } removeRoute(event){ const index2 = this.routeSel.indexOf(event.value.routeId, 0); if (index2 > -1) { this.routeSel.splice(index2, 1); } const index = this.routeIds.indexOf(event.value.routeId, 0); if (index > -1) { this.routeIds.splice(index, 1); } const index1 = this.route.indexOf(event.value.userId, 0); if (index1 > -1) { this.route.splice(index1, 1); } this.getParentComponent().cd.detectChanges(); } plotRouteMarker(reload?){ this.getParentComponent().markers = []; this.getParentComponent().latestLog = []; this.getParentComponent().delmarkers = []; this.getParentComponent().driverP = []; //this.getParentComponent().mapReady(this.getParentComponent().map); if(reload != undefined){ this.getParentComponent().reload = false; this.reload = true; }else{ this.reload = false; } var bounds = new google.maps.LatLngBounds(); var color = ''; this.getParentComponent().loaded1 = false; this.getParentComponent().loaded2 = false; if(this.route.length != 0){ if(this.reload == false){ this.spinnerService.show(); } this._locatorAppService.getLaboratoryDriverDestinationPoints(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,this.route,this.routeTypeIds,this.routeIds) .subscribe((result)=>{ this.points = result.items; let geocodearray1 = Array(); let geocodearray = Array(); let sortlabel = []; this.points.forEach(item => { if(sortlabel[item.routeId] == undefined){ sortlabel[item.routeId] = 0; } if(this.pickupCheck){ sortlabel[item.routeId] ++; this.getParentComponent().markers.push( { lat: item.pickUpLatitude, lng: item.pickUpLongtitude, label: sortlabel[item.routeId]+"", draggable: false, // pin: pin, driverName: item.driverName, orderId: item.orderId, eta: moment(item.eta).toString(), color: item.color, companyName: item.pickUpCompanyName, geocode: item.pickUpGeoCodeId, vehicleColor: item.vehicleColor, statusCode: item.statusCode }); let that = item; bounds.extend(new google.maps.LatLng({lat: that.pickUpLatitude, lng: that.pickUpLongtitude})); if(geocodearray1.indexOf(item.pickUpGeoCodeId) !== -1) { }else{ this.listpick.push({ lat: item.pickUpLatitude, lng: item.pickUpLongtitude, label: item.orderId+"", draggable: false, // pin: pin, driverName: item.driverName, orderId: item.orderId, eta: moment(item.eta).toString(), color: item.color, companyName: item.pickUpCompanyName, geocode: item.pickUpGeoCodeId, vehicleColor: item.vehicleColor }) let that = item; bounds.extend(new google.maps.LatLng({lat: that.pickUpLatitude, lng: that.pickUpLongtitude})); } geocodearray1.push(item.pickUpGeoCodeId); } if(this.deliveryCheck){ if(geocodearray.indexOf(item.deliveryGeoCodeId) !== -1) { // console.log('connn'); }else{ this.getParentComponent().delmarkers.push( { lat: item.deliveryLatitude, lng: item.deliveryLongtitude, label: item.orderId+"", draggable: false, // pin: pin, driverName: item.driverName, orderId: item.orderId, eta: moment(item.eta).toString(), color: item.color, companyName: item.deliveryCompanyName, geocode: item.deliveryGeoCodeId }); let that = item; bounds.extend(new google.maps.LatLng({lat: that.pickUpLatitude, lng: that.pickUpLongtitude})); } geocodearray.push(item.deliveryGeoCodeId); } this.getParentComponent().loaded1 = true; }); this.locationsFilter = this.listpick.concat(this.getParentComponent().delmarkers); //this.locationsMap = geocodearray1; if(this.reload == false){ this.getParentComponent().map.fitBounds(bounds); } if(this.reload == false){ this.spinnerService.hide(); } }) } if(this.activeF == 1){ if(this.route.length != 0){ let user = []; let userId = 0; if(this.reload == false){ this.spinnerService.show(); } this._locatorAppService.getVehicleLatestLocation(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,this.route,undefined,this.routeIds) .subscribe((result)=>{ this.points = result.items; this.points.forEach(item => { if(item.userId in user){ user[item.userId] = user[item.userId] + 1; }else{ user[item.userId] = 1; } this.getParentComponent().latestLog.push( { lat: item.latitude, lng: item.longtitude, label: user[item.userId].toString(), draggable: false, driverName: item.driverName, orderId: item.orderId, eta: moment(item.timeStamp).toString(), color: item.color }); let that = item; }); if(this.reload == false){ // this.getParentComponent().map.fitBounds(bounds); } if(this.reload == false){ this.spinnerService.hide(); } this.getParentComponent().loaded2 = true; }) } } if(this.activeD == 1){ if(this.route.length != 0){ let user = []; let userId = 0; if(this.reload == false){ this.spinnerService.show(); } this.getParentComponent().showMarkers = false; this._locatorAppService.getVehicleLocationLog(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,this.route,undefined,this.routeIds) .subscribe((result)=>{ this.points = result.items; this.points.forEach(item => { if(item.userId in user){ user[item.userId] = user[item.userId] + 1; }else{ user[item.userId] = 1; } this.getParentComponent().driverP.push( { lat: item.latitude, lng: item.longtitude, label: user[item.userId].toString(), draggable: false, // pin: pin, driverName: item.driverName, orderId: item.orderId, eta: moment(item.timeStamp).toString(), color: item.color }); let that = item; }); this.getParentComponent().showMarkers = true; if(this.reload == false){ // this.getParentComponent().map.fitBounds(bounds); } if(this.reload == false){ this.spinnerService.hide(); } }) } } let that = this; this.getParentComponent().cd.detectChanges(); if(this.reload == false){ clearInterval(that.interval); that.interval = setInterval(function(){ that.plotRouteMarker(true); },50000); } } clickRouteType(event, routeTypeid){ if(event.target.checked){ this.routeTypeIds.push(routeTypeid); }else{ var index = this.routeTypeIds.indexOf(routeTypeid); if (index > -1) { this.routeTypeIds.splice(index, 1); } } this.plotRouteMarker(); } // plotFleetMarker(){ // if(this.activeF == 1){ // this.activeF = 1; // }else{ // this.activeF = 1; // } // } plotDriverMarker(){ if(this.activeD == 1){ this.activeD = 0; }else{ this.activeD = 1; } } reCenteMap(){ var result = this.locationsFilter.find(obj => { return obj.geocode === this.recenter }); this.getParentComponent().recenterMap(result.lat, result.lng); } ngOnDestroy() { let that = this; clearInterval(that.interval); } loadData(): void{ this.unselectAll(); this.filterDate = moment(this.filters.startDate).format('YYYY-MM-DD'); this._locatorAppService.getLaboratoryDriverFilter(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,undefined,undefined,undefined) .subscribe((result)=>{ this.driver = []; this.data = result.items; this.plotRouteMarker(); }); this.getParentComponent().getLocationsOnLoad(this.filters.locationFilter); this.staticTabs.tabs[1].active = true } }