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 } 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'; @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; active = false; saving = false; loadDate: any; startTime: any; endTime: any; departure: any; completion: any; driverList: any[]; vehicleList: any[]; driverId: number; locationId: number; routeTemplateId: number; locked: boolean; 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() { } show(routeID?: number): void { this._controllerRouteDetails.getDriverList().subscribe(result => { this.driverList = result; }); this._controllerRouteDetails.getVehicleList().subscribe(result => { this.vehicleList = result; }); this._controllerRouteDetails.getControllerRouteDetail(routeID) .subscribe(result => { if(result.vehicleId == null) { this.selectedVehicleId = null; } if(result.vehicle == undefined) { this.selectedDriverId = null; } this.routeId = routeID; this.routeTemplateId = result.routeTemplateId; this.locationId = result.location.id; this.loadDate = localStorage.getItem('loadDate'); this.startTime = result.departure ? moment(result.departure).format("hh:mm a") : ''; this.endTime = result.completion ? moment(result.completion).format("hh:mm a") : ''; this.completion = result.completion ? moment(result.completion).format("YYYY-MM-DD hh:mm a") : ''; this.departure = result.departure ? moment(result.departure).format("YYYY-MM-DD hh:mm a") : ''; this.locked = result.locked; if (result.vehicle) { this.selectedDriverId = result.vehicle.userId; this.selectedVehicleId = result.vehicle.id; } this.active = true; this.modal.show(); }); } onShown(): void { let s = this; $('.kt-select2').select2(); $('#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(); }); } updateRoute() { this.route.vehicleId = Number((document.getElementById('selectedVehicleId')).value); 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; var vehicleIdHolder = 0; if ((document.getElementById('selectedVehicleId')).value == 'null') { this.attribute = true; (document.getElementById('select2-selectedDriverId-container')).innerText = 'None'; } else { vehicleIdHolder = parseInt((document.getElementById('selectedVehicleId')).value); this.attribute = false; } this._controllerRoute.updateControllerRoute(this.route) .pipe(finalize(() => this.saving = false)) .subscribe(result => { let inputVehicleDriver: any; inputVehicleDriver = {'SearchType': 'Vehicle', 'FilterId': vehicleIdHolder, 'RouteId': this.routeId, 'PreviousDriverVehicleId': this.selectedVehicleId != null ? this.selectedVehicleId : undefined}; if (!this.locked) { this._controllerRouteDetails.updateVehicleDriver(inputVehicleDriver) .subscribe(result => { if (result[0].vehicleId == 0) { this.selectedVehicleId = result[0].vehicleId; this._controllerRouteDetails.getVehicleList().subscribe(result => { this.vehicleList = result; }); this.attribute = true; (document.getElementById('select2-selectedDriverId-container')).innerText = 'None'; } else { this.selectedVehicleId = result[0].vehicleId; (document.getElementById('select2-selectedDriverId-container')).innerText = result[0].driverName; this.attribute = false; } this.modalSave.emit(null); this.notify.info(this.l('SavedSuccessfully')); }); } else { this.notify.info(this.l('Unlocked Loadlist First')); } }); } updateVehicle() { var driverIdHolder = 0; if(Number((document.getElementById('selectedDriverId')).value) != NaN) { driverIdHolder = Number((document.getElementById('selectedDriverId')).value); } if(isNaN(driverIdHolder)) { driverIdHolder = 0; } let inputVehicleDriver: any; inputVehicleDriver = {'SearchType': 'Driver', 'FilterId': driverIdHolder, 'RouteId': this.routeId, 'PreviousDriverVehicleId': this.selectedVehicleId != null ? this.selectedVehicleId : undefined}; if (!this.locked) { this._controllerRouteDetails.updateVehicleDriver(inputVehicleDriver) .subscribe(result => { console.log(result) if (result[0].vehicleId == 0) { this._controllerRouteDetails.getVehicleList().subscribe(result => { this.vehicleList = result; }); this.selectedVehicleId = result[0].vehicleId; this.attribute = true; (document.getElementById('select2-selectedVehicleId-container')).innerText = 'None'; } else { this._controllerRouteDetails.getVehicleList().subscribe(result2 => { this.vehicleList = result2; this.selectedVehicleId = result[0].vehicleId }); this.selectedVehicleId = result[0].vehicleId; (document.getElementById('select2-selectedVehicleId-container')).innerText = result[0].vehicleName; this.attribute = false; } this.modalSave.emit(null); this.notify.info(this.l('SavedSuccessfully')); }); } else { this.notify.info(this.l('Unlocked Loadlist First')); } // this._controllerVehicle.getVehicleForEdit(Number((document.getElementById('selectedVehicleId')).value), driverIdHolder) // .subscribe(result => { // result.vehicle.userId = Number((document.getElementById('selectedDriverId')).value); // result.vehicle.routeId = this.routeId; // if (!this.locked) { // this._controllerVehicle.updateVehicle(result.vehicle) // .pipe(finalize(() => this.saving = false)) // .subscribe(() => { // this.modalSave.emit(null); // this.notify.info(this.l('SavedSuccessfully')); // }); // } else { // this.notify.info(this.l('Unlocked Loadlist First')); // } // }); } 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(); } }