import { Component, Injector, ViewChild, OnInit, ElementRef, Output, EventEmitter } from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { AppComponentBase } from '@shared/common/app-component-base'; import { BusinessHoursServiceProxy, ContactPersonsEditDto, ContactPersonsServiceProxy, CreateOrEditBusinessHoursInput, CreateOrEditContactPersonInput, DAYOFTHEWEEKCODEDTO, GetBusinessHoursDto, GetContactPersonsDto } from '@shared/service-proxies/service-proxies'; import { finalize } from 'rxjs/operators'; declare var $: any; @Component({ selector: 'editContactPersonModal', templateUrl: './edit-contact-person.component.html', styleUrls: ['./customer-portal.component.css'] }) export class EditContactPersonModal extends AppComponentBase { @Output() modalSave: EventEmitter = new EventEmitter(); @ViewChild('modal', { static: false }) modal: ModalDirective; active: boolean = false; saving: boolean = false; getPerson = new GetContactPersonsDto(); personInput = new CreateOrEditContactPersonInput(); personInputList: Array = []; toAdd: ContactPersonsEditDto; firstName:string; lastName:string; phone:string; email:string; contactId: number; constructor( injector: Injector, private _contactPersonRepository: ContactPersonsServiceProxy ) { super(injector); } ngOnInit() { this.personInput.createOrEditContactPerson = []; } show(person: GetContactPersonsDto): void { this.spinnerService.show(); this.personInputList = []; this.getPerson = person; this.personInput.parentContactId = this.getPerson.parentContactId; this.contactId = this.getPerson.parentContactId; if(this.getPerson!=null){ for (var data in person.getContactPersons) { this.toAdd = new ContactPersonsEditDto(); this.toAdd.id = this.getPerson.getContactPersons[data].id; this.toAdd.firstName = this.getPerson.getContactPersons[data].firstName; this.toAdd.lastName = this.getPerson.getContactPersons[data].lastName; this.toAdd.phone = this.getPerson.getContactPersons[data].phone; this.toAdd.email = this.getPerson.getContactPersons[data].email; this.toAdd.contactId = this.getPerson.getContactPersons[data].contactId; this.personInputList.push(this.toAdd) } } this.spinnerService.hide(); this.active = true; this.modal.show(); } onShown(): void { $('.kt-select2').select2(); // this.nameInput.nativeElement.focus(); } addHour() { this.toAdd = new ContactPersonsEditDto(); this.toAdd.firstName = this.firstName; this.toAdd.lastName = this.lastName; this.toAdd.phone = this.phone; this.toAdd.email = this.email; this.toAdd.id = undefined; this.toAdd.contactId = undefined; this.personInputList.push(this.toAdd); this.firstName = undefined; this.lastName = undefined; this.phone = undefined; this.email =undefined; } deleteHour(id: number) { this.personInputList.splice(id, 1); } save(): void { this.saving = true; this.spinnerService.show(); this.personInput.parentContactId = this.contactId; this.personInput.createOrEditContactPerson = this.personInputList; console.log(this.personInput); this._contactPersonRepository.createOrEditContactPerson(this.personInput) .pipe(finalize(() => { this.saving = false; })) .subscribe(() =>{ this.notify.info(this.l('SavedSuccessfully')); this.spinnerService.hide(); this.close(); this.modalSave.emit(null); }) } close(): void { this.modal.hide(); this.active = false; } }