import { Component, EventEmitter, Injector, ViewChild, Output } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { UpdateRouteInput, RouteServiceProxy, LocationServiceProxy,LocationNameDto,RouteDefinitionsServiceProxy,DriverListInput, ControllerRouteDetailServiceProxy, GetAllRouteDefinitionsInput,SelectVehicleInput, LocationListDto, VehicleServiceProxy, VehicleListDto, RouteTemplateServiceProxy, RouteTemplateListDto } from '@shared/service-proxies/service-proxies'; import { ModalDirective } from 'ngx-bootstrap'; import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper'; import { finalize } from 'rxjs/operators'; import * as jquery from 'jquery'; import * as moment from 'moment'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; @Component({ templateUrl: './edit-routes-modal.component.html', selector: 'editRouteModal', }) export class EditRouteModalComponent extends AppComponentBase { @ViewChild('editModal', {static: false}) modal: ModalDirective; @ViewChild('locationList', { static: true }) locationList: CommonLookupModalComponent; @ViewChild('routeCodeList', {static: true}) routeCodeList: CommonLookupModalComponent; @ViewChild('vehiclesList', { static: true }) vehiclesList: CommonLookupModalComponent; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; route: UpdateRouteInput = new UpdateRouteInput(); filteredLocations: LocationNameDto[]; filteredLocation: any; filteredVehicle: any; filteredRouteTemplate: any; locationId: any; routeTemplateId: any; vehicleId: any; loadDate: any; departure: Date; completion: Date; estimatedStartTime: Date; utcOffset: number; locked = false; textLocation: string; filteredRouteTemplates : string; routeCode: string; vehicleName: string; constructor( injector: Injector, private _locationAppService: LocationServiceProxy, private _vehicleAppService: VehicleServiceProxy, private _routeTemplateAppService: RouteTemplateServiceProxy, private _routeService: RouteServiceProxy, private _routeDefinitionsAppService: RouteDefinitionsServiceProxy, private _controllerRouteDetails: ControllerRouteDetailServiceProxy ) { super(injector); } ngOnInit(){ let maxcount = 1000; // this.filterLocation(maxcount); // this.filterVehicle(maxcount); // this.filterRouteTemplate(maxcount); this.locationListModalInit(); this.routeCodeModalInit(); this.vehicleModalInit(); } locationListModalInit() { this.locationList.configure({ title: this.l('Select location'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = { filter, maxResultCount, skipCount }; input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._locationAppService.getPagedLocation( input.maxResultCount, input.skipCount, input.filter); } }); } routeCodeModalInit(){ this.routeCodeList.configure({ title: this.l('Select Route Template'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?:number) => { let input = new GetAllRouteDefinitionsInput(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; return this._routeDefinitionsAppService.showPagedRouteDefinitionForSearch(input); } }); } vehicleModalInit(){ this.vehiclesList.configure({ title: this.l('Select Vehicle'), dataSource: (skipCount: number, maxResultCount: number, filter: string, tenantId?: number, locationId?: number) => { let input = new DriverListInput(); input.filter = filter; input.maxResultCount = maxResultCount; input.skipCount = skipCount; input.tenantId = tenantId; return this._controllerRouteDetails.assignVehiclesList(input); } }); } show(id): void { this.active = true; this.route.id = id; this._routeService.getRoute(id, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result)=> { this.loadDate = moment(result.items[0].loadDate).utcOffset(moment.parseZone(result.items[0].loadDate).utcOffset()).toDate(); this.completion = result.items[0]['completion'] ? result.items[0]['completion'].toDate() : null; //this.departure = result.items[0]['departure'] ? result.items[0]['departure'].toDate() : null; this.locked = result.items[0]['locked']; let completion = moment( result.items[0].completion).utc().format("YYYY-MM-DD hh:mm a"); let newCompletion = new Date(completion); this.completion = result.items[0]['departure'] ? newCompletion : null; let departure = moment( result.items[0].departure).utc().format("YYYY-MM-DD hh:mm a"); let newDeparture = new Date(departure); this.departure = result.items[0]['departure'] ? newDeparture : null; let _estimatedStartTime = moment( result.items[0].estimatedStartTime).utc().format("YYYY-MM-DD hh:mm a"); let newDateEST = new Date(_estimatedStartTime); this.estimatedStartTime = result.items[0].estimatedStartTime ? newDateEST : null; this.utcOffset = result.items[0].utcOffset; this.locationId = result.items[0]['locationId']; this.routeTemplateId = result.items[0]['routeTemplateId']; this.vehicleId = result.items[0]['vehicleId']; this.initLocationField(); this.initRouteTemplateField(result.items[0]['routeTemplateId']); this.initVehicleField(result.items[0]['vehicleId']); this.modal.show(); }); } initLocationField(): void { this._locationAppService.getLocationNames(undefined, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { this.filteredLocations = result; this.textLocation = this.locationId != null ? this.filteredLocations.filter(c => c.id == this.locationId )[0].name : ''; }); } getLocation(filter: string) { this.locationList.show(); this.locationList.filterText = filter; } clearLocation() { this.locationId = null; this.textLocation = ''; } selectLocation(item) { var resultId = item.value var name = item.name this.locationId = resultId; this.textLocation = name; } initRouteTemplateField(templateId): void { this._routeTemplateAppService.getRouteTemplateById(templateId) .subscribe(result => { this.routeTemplateId = result.routeTemplateId; this.filteredRouteTemplates = result.routeTemplateName + ' - ' + result.location; this.routeTemplateId = result.routeTemplateId; this.routeCode = result.routeTemplateName; }); } clearRouteTemplate() { this.filteredRouteTemplates = ''; this.routeTemplateId = null; this.routeCode = ''; } selectRouteTemplate(item) { var resultId = item.value var name = item.name this.routeTemplateId = resultId; this.routeCode = name; } getRoute(routeFilter){ this.routeCodeList.show(); this.routeCodeList.filterText = routeFilter; } initVehicleField(vehicleId): void { if(vehicleId != null){ var dto = new SelectVehicleInput(); dto.vehicleId = vehicleId; this._controllerRouteDetails.selectVehicle(dto) .subscribe(result => { this.vehicleId = result.vehicleId; this.vehicleName = result.vehicleName; }); } else{ this.vehicleId = null; this.vehicleName = ''; } } clearVehicle() { this.vehicleName = ''; this.vehicleId = null; } selectVehicle(item) { var resultId = item.value var name = item.name this.vehicleId = resultId; this.vehicleName = name; } getVehicle(routeFilter){ this.vehiclesList.show(); this.vehiclesList.filterText = routeFilter; } onShown(): void { //document.getElementById('cityInput').focus(); $('.kt-select2').select2(); } save(): void { var date = (document.getElementById('LoadDate')).value; this.saving = true; // this.route.locationId = Number((document.getElementById('location')).value); // this.route.vehicleId = Number((document.getElementById('vehicle')).value); // this.route.routeTemplateId = Number((document.getElementById('routeTemplate')).value); this.route.locationId = this.locationId; this.route.vehicleId = this.vehicleId; this.route.routeTemplateId = this.routeTemplateId; this.route.loadDate = this.loadDate; // this.route.completion = moment(moment(this.loadDate).format('YYYY-MM-DD') + ' ' + moment(this.completion).format('HH:mm:ss')); // this.route.departure = moment(moment(this.loadDate).format('YYYY-MM-DD') + ' ' + moment(this.departure).format('HH:mm:ss')); //this.route.estimatedStartTime = moment(moment(this.loadDate).format('YYYY-MM-DD') + ' ' + moment(this.estimatedStartTime).format('HH:mm:ss')); this.route.completion = this.completion ? moment.utc(moment(this.completion).local().format('YYYY-MM-DD hh:mm a')) : null; this.route.departure = this.departure ? moment.utc(moment(this.departure).local().format('YYYY-MM-DD hh:mm a')) : null; this.route.estimatedStartTime = this.estimatedStartTime ? moment.utc(moment(this.estimatedStartTime).local().format('YYYY-MM-DD hh:mm a')) : null; if (this.locationId == null) { this.saving = false; (document.getElementById('locationDiv')).style.color = "#FF3300"; } else { (document.getElementById('locationDiv')).style.color = "#646C9A"; } this.route.locked = this.locked; this._routeService.updateRoute(this.route) .pipe(finalize(() => this.saving = false)) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } close(): void { this.active = false; this.locationId = null; this.routeTemplateId = null; this.vehicleId = null; this.modal.hide(); } filterLocation(maxcount): void { // this._locationAppService.getLocation( // undefined, // undefined, // undefined, // undefined, // undefined, // undefined, // undefined, // maxcount, // undefined // ).subscribe(result => { // this.filteredLocation = result.items; // }); this._locationAppService.getLocationTableOnly( undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.filteredLocation = result; }); } filterRouteTemplate(maxcount): void { this._routeTemplateAppService.getRouteTemplate( undefined, undefined, undefined, undefined, undefined, undefined, undefined, maxcount, undefined ).subscribe(result => { this.filteredRouteTemplate = result.items; }); } filterVehicle(maxcount): void { this._vehicleAppService.getVehiclePaged( undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, maxcount, undefined ).subscribe(result => { this.filteredVehicle = result.items; }); } }