You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

200 line
7.5 KiB

  1. import {
  2. ChangeDetectionStrategy,
  3. ChangeDetectorRef,
  4. Component,
  5. EventEmitter,
  6. OnInit,
  7. Output,
  8. ViewChild
  9. } from '@angular/core';
  10. import {ICustomerMeeting} from '../../../../model/entities/customer-meeting';
  11. import {CustomerMeetingEditComponent} from '../customer-meeting-edit/customer-meeting-edit.component';
  12. import {Factory} from '../../../../factory/factory';
  13. import {ICustomer} from '../../../../model/entities/customer';
  14. import {Utils} from '../../../../utils/utils';
  15. import {IUser} from '../../../../model/entities/user';
  16. import {AppService} from '../../../../services/app.service';
  17. import {IMeetingType} from '../../../../model/entities/meeting-type';
  18. import {IConfig} from '../../../../model/virtual/config';
  19. import {NgForm} from '@angular/forms';
  20. // import {ScrollToService} from '@nicky-lenaers/ngx-scroll-to';
  21. import {CustomerService} from '../../../../services/customer.service';
  22. import {MeetingService} from '../../../../services/meeting.service';
  23. import {FormComponent} from '../../../../components/form-component';
  24. import {Const} from '../../../../utils/const';
  25. @Component({
  26. selector: 'app-customer-meeting-detail',
  27. templateUrl: './customer-meeting-detail.component.html',
  28. styleUrls: ['./customer-meeting-detail.component.scss'],
  29. // NOTE: We need manual change detection in this component
  30. changeDetection: ChangeDetectionStrategy.OnPush,
  31. })
  32. export class CustomerMeetingDetailComponent extends FormComponent implements OnInit {
  33. @Output() editMeeting: EventEmitter<ICustomerMeeting> = new EventEmitter<ICustomerMeeting>();
  34. @Output() deleteMeeting: EventEmitter<number> = new EventEmitter<number>();
  35. @Output() editMeetingReport: EventEmitter<any> = new EventEmitter<any>();
  36. @ViewChild('customerMeetingDetailEdit', { static: true }) customerMeetingDetailEdit: CustomerMeetingEditComponent;
  37. @ViewChild('customerMeetingReportForm', { static: true }) customerMeetingReportForm: NgForm;
  38. public customer: ICustomer;
  39. public customerMeeting: ICustomerMeeting;
  40. public isEditMode: boolean;
  41. public creator: IUser;
  42. public owner: IUser;
  43. public participants: IUser[];
  44. public meetingType: IMeetingType;
  45. public date: string;
  46. public timeStart: string;
  47. public timeEnd: string;
  48. public config: IConfig;
  49. public reportFormVisible: boolean;
  50. public reportOld: string;
  51. public hasEditRights: boolean;
  52. public hasReportRights: boolean;
  53. constructor(public appService: AppService, private customerService: CustomerService, private meetingService: MeetingService, /*protected scrollToService: ScrollToService,*/ private ref: ChangeDetectorRef) {
  54. super(/*scrollToService*/);
  55. }
  56. ngOnInit() {
  57. this.hasEditRights = this.appService.userHasRole(Const.USER_ROLE_ADMIN);
  58. this.hasReportRights = this.appService.userHasRole(Const.USER_ROLE_ADMIN);
  59. this.config = this.appService.getConfig();
  60. this.customer = Factory.getEmptyCustomer();
  61. this.customerMeeting = Factory.getEmptyCustomerMeeting();
  62. this.creator = Factory.getEmptyUser();
  63. this.owner = Factory.getEmptyUser();
  64. this.participants = [];
  65. this.meetingType = Factory.getEmptyMeetingType();
  66. this.date = '';
  67. this.timeStart = '';
  68. this.timeEnd = '';
  69. this.reportFormVisible = false;
  70. this.reportOld = '';
  71. }
  72. /**
  73. * Set data
  74. */
  75. public setData(customer: ICustomer, customerMeeting: ICustomerMeeting): void {
  76. this.customer = customer;
  77. this.customerMeeting = customerMeeting;
  78. this.hasEditRights =
  79. this.appService.userHasRole(Const.USER_ROLE_ADMIN) ||
  80. this.appService.getAppUser().id === customerMeeting.creation_user_id ||
  81. this.appService.getAppUser().id === customerMeeting.owner_user_id;
  82. this.hasReportRights =
  83. this.appService.userHasRole(Const.USER_ROLE_ADMIN) ||
  84. this.appService.getAppUser().id === customerMeeting.owner_user_id;
  85. this.setMeetingData();
  86. }
  87. /**
  88. * Updates data
  89. */
  90. public updateData(customer: ICustomer): void {
  91. this.customer = customer;
  92. if (null !== this.customerMeeting.id){
  93. this.customer.v_customer_meetings.forEach(item => {
  94. if (item.id === this.customerMeeting.id) {
  95. this.customerMeeting = item;
  96. }
  97. });
  98. this.setMeetingData();
  99. }
  100. }
  101. /**
  102. * Sets variables for meeting
  103. */
  104. public setMeetingData(): void {
  105. this.errorMsg = '';
  106. this.reportFormVisible = false;
  107. this.reportOld = this.customerMeeting.report;
  108. this.creator = this.appService.getConfig().vc_user_by_id[this.customerMeeting.creation_user_id];
  109. this.owner = this.appService.getConfig().vc_user_by_id[this.customerMeeting.owner_user_id];
  110. this.participants = [];
  111. this.customerMeeting.v_participants.forEach(item => {
  112. this.participants.push(this.appService.getConfig().vc_user_by_id[item.participant_user_id]);
  113. });
  114. this.meetingType = this.appService.getConfig().vc_meeting_types_by_id[this.customerMeeting.meeting_type_id];
  115. this.date = Utils.getDateTimeToDisplay(this.customerMeeting.start_date);
  116. this.timeStart = Utils.getDateTimeToDisplay(this.customerMeeting.start_date, false, true);
  117. this.timeEnd = Utils.getDateTimeToDisplay(this.customerMeeting.end_date, false, true);
  118. this.isEditMode = false;
  119. this.resetFormValidation();
  120. // NOTE: We need manual change detection in this component
  121. this.ref.detectChanges();
  122. }
  123. /**
  124. * Edit meeting entry
  125. */
  126. public editEntry(): void {
  127. this.isEditMode = true;
  128. this.customerMeetingDetailEdit.setData(this.customer, Utils.deepClone(this.customerMeeting) as ICustomerMeeting);
  129. }
  130. /**
  131. * Edits meeting entry
  132. */
  133. public editCustomerMeetingFunction(customerMeeting: ICustomerMeeting): void {
  134. this.editMeeting.emit(customerMeeting);
  135. }
  136. /**
  137. * Delete meeting entry
  138. */
  139. public deleteEntry(): void {
  140. const confirmAction = confirm('Wollen Sie diesen Eintrag wirklich löschen?');
  141. if (confirmAction === true) {
  142. this.deleteMeeting.emit(this.customerMeeting.id);
  143. }
  144. }
  145. /**
  146. * Check if edit report after meeting has started
  147. */
  148. public editReport(): void {
  149. this.meetingService.apiCheckCustomerMeetingReport(this.customerMeeting.id).subscribe(
  150. data => {
  151. this.reportFormVisible = data.result_data as boolean;
  152. // NOTE: We need manual change detection in this component
  153. this.ref.detectChanges();
  154. // todo this.scrollUp('customer-meeting-edit-report');
  155. },
  156. error => {
  157. }
  158. );
  159. }
  160. /**
  161. * Resets Form Validation
  162. */
  163. public resetFormValidation(): void {
  164. if (this.reportFormVisible) {
  165. this.customerMeetingReportForm.form.markAsUntouched();
  166. }
  167. }
  168. /**
  169. * Saves customer meeting report
  170. */
  171. public onFormSubmit({value, valid}: { value: any, valid: boolean }): void {
  172. this.errorMsg = 'Bitte beheben Sie alle Fehler.';
  173. if (valid) {
  174. this.editMeetingReport.emit({
  175. id: this.customerMeeting.id,
  176. report: this.customerMeeting.report
  177. });
  178. this.errorMsg = '';
  179. } else {
  180. // todo this.scrollUp('customer-meeting-edit-report');
  181. }
  182. }
  183. }