import { Component, EventEmitter, Injector, ViewChild, Output, Input } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { ModalDirective } from 'ngx-bootstrap'; import { ControllerRouteDetailServiceProxy, RouteServiceProxy, UpdateRouteInput, VehicleServiceProxy, DriverListInput, VehicleInput, NameValueDto, UpdateDriverDto, SelectVehicleInput } from '@shared/service-proxies/service-proxies'; import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper'; import { finalize } from 'rxjs/operators'; import * as jquery from 'jquery'; import * as moment from 'moment'; import { formatDate } from '@angular/common'; // import * as DualListbox from 'dual-listbox'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; @Component({ templateUrl: './route-detail-modal.component.html', selector: 'routeDetailModal' }) export class RouteDetailModal extends AppComponentBase { // @ViewChild('routeDetail', { static: false }) modal: ModalDirective; // @Output() modalSave: EventEmitter = new EventEmitter(); @ViewChild('routeDetail', { static: true }) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); @Input('selectedDriverId') selectedDriverId: any; @Input('selectedVehicleId') selectedVehicleId: number; @ViewChild('driversList', { static: true }) driversList: CommonLookupModalComponent; @ViewChild('vehiclesList', { static: true }) vehiclesList: CommonLookupModalComponent; active = false; saving = false; driverName: string; vehicleName: string; loadDate: any; startTime: any; endTime: any; departure: any; completion: any; driverList: any[]; vehicleList: any[]; driverId: number; locationId: number; routeTemplateId: number; locked: boolean; isVisibleToDriver:boolean = false; isVisibleToDriverFeature:boolean = false; route: UpdateRouteInput = new UpdateRouteInput(); routeId: number; attribute = false; // selectedVehicleId: number; constructor( injector: Injector, private _controllerRouteDetails: ControllerRouteDetailServiceProxy, private _controllerRoute: RouteServiceProxy, private _controllerVehicle: VehicleServiceProxy, ) { super(injector); } ngOnInit() { this.isVisibleToDriverFeature = abp.features.isEnabled('App.RouteVisibleToDriver'); this.driversList.configure({ title: this.l('Assign Driver'), type:'assignDriver', 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; input.locationId = parseInt(localStorage.getItem('operatingLocationId')); return this._controllerRouteDetails.assignDriversList(input); } }); this.vehiclesList.configure({ title: this.l('Assign 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(routeID?: number): void { this.updateDetails(routeID); } updateDetails(id):void{ this.spinnerService.show(); this._controllerRouteDetails.getControllerRouteDetail(id) .subscribe(result => { if (result.vehicleId == null) { this.selectedVehicleId = null; } if (result.vehicle == undefined) { this.selectedDriverId = null; } this.routeId = id; this.routeTemplateId = result.routeTemplateId; this.locationId = result.locationId; this.loadDate = localStorage.getItem('loadDate'); this.startTime = result.departure ? moment(result.departure).utc().format("hh:mm a") : ''; this.endTime = result.completion ? moment(result.completion).utc().format("hh:mm a") : ''; this.completion = result.completion ? moment(result.completion).utc().format("YYYY-MM-DD hh:mm a") : ''; this.departure = result.departure ? moment(result.departure).utc().format("YYYY-MM-DD hh:mm a") : ''; this.locked = result.locked; this.isVisibleToDriver = result.isVisibleToDriver; if (result.vehicle) { this.selectedDriverId = result.vehicle.userId; this.selectedVehicleId = result.vehicle.id; this.vehicleName = result.vehicle.name; if (result.vehicle.user) { this.driverName = result.vehicle.user.name + " " + result.vehicle.user.surname; } else { this.driverName = ''; } } else { this.driverName = ''; this.vehicleName = ''; } $('.vehicle_name').text(this.vehicleName); this.active = true; this.spinnerService.hide(); this.modal.show(); }); } onShown(): void { let s = this; $('.kt-select2').select2(); // $('#selectedVehicleId').val(this.vehicleName); // $('#selectedDriverId').val(this.driverName); // $('#selectedVehicleId').on('select2:select', function (e) { // if (!s.locked) { // s.updateRoute(); // } else { // s.notify.info(s.l('Unlocked Loadlist First')); // } // }); // $('#selectedDriverId').on('select2:select', function (e) { // s.updateVehicle(); // }); } deleteDriver(): void { this.selectedDriverId = null; this.driverName = ''; // this.updateVehicle(); } deleteVehicle(): void { this.selectedVehicleId = null; this.selectedDriverId = null; this.driverName = ''; this.vehicleName = ''; // this.updateRoute(); } selectDriver(item: NameValueDto): void { let input = new VehicleInput(); this.selectedDriverId = parseInt(item.value); this.driverName = item.name; // this.updateVehicle(); } selectVehicle(item: NameValueDto): void { this.spinnerService.show(); let input = new VehicleInput(); this.selectedVehicleId = parseInt(item.value); this.vehicleName = item.name; var dto = new SelectVehicleInput(); dto.vehicleId = this.selectedVehicleId; this._controllerRouteDetails.selectVehicle(dto) .subscribe(result => { if (result.vehicleId != null) { this.selectedVehicleId = result.vehicleId; this.vehicleName = result.vehicleName; this.driverName = result.driverName; this.selectedDriverId = result.driverId; } else { this.selectedVehicleId = null; this.selectedDriverId = null; this.vehicleName = ''; this.driverName = ''; } this.spinnerService.hide(); }); } assignDriverModal(): void { this.driversList.show(); } assignVehicleModal(): void { this.vehiclesList.show(); } updateRoute() { this.spinnerService.show(); this.route.vehicleId = this.selectedVehicleId == 0 ? null : this.selectedVehicleId; this.route.routeTemplateId = this.routeTemplateId; this.route.locationId = this.locationId; this.route.completion = this.completion ? moment.utc(moment(this.completion).format('YYYY-MM-DD[T]HH:mm:ss')) : null; this.route.departure = this.departure ? moment.utc(moment(this.departure).format('YYYY-MM-DD[T]HH:mm:ss')) : null; this.route.locked = this.locked; this.route.id = this.routeId; this.route.driverId = this.selectedDriverId; this.route.isVisibleToDriver = this.isVisibleToDriver; var vehicleIdHolder = 0; if (this.selectedVehicleId == null) { this.attribute = true; } else { vehicleIdHolder = this.selectedVehicleId; this.attribute = false; } this._controllerRoute.updateControllerRoute(this.route) .pipe(finalize(() => this.saving = false)) .subscribe(result => { this.updateDetails(this.routeId); // this.modalSave.emit(null); this.spinnerService.hide(); $('#refreshBtn').trigger('click'); this.notify.info(this.l('SavedSuccessfully')); }, error => { this.spinnerService.hide(); }); } setDeparture() { if (!this.locked) { if (this.departure) { this.departure = ''; this.startTime = ''; } else { this.departure = moment(new Date()).format('YYYY-MM-DD hh:mm a'); this.startTime = moment(new Date()).format('hh:mm a'); } this.updateRoute(); } else { this.notify.info(this.l('Unlocked Loadlist First')); } } setCompletion() { if (!this.locked) { if (this.completion) { this.completion = ''; this.endTime = ''; } else { this.completion = moment(new Date()).format('YYYY-MM-DD hh:mm a'); this.endTime = moment(new Date()).format('hh:mm a'); } this.updateRoute(); } else { this.notify.info(this.l('Unlocked Loadlist First')); } } close(): void { this.active = false; this.modal.hide(); } }