import { Component, OnInit } from '@angular/core'; import { DialogService, DynamicDialogConfig } from 'primeng/api'; import { THIS_EXPR } from '@angular/compiler/src/output/output_ast'; import { DashboardServicesService } from '../../../shared/services/dashboard-services.service'; import { ActivityServicesService } from '../../../shared/services/activity-services.service'; import { ILinkedStaff } from '../../../shared/core/ILinkedStaff'; import { IMessages } from '../../../shared/core/IMessages'; @Component({ selector: 'app-add-message', templateUrl: './add-message.component.html', styleUrls: ['./add-message.component.css'], providers: [DashboardServicesService, ActivityServicesService] }) export class AddMessageComponent implements OnInit { linkStaffs: ILinkedStaff[] = []; selectedStaff: ILinkedStaff; // Variables to hold the user input subject = ''; message = ''; // Variable to identify the message type isInternalMessage = false; // Disable the btn disableBtn = true; ulpoadFile: string; // Jobplanid JobPlanId: number; constructor(public dialogService: DialogService, public config: DynamicDialogConfig, private dashboardServicesService: DashboardServicesService, private activitiesService: ActivityServicesService) { } ngOnInit() { this.dashboardServicesService.getAllLinkedStaff().subscribe( data => { this.linkStaffs = data; this.selectedStaff = this.linkStaffs[0]; } ); // tslint:disable-next-line:no-string-literal if (this.config.data.isInternalMessage != null) { // tslint:disable-next-line:no-string-literal this.isInternalMessage = this.config.data.isInternalMessage; // Assign Job plan id this.JobPlanId = this.config.data.JobPlanId; } } // Closing the popup closePopUp() { this.dialogService.dialogComponentRef.instance.close(); } // Validate fields validateFileds() { if (this.subject !== '' && this.message !== '') { this.disableBtn = false; } else { this.disableBtn = true; } } // Save on click function saveOnClick() { let messageParentType = ''; let selectedStaffID = 0; if (this.isInternalMessage) { messageParentType = 'INTERNAL'; selectedStaffID = this.selectedStaff.StaffId; } else { messageParentType = 'EXTERNAL'; selectedStaffID = 0; } const imaessage: IMessages = { MessageId: 0, MessageParentType: { Description: messageParentType, MessageTypeId: 0 }, MessageType: { Description: 'Outgoing', MessageTypeId: 0 }, MessageStatus: { Description: 'SENT', MessageTypeId: 0 }, JobPlanId: this.JobPlanId, StaffId: selectedStaffID, Subject: this.subject, Description: this.message, FilePath: '', CreateDate: '', LastUpdated: '', LoggedStaffID: 14 }; const frmData = new FormData(); frmData.append('fileUpload', this.ulpoadFile); this.activitiesService.UploadFile(frmData).subscribe( filePath => { // Setting the file path imaessage.FilePath = filePath; this.activitiesService.InsertMessage(imaessage).subscribe( data => { this.closePopUp(); } ); } ); } saveDraftOnClick() { let messageParentType = ''; let selectedStaffID = 0; if (this.isInternalMessage) { messageParentType = 'INTERNAL'; selectedStaffID = this.selectedStaff.StaffId; } else { messageParentType = 'EXTERNAL'; selectedStaffID = 0; } const imaessage: IMessages = { MessageId: 0, MessageParentType: { Description: messageParentType, MessageTypeId: 0 }, MessageType: { Description: 'Outgoing', MessageTypeId: 0 }, MessageStatus: { Description: 'DRAFT', MessageTypeId: 0 }, JobPlanId: this.JobPlanId, StaffId: selectedStaffID, Subject: this.subject, Description: this.message, FilePath: '', CreateDate: '', LastUpdated: '', LoggedStaffID: 14 }; const frmData = new FormData(); frmData.append('fileUpload', this.ulpoadFile); this.activitiesService.UploadFile(frmData).subscribe( filePath => { // Setting the file path imaessage.FilePath = filePath; this.activitiesService.InsertMessage(imaessage).subscribe( data => { } ); } ); this.closePopUp(); } // Upload file Upload(event: any): void { const files = event.target.files; if (files) { for (const file of files) { const reader = new FileReader(); reader.onload = (e: any) => { }; reader.readAsDataURL(file); } } this.ulpoadFile = event.target.files[0]; } }