import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { VehicleLocationServiceProxy, UpdateVehicleLocationInput, VehicleLocationListDto, VehicleServiceProxy, VehicleListDto, LocationTableListDto } from '@shared/service-proxies/service-proxies'; import { LocationListDto, LocationServiceProxy } from '@shared/service-proxies/service-proxies'; import * as _ from 'lodash'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import * as jquery from 'jquery'; import { CommonLookupModalComponent } from '@app/shared/common/lookup/common-lookup-modal.component'; @Component({ selector: 'editVehicleLocationModal', templateUrl: './edit-vehicle-location-modal.component.html' }) export class EditVehicleLocationModalComponent extends AppComponentBase { @Output() modalSave: EventEmitter = new EventEmitter(); @ViewChild('modal', { static: false }) modal: ModalDirective; @ViewChild('nameInput', { static: false }) nameInput: ElementRef; @ViewChild('locationList', {static: true}) locationList: CommonLookupModalComponent; @ViewChild('vehicleList', {static: true}) vehicleList: CommonLookupModalComponent; vehicleLocation: UpdateVehicleLocationInput = new UpdateVehicleLocationInput(); // locations: LocationListDto[] = []; locations: LocationTableListDto[] = []; vehicles: VehicleListDto[] = []; locationInput: { id: number; name: string; }; textLocation textVehicle : string locationId: number; vehicleId: number; active: boolean = false; saving: boolean = false; constructor( injector: Injector, private _vehicleLocationService: VehicleLocationServiceProxy, private _locationService: LocationServiceProxy, private _vehicleService: VehicleServiceProxy ) { super(injector); } show(vehicleLocationId: any, vehicleId: any): void { this.active = true; this._vehicleLocationService.getVehicleLocation(vehicleLocationId, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result) => { this.vehicleLocation.id = result.items[0]['id']; this.vehicleId = result.items[0].vehicle.id; // this.locationId = result.items[0].location.id; this.initLocationField() this.initVehicleField() // this._locationService.getLocation(result.items[0]['locationId'], undefined, undefined, undefined, undefined, undefined, undefined).subscribe((resultLocations) => { // this.txtLocation = resultLocations.items[0]; // this.vehicleLocation.locationId = resultLocations.items[0]['id']; // this.vehicleLocation.vehicleId = vehicleId; // console.log(this.vehicleLocation); // }); this.modal.show(); }); } ngOnInit(): void { this.vehicleListModalInit(); this.locationListModalInit(); } 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._locationService.getPagedLocation( input.maxResultCount,input.skipCount,input.filter); } }); } vehicleListModalInit(){ this.vehicleList.configure({ title: this.l('Select vehicle'), 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._vehicleService.getPagedVehicleLookup( input.maxResultCount,input.skipCount,input.filter); } }); } onShown(): void { // this.nameInput.nativeElement.focus(); let maxcount = 1000; this.showAllLocation(maxcount); this.showAllVehicle(maxcount); // $('.kt-select2').select2(); } showAllLocation(maxcount): void { this.active = true; // this._locationService.getLocation(undefined, undefined, undefined, undefined, undefined, undefined, undefined, maxcount, undefined).subscribe((result) => { // this.locations = result.items; // }); this._locationService.getLocationTableOnly(undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result) => { this.locations = result; }); } showAllVehicle(maxcount): void { this.active = true; this._vehicleService.getVehiclePaged(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, maxcount, undefined).subscribe((result) => { this.vehicles = result.items; }); } save(): void { this.saving = true; //this.vehicleLocation.locationId = this.txtLocation.id; this.vehicleLocation.locationId =this.locationId; this.vehicleLocation.vehicleId =this.vehicleId; this._vehicleLocationService.updateVehicleLocation(this.vehicleLocation) .subscribe(() => { //console.log(this.vehicleLocation); this.notify.info(this.l('SavedSuccessfully')); console.log(this.vehicleLocation); this.saving = false; this.close(); this.modalSave.emit(this.vehicleLocation); }); } close(): void { this.modal.hide(); this.active = false; } 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; } getVehicle(filter : string){ this.vehicleList.show(); this.vehicleList.filterText = filter; } clearVehicle(){ this.vehicleId = null; this.textVehicle = ''; } selectVehicle(item){ var resultId = item.value var name = item.name this.vehicleId = resultId; this.textVehicle = name; } initLocationField(): void { this._locationService.getLocationTableOnly(this.locationId, undefined, undefined, undefined, undefined, undefined) .subscribe(result => { this.locations = result; this.textLocation = this.locationId != null? this.locations.filter(c => c.id == this.locationId)[0].name : ''; }); } initVehicleField(): void { let id = this.vehicleId == null? undefined : this.vehicleId; this._vehicleService.getVehiclePaged( id, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 1000, undefined ) .subscribe(result => { this.vehicles = result.items; this.textVehicle = this.vehicleId != null? this.vehicles.filter(c => c.id == this.vehicleId)[0].name : ''; }); } }