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 } 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"; @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; 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; listpick: Array = []; routeType: List; @Input() routeSel: Array = []; @Input() routeIds: Array = []; routeTypeIds: Array = []; recenter: number; todaysDate: moment.Moment; 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('loadDate') == null) { // $("#kt_datetimepicker_6").val(moment(this.todaysDate).format('MM/DD/YYYY')); this.filters.startDate = moment(this.todaysDate); this.filterDate = $("#kt_datetimepicker_6").val(); } else { //$("#kt_datetimepicker_6").val(moment.tz(this.filterDate,this.timeId).format('MM/DD/YYYY')); this.filters.startDate = moment(localStorage.getItem('loadDate')); this.filterDate = localStorage.getItem('loadDate'); } if(localStorage.getItem('operatingLocationId')!=null){ this.filters.locationFilter = parseInt(localStorage.getItem('operatingLocationId')); } // setInterval(() => {this.routeClick();}, 3000); if(this.routeIds[0] != 0){ }else{ this.route = []; this.routeSel = []; this.routeIds = []; } $("#locationSelectInputMap").val(that.filters.locationFilter); // $("#locationSelectInputMap").val(that.filters.locationFilter); $('#locationSelectInputMap').on('change', function(){ $("#locationSelectInputMap").val($(this).val()); that.filters.locationFilter = parseInt($("#locationSelectInputMap").val().toString()); }); } 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._locatorAppService.getRouteType().subscribe((result)=>{ this.routeTypeIds = []; this.routeType = result.items; result.items.forEach(item => { this.routeTypeIds.push(item.id); }); this.plotRouteMarker(); }); }else{ this.route = []; this.routeSel = []; this.routeIds = []; } } 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; } 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 = 0; // 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); } unselectAll() { this.route = []; this.routeSel = []; this.routeIds = []; this.driver = []; this.getParentComponent().markers = []; this.getParentComponent().latestLog = []; this.getParentComponent().delmarkers = []; this.getParentComponent().driverP = []; this.activeF = 0; this.activeD = 0; } clearAll() { this.route = []; this.routeSel = []; this.routeIds = []; this.driver = []; this.getParentComponent().markers = []; this.getParentComponent().latestLog = []; this.getParentComponent().delmarkers = []; this.getParentComponent().driverP = []; this.activeF = 0; this.activeD = 0; } addRoute(event){ this.routeIds.push(event.routeId); this.route.push(event.userId); this.routeSel.push(event.routeId); } plotRouteMarker(reload?){ console.log(this.route); console.log(this.routeTypeIds); console.log(this.routeIds); if(reload != undefined){ this.getParentComponent().reload = false; this.reload = true; }else{ this.reload = false; } var bounds = new google.maps.LatLngBounds(); var color = ''; this.getParentComponent().markers = []; this.getParentComponent().latestLog = []; this.getParentComponent().delmarkers = []; this.getParentComponent().driverP = []; console.log(this.route); console.log(this.routeTypeIds); console.log(this.routeIds); 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(); this.points.forEach(item => { if(this.pickupCheck){ this.getParentComponent().markers.push( { lat: item.pickUpLatitude, lng: item.pickUpLongtitude, label: item.orderNumber+"", draggable: false, // pin: pin, driverName: item.driverName, orderId: item.orderId, eta: moment(item.eta).toString(), color: item.color, companyName: item.pickUpCompanyName, geocode: item.pickUpGeoCodeId }); 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 }) 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.locationsFilter = this.listpick.concat(this.getParentComponent().delmarkers); 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(); } }) } } if(this.activeD == 1){ if(this.route.length != 0){ let user = []; let userId = 0; if(this.reload == false){ this.spinnerService.show(); } 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; }); if(this.reload == false){ // this.getParentComponent().map.fitBounds(bounds); } if(this.reload == false){ this.spinnerService.hide(); } }) } } let that = this; clearInterval(that.interval); that.interval = setInterval(function(){ that.plotRouteMarker(true); },30000); } clickRouteType(event, routeTypeid){ // this.route = this.data.map(x => x.userId); 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(){ var color = ''; var bounds = new google.maps.LatLngBounds(); if(this.activeF == 1){ this.activeF = 0; this.getParentComponent().latestLog = []; }else{ this.activeF = 1; } // else{ // this.activeF = 1; // if(this.route.length != 0){ // let user = []; // let userId = 0; // if(this.reload == false){ // this.spinnerService.show(); // } // this._locatorAppService.getVehicleLatestLocation(moment(this.filters.startDate),moment(this.filters.startDate),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.eta).toString(), // color: item.color // }); // let that = item; // // bounds.extend(new google.maps.LatLng({lat: that.pickUpLatitude, lng: that.pickUpLongtitude})); // }); // if(this.reload == false){ // // this.getParentComponent().map.fitBounds(bounds); // } // if(this.reload == false){ // this.spinnerService.hide(); // } // }) // } // } } plotDriverMarker(){ var bounds = new google.maps.LatLngBounds(); var color = ''; if(this.activeD == 1){ this.activeD = 0; this.getParentComponent().driverP = []; }else{ this.activeD = 1; } // else{ // this.activeD = 1; // if(this.route.length != 0){ // let user = []; // let userId = 0; // if(this.reload == false){ // this.spinnerService.show(); // } // this._locatorAppService.getVehicleLocationLog(moment(this.filters.startDate),moment(this.filters.startDate),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.eta).toString(), // color: item.color // }); // let that = item; // // bounds.extend(new google.maps.LatLng({lat: that.pickUpLatitude, lng: that.pickUpLongtitude})); // }); // if(this.reload == false){ // // this.getParentComponent().map.fitBounds(bounds); // } // if(this.reload == false){ // this.spinnerService.hide(); // } // }) // } // } } reCenteMap(){ var result = this.locations.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 = $("#kt_datetimepicker_6").val(); 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 } }