import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { VehicleServiceProxy, UpdateVehicleInput, UserServiceProxy, UserListDto, DriversServiceProxy, VehicleTypesServiceProxy, GetDriverListDto, GetUnassignedDrivers, GetDriverList } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as _ from 'lodash'; import { finalize } from 'rxjs/operators'; import * as moment from 'moment-timezone'; declare var $: any; @Component({ selector: 'editVehicleModal', templateUrl: './edit-vehicle-modal.component.html' }) export class EditVehicleModalComponent extends AppComponentBase { @Output() modalSave: EventEmitter = new EventEmitter(); @ViewChild('modal', { static: false }) modal: ModalDirective; @ViewChild('nameInput', { static: false }) nameInput: ElementRef; vehicle: UpdateVehicleInput = new UpdateVehicleInput(); users: GetDriverList[] = []; defaultUser: any; userInput: { id: number, name: string }; txtUser: any; active: boolean = false; saving: boolean = false; vehicleType: any; constructor( injector: Injector, private _vehicleService: VehicleServiceProxy, private _driverService: DriversServiceProxy, private _vehicleTypeService: VehicleTypesServiceProxy ) { super(injector); } show(vehicleId: any): void { let maxcount = 1000; this.active = true; this._vehicleService.getVehicleForEdit( vehicleId, 0 ).subscribe((result) => { result.vehicle.user === undefined ? this.defaultUser = undefined : this.defaultUser = result.vehicle.user.id; result.vehicle.description == null ? "" : result.vehicle.description; if(result.vehicle.acquisitionDate!=undefined){ result.vehicle.acquisitionDateString = moment.utc(result.vehicle.acquisitionDate).format('YYYY-MM-DD'); $("#acquisitionDate").val(result.vehicle.acquisitionDateString); } this.vehicle = result.vehicle; this.showAllDriver(maxcount); this.getVehicleType(); this.modal.show(); }); } onShown(): void { $('.kt-select2').select2(); } showAllDriver(maxcount): void { this.active = true; // this._driverService.getDrivers(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, maxcount, undefined).subscribe((result) => { // this.users = result.items; // }); this._driverService.getDriverList().subscribe((result) => { this.users = result; }); } save(): void { let timeId = localStorage.getItem('timeZoneId'); // this.vehicle.userId = Number((document.getElementById('driver')).value); this.vehicle.userId = Number((document.getElementById('driver')).value); this.vehicle.userId = this.vehicle.userId==0 ? null : this.vehicle.userId; this.vehicle.vehicleTypeId = Number((document.getElementById('vehicleType')).value); this.vehicle.vehicleTypeId = this.vehicle.vehicleTypeId==0 ? null : this.vehicle.vehicleTypeId; this.vehicle.acquisitionDate = moment.tz($("#acquisitionDate").val(), timeId); this.saving = true; this._vehicleService.updateVehicle(this.vehicle) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(this.vehicle); }); this.saving = false; } close(): void { this.modal.hide(); this.active = false; } getVehicleType() { this._vehicleTypeService.getAll(undefined, undefined, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { this.vehicleType = result.items; }); } }