import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { UserLocationServiceProxy, CreateUserLocationInput, LocationTableListDto, NameValueDto, } from '@shared/service-proxies/service-proxies'; import * as _ from 'lodash'; import { ModalDirective } from 'ngx-bootstrap'; import { LocationListDto, LocationServiceProxy } from '@shared/service-proxies/service-proxies'; import { UserListDto, UserServiceProxy } from '@shared/service-proxies/service-proxies'; import * as jquery from 'jquery'; import { finalize } from 'rxjs/operators'; import { ICommonLookupModalOptions } from '@app/shared/common/lookup/common-lookup-modal.component'; import { AppConsts } from '@shared/AppConsts'; import { LazyLoadEvent } from 'primeng/api'; import { Paginator } from 'primeng/primeng'; import { Table } from 'primeng/table'; import { Observable } from 'rxjs'; declare var $: any; @Component({ selector: 'createUserLocationModal', templateUrl: './create-User-location-modal.component.html' }) export class CreateUserLocationModalComponent extends AppComponentBase { @ViewChild('createModal', { static: false }) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; userlocation: CreateUserLocationInput = new CreateUserLocationInput(); // locations: LocationListDto[] = []; locations: LocationTableListDto[] = []; users: UserListDto[] = []; locationInput: { id: number; name: string; }; userInput: { id: number; name: string; }; static defaultOptions: ICommonLookupModalOptions = { dataSource: undefined, canSelect: () => true, loadOnStartup: true, isFilterEnabled: true, pageSize: AppConsts.grid.defaultPageSize }; @Output() itemSelected: EventEmitter = new EventEmitter(); @ViewChild('dataTable', { static: true }) dataTable: Table; @ViewChild('paginator', { static: true }) paginator: Paginator; options: ICommonLookupModalOptions = _.merge({}); isShown = false; isInitialized = false; filterText = ''; tenantId?: number; filteredLocation: any; selectedLocation: any; constructor( injector: Injector, private _locationService: LocationServiceProxy, private _userService: UserServiceProxy, private _userLocationService: UserLocationServiceProxy ) { super(injector); } ngOnInit():void { } shown(): void { this.isShown = true; if (!this.options) { throw Error('Should call CreateUserLocationModalComponent.configure once before CreateUserLocationModalComponent.show!'); } this.showAllocations(); this.getRecordsIfNeeds(null); this.modal.show(); } onShown(): void{ $('.kt-select2').select2(); } configure(options: ICommonLookupModalOptions): void { console.log(options) this.options = _.merge({}, CreateUserLocationModalComponent.defaultOptions, { title: this.l('SelectAnItem')}, options); } getRecordsIfNeeds(event?: LazyLoadEvent): void { if (!this.isShown) { return; } if (!this.options.loadOnStartup && !this.isInitialized) { return; } this.getRecords(event); this.isInitialized = true; } getRecords(event?: LazyLoadEvent): void { const maxResultCount = this.primengTableHelper.getMaxResultCount(this.paginator, event); const skipCount = this.primengTableHelper.getSkipCount(this.paginator, event); if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } this.spinnerService.show(); this.options .dataSource(skipCount, maxResultCount, this.filterText, this.tenantId) .pipe(finalize(() => this.spinnerService.hide())) .subscribe(result => { this.primengTableHelper.totalRecordsCount = result.totalCount; this.primengTableHelper.records = result.items; this.spinnerService.hide(); }); } selectItem(item: NameValueDto) { this.userlocation.locationIds = $("#locationSelectInput1").val(); if(this.userlocation.locationIds.length==0){ $(".text-location").css("color", "red"); this.notify.error(this.l('Please select location')); }else{ const boolOrPromise = this.options.canSelect(item); if (!boolOrPromise) { return; } if (boolOrPromise === true) { this.itemSelected.emit(item); this.close(); return; } //assume as observable (boolOrPromise as Observable) .subscribe(result => { if (result) { this.itemSelected.emit(item) this.close(); } }); } } save(): void { this.selectedLocation = $("#locationSelectInput1").val(); this.userlocation.locationIds = this.selectedLocation; this.userlocation.userId = Number((document.getElementById('userSelectInput')).value); this._userLocationService.createUserLocation(this.userlocation) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } close(): void { this.active = false; this.modal.hide(); } showAllocations(): void { this._locationService.getLocationTableOnly( undefined, undefined, undefined, undefined, undefined, undefined ).subscribe(result => { this.filteredLocation = result; }); } refreshTable(): void { this.paginator.changePage(this.paginator.getPage()); } }