import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { FindOrganizationUnitUsersInput, NameValueDto, OrganizationUnitServiceProxy, UsersToOrganizationUnitInput,ClinicsServiceProxy, ContactGroupServiceProxy, AddContactForGroupInputDto, AddContactToGroupInput, GetClinicForViewDto } from '@shared/service-proxies/service-proxies'; import * as _ from 'lodash'; import { ModalDirective } from 'ngx-bootstrap'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; import { Paginator } from 'primeng/components/paginator/paginator'; import { Table } from 'primeng/components/table/table'; // import { IUsersWithOrganizationUnit } from './users-with-organization-unit'; import { finalize } from 'rxjs/operators'; import { ICustomerWithGroup } from './customer-with-group.component'; @Component({ selector: 'addCustomerModal', templateUrl: './add-customer.component.html' }) export class AddCustomerModalComponent extends AppComponentBase { groupId: number; @Output() membersAdded: EventEmitter = new EventEmitter(); @ViewChild('modal', {static: true}) modal: ModalDirective; @ViewChild('dataTable', {static: true}) dataTable: Table; @ViewChild('paginator', {static: true}) paginator: Paginator; isShown = false; filterText = ''; tenantId?: number; saving = false; selectedMembers: NameValueDto[]; contactGroupId: number; constructor( injector: Injector, private _organizationUnitService: OrganizationUnitServiceProxy, private _clinicsService:ClinicsServiceProxy, private _contactGroupService: ContactGroupServiceProxy ) { super(injector); } show(): void { this.modal.show(); } refreshTable(): void { this.paginator.changePage(this.paginator.getPage()); } close(): void { this.filterText = undefined; this.modal.hide(); } shown(): void { this.isShown = true; this.getRecordsIfNeeds(null); } getRecordsIfNeeds(event: LazyLoadEvent): void { if (!this.isShown) { return; } this.getRecords(event); } getRecords(event?: LazyLoadEvent): void { if (this.primengTableHelper.shouldResetPaging(event)) { this.paginator.changePage(0); return; } this.spinnerService.show(); this._clinicsService .getClinicList(this.groupId,this.primengTableHelper.getMaxResultCount(this.paginator, event), this.primengTableHelper.getSkipCount(this.paginator, event),this.filterText) .pipe(finalize(() => this.spinnerService.hide())) .subscribe(result => { this.primengTableHelper.totalRecordsCount = result.totalCount; this.primengTableHelper.records = result.items; this.spinnerService.hide(); }); } addCustomersToGroup(): void { this.spinnerService.show(); const input = new AddContactForGroupInputDto(); input.groupId = this.groupId input.contactIds=_.map(this.selectedMembers, selectedMember => parseInt(selectedMember.value)); this._contactGroupService .addMultipleContactForGroup(input) .subscribe(() => { this.notify.success(this.l('SuccessfullyAdded')); this.membersAdded.emit({ contactIds: input.contactIds, ouId: input.groupId }); this.saving = false; this.close(); this.spinnerService.hide(); this.selectedMembers = []; }, error => { this.saving = false; this.spinnerService.hide(); }); this.filterText = undefined; } }