import { Component, Injector, ViewChild, ViewEncapsulation, ElementRef, Directive, Input, EventEmitter, Output } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { UserLocationServiceProxy, UpdateUserLocationInput, UserLocationListDto, 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'; declare var KTWizard: any; declare var $: any; @Component({ selector: 'editUserLocationModal', encapsulation: ViewEncapsulation.None, templateUrl: './edit-user-location-modal.component.html' }) export class EditUserLocationModalComponent extends AppComponentBase { @Output() modalSave: EventEmitter = new EventEmitter(); @ViewChild(ModalDirective, {static: false}) public modal: ModalDirective; @ViewChild('nameInput', {static: false}) nameInput: ElementRef; @ViewChild("locationSelectInput", { static: false }) locationSelectInput: ElementRef; userLocation: UpdateUserLocationInput = new UpdateUserLocationInput(); locations: LocationTableListDto[] = []; filteredLocations: any; defaultLocation:any; locationInput: { id: number; name: string; }; txtLocation: any; active: boolean = false; saving: boolean = false; selectedLocation: any; userName: string; constructor( injector: Injector, private _userLocationService: UserLocationServiceProxy, private _locationService: LocationServiceProxy, ) { super(injector); } show(record): void { this.active = true; let maxcount = 1000; console.log(record) this.modal.show(); let values = []; record.locationIds.forEach(el => { values.push(el); }); this.defaultLocation = values; this.showAllLocation(1000); this.userLocation.userId = record.userId; this.userName = record.userName; } onShown(): void { // this.nameInput.nativeElement.focus(); $('.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.proximity.locationId = result.items[0]['id']; // //console.log(this.proximity.locationId); // }); this._locationService.getLocationTableOnly(undefined, undefined, undefined, undefined, undefined, undefined).subscribe((result) => { this.locations = result; let key, key2, arrAny = {}, arrHolder = []; for (key in result) { arrAny = { 'name': result[key].name, 'id': result[key].id, 'selected': false }; for (key2 in this.defaultLocation) { if (this.defaultLocation[key2] == result[key].id) { arrAny = { 'name': result[key].name, 'id': result[key].id, 'selected': true }; } } arrHolder.push(arrAny); } this.filteredLocations = arrHolder; }); } save(): void { this.saving = true; //this.userLocation.locationId = this.txtLocation.id; // this.userLocation.locationId = Number((document.getElementById('editUserLocationSelectInput')).value); this.userLocation.locationIds = $("#locationSelectInput2").val(); this._userLocationService.updateUserLocation(this.userLocation) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(this.userLocation); }); this.saving = false; } close(): void { this.modal.hide(); this.active = false; } }