import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core'; import {ICustomerNote} from '../../../../model/entities/customer-note'; import {NgForm} from '@angular/forms'; import {Factory} from '../../../../factory/factory'; import {ICustomer} from '../../../../model/entities/customer'; import {ICustomerContact} from '../../../../model/entities/customer-contact'; import {Utils} from '../../../../utils/utils'; import {ScrollToService} from '@nicky-lenaers/ngx-scroll-to'; import {FormComponent} from '../../../../components/form-component'; @Component({ selector: 'app-customer-note-edit', templateUrl: './customer-note-edit.component.html', styleUrls: ['./customer-note-edit.component.scss'] }) export class CustomerNoteEditComponent extends FormComponent implements OnInit { @Output() createNote: EventEmitter = new EventEmitter(); @Output() editNote: EventEmitter = new EventEmitter(); @ViewChild('customerNoteForm', { static: true }) customerNoteForm: NgForm; public customer: ICustomer; public customerNote: ICustomerNote; public customerContacts: ICustomerContact[]; public customerContactsById: {}; public isExistingCustomerContact: boolean; public contactId: string; constructor(protected scrollToService: ScrollToService) { super(scrollToService); } ngOnInit() { this.customer = Factory.getEmptyCustomer(); this.customerNote = Factory.getEmptyCustomerNote(); this.customerContacts = []; this.customerContactsById = {}; this.errorMsg = ''; this.contactId = ''; this.isCreationMode = false; this.isExistingCustomerContact = false; } /** * Sets data * @param customer * @param customerNote * @param isCreationMode */ public setData(customer: ICustomer, customerNote: ICustomerNote, isCreationMode = false): void { this.customer = customer; this.customerNote = customerNote; this.customerContacts = this.customer.v_customer_contacts; this.customerContactsById = Utils.getSortedObjFromArray(this.customerContacts, 'id'); this.customerNote.customer_id = this.customer.id; this.contactId = ''; this.isCreationMode = isCreationMode; this.errorMsg = ''; if (null === this.customerNote.customer_contact_id) { this.customerNote.customer_contact_id = -1; } if (this.customerNote.customer_contact_id > 0) { this.isExistingCustomerContact = true; } this.resetFormValidation(); } /** * On select contact * @param e */ public selectContact(e: any) { this.customerNote.customer_contact_id = e.target.value; this.isExistingCustomerContact = false; this.customerNote.gender = null; this.customerNote.firstname = null; this.customerNote.lastname = null; this.customerNote.department = null; this.customerNote.email = null; this.customerNote.phone_no = null; this.customerNote.mobile_no = null; this.customerNote.fax_no = null; if (this.customerNote.customer_contact_id > 0) { this.customerNote.gender = this.customerContactsById[this.customerNote.customer_contact_id].gender; this.customerNote.firstname = this.customerContactsById[this.customerNote.customer_contact_id].firstname; this.customerNote.lastname = this.customerContactsById[this.customerNote.customer_contact_id].lastname; this.customerNote.department = this.customerContactsById[this.customerNote.customer_contact_id].department; this.customerNote.email = this.customerContactsById[this.customerNote.customer_contact_id].email; this.customerNote.phone_no = this.customerContactsById[this.customerNote.customer_contact_id].phone_no; this.customerNote.mobile_no = this.customerContactsById[this.customerNote.customer_contact_id].mobile_no; this.customerNote.fax_no = this.customerContactsById[this.customerNote.customer_contact_id].fax_no; this.isExistingCustomerContact = true; } } /** * Resets Form Validation */ public resetFormValidation(): void { this.customerNoteForm.form.markAsUntouched(); } /** * Saves customer * @param value * @param valid */ public onFormSubmit({value, valid}: { value: any, valid: boolean }): void { this.errorMsg = 'Bitte beheben Sie alle Fehler.'; if (valid) { if (this.customerNote.customer_contact_id == -1) { this.customerNote.customer_contact_id = null; } if (this.isCreationMode) { this.createNote.emit(this.customerNote); } else { this.editNote.emit(this.customerNote); } this.errorMsg = ''; } else { this.scrollUp(this.isCreationMode ? 'customer-note-modal' : 'customer-note-edit-modal'); } } }