import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, ElementRef, AfterViewInit, Renderer, Input } from '@angular/core'; import { ActivatedRoute,Route } from '@angular/router'; import { AppConsts } from '@shared/AppConsts'; import { CommonModule } from '@angular/common'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { RouteListDto, RouteServiceProxy, LocationListDto, LocationServiceProxy, ControllerRouteServiceProxy, GenerateRouteInputDto, AddressAddressTypeLookupTableDto, // ControllerRouteDetailServiceProxy } from '@shared/service-proxies/service-proxies'; import * as moment from 'moment'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from 'primeng/components/table/table'; import { HttpClient } from '@angular/common/http'; import { finalize } from 'rxjs/operators'; import * as _ from 'lodash'; import { TabsetComponent } from 'ngx-bootstrap'; import { TableRoutesComponents } from './table-routes-details.component'; import { TimerComponent } from '@app/sprintship/core-components/timer/timer.component'; import { ControllerRoutesService } from './croutes.service'; import { ControllerShowHideComponent } from '@app/sprintship/controller/core-components/controller-show-hide-modal.component.ts'; import { CreateRouteControllerModalComponent } from '@app/sprintship/controller/modals/create-routes-controller-modal.component'; import { DriverListTabComponent } from '../../controller/drivers/driver-list.component'; import { KPIBoxComponent } from '../kpi-box/kpi-box.component'; import { VehicleListTabComponent } from '../vehicles/vehicle-list.component'; import { AppSessionService } from '@shared/common/session/app-session.service'; import { GenerateRoutesTrackingComponent } from './generate-routes-tracking.component'; import { interval, Subscription } from 'rxjs'; import * as signalR from '@aspnet/signalr'; import { OrderListTabComponent } from '../orders/order-list.component'; import * as controller_table_settings from '@app/sprintship/controller/core-components/controller-table-settings.json'; import { OrderPackagesComponent } from '@app/sprintship/order-management/order-package-rad/orderPackages.component'; import { ControllerOrderPackagesComponent } from '../order-packages/order-packages-list.component'; import { TimerService } from '@app/sprintship/core-components/timer/timer.service'; import { CallBackModalComponent } from '@app/sprintship/controller/modals/call_back_modal.component'; import { CallInModalComponent } from '@app/sprintship/controller/modals/call_in_modal.component'; import { ControllerOrderIncidentsComponent } from '../order-incident/order-incident-list.component'; import { ControllerHeaderFlyoutComponent } from '@app/shared/layout/flyout/controller-header/controller-header-flyout.component'; import { RouteDetailModal } from '@app/sprintship/controller/route-detail/route-detail-modal.component'; // import { CallBackModalComponent } from '@app/sprintship/controller/modals/call_back_modal.component'; // import { CallInModalComponent } from '@app/sprintship/controller/modals/call_in_modal.component'; declare var $: any; declare var jquery: any; @Component({ selector: 'controller-main', templateUrl: 'croutes.component.html', encapsulation: ViewEncapsulation.None, styleUrls: ['./croutes.component.less', './dropdown.component.less'], animations: [appModuleAnimation()], providers: [ControllerRoutesService, TimerService] }) export class CRoutesComponent extends AppComponentBase implements OnInit { private hubConnection: signalR.HubConnection @ViewChild('routeDetailModal', { static: true }) routeDetailModal: RouteDetailModal; @ViewChild('controllerTab', { static: false }) staticTabs: TabsetComponent; @ViewChild('controllerTab', { read: ElementRef, static: true }) cRef: ElementRef; @ViewChild('controllerHeader', { read: ElementRef, static: true }) cHeader: ElementRef; @ViewChild('timerComponent', { static: false }) timerComponent: TimerComponent; @ViewChild(TableRoutesComponents, { static: false }) tRouteComponents: TableRoutesComponents; @ViewChild(DriverListTabComponent, { static: false }) driverTab: DriverListTabComponent; @ViewChild(VehicleListTabComponent, { static: false }) vehicleTab: VehicleListTabComponent; @ViewChild(OrderListTabComponent, { static: false }) tOrderComponents: OrderListTabComponent; @ViewChild(KPIBoxComponent, { static: false }) tKpiComponents: KPIBoxComponent; @ViewChild(ControllerOrderPackagesComponent, { static: false }) tOrderPackages: ControllerOrderPackagesComponent; @ViewChild(ControllerOrderIncidentsComponent, { static: false }) tOrderIncidents: ControllerOrderIncidentsComponent; @ViewChild('controllerHeaderFlyoutComponent', { static: false }) controllerHeaderFlyoutComponent: ControllerHeaderFlyoutComponent; // @ViewChild(KPIBoxComponent, {static: false}) tKpiBoxComponents: KPIBoxComponent; @ViewChild('dr', { static: false }) dr: ElementRef; @ViewChild('columnChooser', { static: false }) columnChooser: ControllerShowHideComponent; @ViewChild('generateRoutesTracking', { static: false }) generateRoutesTracking: GenerateRoutesTrackingComponent; @ViewChild('createRouteControllerModal', { static: false }) createRouteControllerModal: CreateRouteControllerModalComponent; // @ViewChild('callBackModal', {static: false}) callBackModal: CallBackModalComponent; // @ViewChild('callInModal', {static: false}) callInModal: CallInModalComponent; locations: LocationListDto[] = []; hide: boolean = true; hide_menu: boolean = true; key: number; tblMain: any; todaysDate: Date; event: LazyLoadEvent; generateEnable: boolean; time_interval: number; // orderStatusCode: string = undefined; filters: { locationFilter: number, statusFilter: number, typeFilter: number, startDate: Date, endDate: Date, orderStatusCode: string } = {}; search_string: string; txtFilter: string; statusLocatorComponent: boolean = false; locationName: string; orderPackageListComponent: boolean = false; orderIncidentListComponent: boolean = false; orderListComponent: boolean = false; heightTemp: string; inputs: { locationId: number; loadDate: Date; } = {}; subscription: Subscription; @Input('edition_name') edition: string; constructor( injector: Injector, private _controllerRouteService: ControllerRouteServiceProxy, private _locationServiceProxy: LocationServiceProxy, private _routeService: RouteServiceProxy, private _cRoutesService: ControllerRoutesService, private _timerService: TimerService, private renderer: Renderer, private elem: ElementRef ) { super(injector); this.todaysDate = new Date(Date.now()); // this.filters.startDate = this.todaysDate; // this.filters.endDate = this.todaysDate; this.generateEnable = false; } hideKpi(): void { if (this.hide == true) { this.hide = false; $('#kpiToggleIcon').removeClass('fa-eye-slash'); $('#kpiToggleIcon').addClass('fa-eye'); } else { this.hide = true; $('#kpiToggleIcon').addClass('fa-eye-slash'); $('#kpiToggleIcon').removeClass('fa-eye'); } } hideMenu(): void { if (this.hide_menu == true) { $('.dropdown-controller-menu').css('display', 'block'); this.hide_menu = false; } else { $('.dropdown-controller-menu').css('display', 'none'); this.hide_menu = true; } } showColumnChooser() { this.columnChooser.show(); } ngOnInit(): void { let that = this; this.time_interval = $('#refreshInterval').val(); this.todaysDate = new Date(Date.now()); // this.cHeader.nativeElement.querySelector('.controllerDatePicker1').value = moment(that.todaysDate).format('MM/DD/YYYY'); // this.cHeader.nativeElement.querySelector('.controllerDatePicker1').value = moment(that.todaysDate).format('MM/DD/YYYY'); this.getLocationsOnLoad().then(function (result) { $('#inputLocation').val(result); // that.filters.startDate = that.cHeader.nativeElement.querySelector('.controllerDatePicker').value; // that.filters.endDate = that.cHeader.nativeElement.querySelector('.controllerDatePicker').value; var locationId = result; if(localStorage.getItem('operatingLocationId') == null) { localStorage.setItem('operatingLocationId', $('#inputLocation').val()); }else{ $('#inputLocation').val(localStorage.getItem('operatingLocationId')); } that.tKpiComponents.getKpiCount(); that.tRouteComponents.populateTable(); that.inputs.loadDate = that.filters.startDate; that.inputs.locationId = that.filters.locationFilter; // localStorage.setItem('loadDate', moment(that.cHeader.nativeElement.querySelector('.controllerDatePicker1').value).format('YYYY-MM-DD')); if(localStorage.getItem('loadDate') == null) { localStorage.setItem('loadDate', moment(that.todaysDate).format('YYYY-MM-DD')); } else { that.filters.locationFilter = parseInt($('#inputLocation').val()); that.filters.startDate = moment(localStorage.getItem('loadDate')).toDate(); that.filters.endDate = moment(localStorage.getItem('loadDate')).toDate(); console.log('FILTERS: '); console.log(that.filters); $(".datePicker").text(moment(localStorage.getItem('loadDate')).format('dddd, MMMM DD, YYYY')); // this.filters.startDate = moment(localStorage.getItem('loadDate')).format('MM/DD/YYYY'); // this.filters.endDate = moment(localStorage.getItem('loadDate')).format('MM/DD/YYYY'); // console.log(this.filters); } that.filterRoutes(); // that.controllerHeaderFlyoutComponent.filterRoutes(); }); $('.selLocation').select2(); // $('#locationSelectInput').on('change', function(){ // $("#locationSelectInput").val($(this).val()); // that.filters.locationFilter = $("#locationSelectInput").val(); // }); this.cRef.nativeElement.querySelectorAll('#tblRoutes .primeng-datatable-container')[0].style.height = "0px"; var x = $('.kt-subheader').css('line-height').split('px'); // var heightTmp = (parseInt($('.kt-grid__item').height()) - parseInt($('.kt-subheader-controller').height())) - 370; var heightTmp = ((x[0]) * 20 + 100); this.heightTemp = heightTmp.toString() + 'px'; this.cRef.nativeElement.querySelectorAll('#tblRoutes .primeng-datatable-container')[0].style.height = this.heightTemp; // console.log('HEIGHT TEMP: '); // console.log(this.heightTemp); // console.log($('.kt-subheader').css('line-height')); // console.log('GRID HEIGHT'); // console.log($('.kt-grid__item').height()); this.backgroundProcessListener(); // console.log(this.edition); // console.log(controller_table_settings); this.filters.orderStatusCode = ""; $("#target").click(function () { console.log("laaaa"); }); } getLocationsOnLoad() { let that = this; return new Promise(function (resolve, reject) { that._locationServiceProxy.getLocation(undefined, undefined, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { that.locations = result.items; console.log(that.locations['0']['name']); // that.filters.locationFilter = result.items["0"].id; that.locationName = that.locations['0']['name']; $("#locationSelectInput").val(result.items["0"].id); that.txtFilter = null; null; null; resolve(result.items["0"].id); }); }); } loadRouteComponent() { this.statusLocatorComponent = false; } refreshPage(value: any) { if (value.action === 'CONTROLLER_REFRESH') { this.filterRoutes(); } } changeInterval(value: any) { this.time_interval = value; } filterRoutes() { let key; for (key in this.locations) { if (this.locations[key].id == this.filters.locationFilter) { $(".location").text(this.locations[key].name +" - "); } } $(".datePicker").text(moment(this.filters.endDate).format('dddd, MMMM DD, YYYY')); let that = this; let nowDate = moment(this.todaysDate).toDate(); let nowDateMoment = moment(nowDate); let duration = {}; duration['_data'] = { 'days': 0 }; // duration = moment.duration(nowDateMoment.diff(that.cHeader.nativeElement.querySelector('.controllerDatePicker1').value)); duration = moment.duration(nowDateMoment.diff(this.filters.endDate)); if (typeof duration !== "undefined") { if (duration['_data'].days > 0) { this.generateEnable = true; } else { this.generateEnable = false; } } // localStorage.setItem('loadDate', moment(that.cHeader.nativeElement.querySelector('.controllerDatePicker1').value).format('YYYY-MM-DD')); localStorage.setItem('loadDate', moment(this.filters.endDate).format('YYYY-MM-DD')); // this.filters.startDate = that.cHeader.nativeElement.querySelector('.controllerDatePicker').value; // this.filters.endDate = that.cHeader.nativeElement.querySelector('.controllerDatePicker').value; // this.inputs.loadDate = that.cHeader.nativeElement.querySelector('.controllerDatePicker').value; this.inputs.loadDate = this.filters.endDate; this._cRoutesService.setStatistics({ "result": "OK", "total_vehicles": "0", "total_packages": "0" }); // that.filters.locationFilter = $("#locationSelectInput").val(); this.inputs.locationId = that.filters.locationFilter; that.tRouteComponents.populateTable(); that.tKpiComponents.getKpiCount(); that.tOrderPackages.getOrderPackages(); that.tOrderIncidents.getOrderIncident(); this._timerService.default = { 'action': 'CONTROLLER_REFRESH' }; this._timerService.messageTimer({ 'action': 'CONTROLLER_REFRESH' }); that.vehicleTab.getVehicleList(); that.driverTab.getDriverList(); } loadDriverListComponent() { //this.driverListComponent = true; //console.log('driverTab'); } loadVehicleListComponent() { // this.tVehicleComponents.getVehicleList(); } loadOrderPackagesListComponent() { let that = this; this.orderPackageListComponent = true; // that.filters.locationFilter = $("#locationSelectInput").val(); // this.filters.startDate = $('#kt_datetimepicker_6').val(); // this.filters.endDate = $('#kt_datetimepicker_6').val(); // console.log(this.filters) this.tOrderPackages.getOrderPackages(); that.filters.orderStatusCode = undefined; } loadOrderIncidentListComponent() { let that = this; this.orderIncidentListComponent = true; // that.filters.locationFilter = $("#locationSelectInput").val(); // this.filters.startDate = $('#kt_datetimepicker_6').val(); // this.filters.endDate = $('#kt_datetimepicker_6').val(); // console.log(this.filters) this.tOrderIncidents.getOrderIncident(undefined); // console.log(that.filters.locationFilter) } loadOrderListComponent() { let that = this; this.orderListComponent = true; // that.filters.locationFilter = $("#locationSelectInput").val(); // this.filters.startDate = $('#kt_datetimepicker_6').val(); // this.filters.endDate = $('#kt_datetimepicker_6').val(); // this.getLocationsOnLoad(); this.tOrderComponents.getOrder(undefined); } getSelected() { //console.log('T COMPONENTS: '); let elements = this.elem.nativeElement.querySelectorAll('.checkboxItem'); //console.log(elements); } filterRoutesByParameters() { //console.log($('#txtFilter').val()); this.search_string = this.txtFilter; this.tRouteComponents.setFilter(this.txtFilter); this._cRoutesService.setStatistics({ "result": "OK", "total_vehicles": "0", "total_packages": "0" }); this.tRouteComponents.populateTable(); } backgroundProcessListener() { this.hubConnection = new signalR.HubConnectionBuilder() .withUrl(AppConsts.remoteServiceBaseUrl + '/signalr-background') .build(); this.hubConnection .start() .then(() => console.log('Connection started')) .catch(err => console.log('Error while starting connection: ' + err)); this.hubConnection.on('BackgroundMessage:' + abp.session.tenantId + ':generateRoutes', (message) => { if (message.match(/Route created/) || message.match(/Order created/)) { this.generateRoutesTracking.logMessage(message, true); } else { this.generateRoutesTracking.logMessage(message); } }); } showCreateRoutes() { this.createRouteControllerModal.show(); } // showCallBack () { // this.callBackModal.show(); // } // showCallIn () { // this.callInModal.show(); // } refresh(event: any) { this.filterRoutes(); } async generateRoute() { this.generateRoutesTracking.clearMessages(); // this.filters.startDate = this.cHeader.nativeElement.querySelector('.controllerDatePicker').value; // this.filters.endDate = this.cHeader.nativeElement.querySelector('.controllerDatePicker').value; // this.filters.locationFilter = $("#locationSelectInput").val(); let params = new GenerateRouteInputDto({ loadDateEnd: moment(this.filters.endDate), loadDateStart: moment(this.filters.startDate), locationId: this.filters.locationFilter, action: "RUN", jobId: "" }); let runResult = await this._controllerRouteService.generateRouteManual(params).toPromise(); if (runResult.success) { if (runResult.jobId) { this.generateRoutesTracking.modal.show(); params.jobId = runResult.jobId; params.action = "TRACK"; let observable = interval(2000).subscribe((val) => { this._controllerRouteService.generateRouteManual(params).subscribe(watcher => { if (watcher.result == "DONE") { this.generateRoutesTracking.complete(); observable.unsubscribe(); this.filterRoutes(); } }); }); } } } showFlyOut(): void { this.controllerHeaderFlyoutComponent.showFlyout(this.locations); } }