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, LocationServiceProxy,LocationListDto, RouteTypeList } from "@shared/service-proxies/service-proxies"; // import { Select2OptionData } from 'ng2-select2'; import { MapsAPILoader } from "@agm/core"; import { stringify } from "querystring"; import { Options } from "select2"; import { isNull } from "util"; // import { CreatePostalCodesModalComponent } from "@app/sprintship/contact-management/postal-codes/create-or-edit-postalCode-modal.component"; import { TabsetComponent } from "ngx-bootstrap"; import { AppConsts } from "@shared/AppConsts"; import { List } from "lodash"; @Component({ selector: 'mapFlyOutComponent', templateUrl: 'map-flyout.component.html', animations: [appModuleAnimation()], styleUrls: ['map-flyout.component.less'], }) export class MapFlyOutComponent extends AppComponentBase implements AfterViewInit{ @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; driverFilter = true; driverFilterSel = 1; check: Array = []; filterSearch: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[]; data1: 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; track = 0; 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')); }else{ this.filters.locationFilter = this.locations[0].id; } if(this.routeIds[0] != 0 && this.routeIds[0] != undefined){ let todaysTime = moment(Date.now()).format("hh:mm:ss a"); this.filters.startDate = moment(moment(localStorage.getItem('loadDate') + ' ' + todaysTime).format('YYYY-MM-DD')); this.filterDate = moment(moment(localStorage.getItem('loadDate') + ' ' + todaysTime).format('YYYY-MM-DD')); }else{ this.todaysDate = moment(Date.now()).format(); var loadDate = this.todaysDate; 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.getLocationNames(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { $('#locationSelectInputMap').select2(); that.locations = result; }); }); } 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.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); }); } this.getParentComponent().getLocationsOnLoad(this.filters.locationFilter); } 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.active = 1; this.activeD = 0; this.activeF = 1; } this.options = { width: '300', multiple: true, tags: true }; } 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,undefined,this.driverFilter) .subscribe((result)=>{ this.data = this.data1 = result.items; this.filterSearch = ''; }); } 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(data,event){ if(event.target.checked){ // if(data.userId != 0 && data.userId != undefined){ this.route.push(data.userId); //} this.routeIds.push(data.routeId); }else{ const index = this.routeIds.indexOf(data.routeId, 0); if (index > -1) { this.routeIds.splice(index, 1); } if(data.userId == undefined || data.userId == 0){ const index1 = this.route.indexOf(0, 0); if (index1 > -1) { this.route.splice(index1, 1); } }else{ const index1 = this.route.indexOf(data.userId, 0); if (index1 > -1) { this.route.splice(index1, 1); } } } } applyFilter(event){ let filterValueLower = event.target.value; if(filterValueLower === '' || filterValueLower === undefined ) { this.data = this.data1; } else { this.data = this.data1.filter((d) => d.searchTerm.includes(filterValueLower)) } } 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(); } trackOnChange(value) { this.track = value; } plotRouteMarker(reload?){ this.activeD = 0; if(this.track == 1){ } if(this.track == 2){ this.activeD = 1; } this.getParentComponent().getOption(1); this.getParentComponent().markers = []; this.getParentComponent().latestLog = []; this.getParentComponent().delmarkers = []; this.getParentComponent().driverP = []; this.getParentComponent().idle = []; //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; this.route = this.route.map(function(val, i) { return val === null ? 0 : val; }); if(this.route.length != 0){ if(this.reload == false){ this.spinnerService.show(); } this._locatorAppService.getVehicleDestinationPoints(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,this.route,this.routeTypeIds,this.routeIds,undefined,this.driverFilter) .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.deliveryCheck){ sortlabel[item.routeId] ++; this.getParentComponent().delmarkers.push( { lat: item.deliveryLatitude, lng: item.deliveryLongtitude, label: sortlabel[item.routeId]+"", draggable: false, // pin: pin, driverName: item.driverName, orderId: item.orderId, eta: item.eta, color: item.color, companyName: item.pickUpCompanyName, geocode: item.pickUpGeoCodeId, vehicleColor: item.vehicleColor, statusCode: item.statusCode, statusTime: item.statusTime, statusDescription: item.statusDescription, deliveryTime: item.deliveryTime }); let that = item; bounds.extend(new google.maps.LatLng({lat: that.deliveryLatitude, lng: that.deliveryLongtitude})); // if(geocodearray1.indexOf(item.deliveryGeoCodeId) !== -1) { // }else{ // this.listpick.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.destinationAddress, // geocode: item.deliveryGeoCodeId, // vehicleColor: item.vehicleColor // }) // let that = item; // bounds.extend(new google.maps.LatLng({lat: that.deliveryLatitude, lng: that.deliveryLongtitude})); // } // geocodearray1.push(item.deliveryGeoCodeId); } if(this.pickupCheck){ if(geocodearray.indexOf(item.pickUpGeoCodeId) !== -1) { }else{ this.getParentComponent().markers.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.destinationAddress, geocode: item.pickUpGeoCodeId }); let that = item; bounds.extend(new google.maps.LatLng({lat: that.pickUpLatitude, lng: that.pickUpLongtitude})); } geocodearray.push(item.pickUpGeoCodeId); } this.getParentComponent().loaded1 = true; }); 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,undefined,this.driverFilter) .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,undefined,this.driverFilter) .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(); } }) } } if(this.track == 1){ if(this.route.length != 0){ let user = []; let userId = 0; if(this.reload == false){ this.spinnerService.show(); } this.getParentComponent().showMarkers = false; this._locatorAppService.getAllVehicleIdle(moment.tz(this.filterDate, this.timeId),moment.tz(this.filterDate, this.timeId),this.filters.locationFilter,this.route,undefined,this.routeIds,5,this.driverFilter) .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().idle.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(); // } 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); } driverFilterOnChange(value){ if(value == 1){ this.driverFilter = true; } if(value == 2){ this.driverFilter = undefined; } } 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,undefined,this.driverFilter) .subscribe((result)=>{ this.driver = []; result.items.forEach(value => { this.check[value.routeId] = false; }); this.data = this.data1 = result.items; this.filterSearch = ''; this.plotRouteMarker(); }); this.getParentComponent().getLocationsOnLoad(this.filters.locationFilter); this.staticTabs.tabs[1].disabled = false; this.staticTabs.tabs[1].active = true } checkAll(event){ this.data.forEach(value => { this.addRoute(value, event); if(event.target.checked){ this.check[value.routeId] = true; }else{ this.check[value.routeId] = false; } }); } }