import { ChangeDetectorRef, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { CreateOrganizationUnitInput, OrganizationUnitDto, OrganizationUnitServiceProxy, UpdateOrganizationUnitInput,ContactGroupServiceProxy,CreateOrUpdateContactGroupInputDto } from '@shared/service-proxies/service-proxies'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; export interface IClinicGroupUnit { id?: number; parentId?: number; displayName?: string; code?:string; description?:string; isActive?:boolean; color?:string; icon?:string; } @Component({ selector: 'createOrEditCustomerGroupsModal', templateUrl: './create-or-edit-customer-groups.component.html' }) export class CreateOrEditCustomerGroupsModal extends AppComponentBase { @ViewChild('createOrEditModal', {static: true}) modal: ModalDirective; @ViewChild('organizationUnitDisplayName', {static: true}) organizationUnitDisplayNameInput: ElementRef; @Output() unitCreated: EventEmitter = new EventEmitter(); @Output() unitUpdated: EventEmitter = new EventEmitter(); active = false; saving = false; customerGroupUnit: IClinicGroupUnit; constructor( injector: Injector, private _organizationUnitService: OrganizationUnitServiceProxy, private _contactGroupService: ContactGroupServiceProxy, private _changeDetector: ChangeDetectorRef ) { super(injector); } onShown(): void { document.getElementById('OrganizationUnitDisplayName').focus(); } showModal(customerGroupUnit): void { this.customerGroupUnit = customerGroupUnit; console.log(customerGroupUnit) this.active = true; this.modal.show(); this._changeDetector.detectChanges(); } save(): void { this.saving = true; var data = new CreateOrUpdateContactGroupInputDto(); data.code = this.customerGroupUnit.code; data.description = this.customerGroupUnit.description; data.color = this.customerGroupUnit.color; data.isActive = this.customerGroupUnit.isActive; data.icon = this.customerGroupUnit.icon; if(this.customerGroupUnit.id != null){ data.id = this.customerGroupUnit.id; } this._contactGroupService.createOrUpdateContactGroup(data).subscribe(reult => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.unitCreated.emit(); this.saving = false; }); } createUnit() { const createInput = new CreateOrganizationUnitInput(); createInput.parentId = this.customerGroupUnit.parentId; createInput.displayName = this.customerGroupUnit.displayName; this.saving = true; this._organizationUnitService .createOrganizationUnit(createInput) .pipe(finalize(() => this.saving = false)) .subscribe((result: OrganizationUnitDto) => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.unitCreated.emit(result); }); } updateUnit() { const updateInput = new UpdateOrganizationUnitInput(); updateInput.id = this.customerGroupUnit.id; updateInput.displayName = this.customerGroupUnit.displayName; this.saving = true; this._organizationUnitService .updateOrganizationUnit(updateInput) .pipe(finalize(() => this.saving = false)) .subscribe((result: OrganizationUnitDto) => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.unitUpdated.emit(result); }); } close(): void { this.modal.hide(); this.active = false; } }