// Importing the libraries import { Component, OnInit } from '@angular/core'; import { DynamicDialogRef, DynamicDialogConfig, DialogService, MessageService, TreeNode } from 'primeng/api'; import { AddClientComponent } from '../add-client/add-client.component'; import { AddContactComponent } from '../add-contact/add-contact.component'; import { AddStaffComponent } from '../add-staff/add-staff.component'; import { CookieService } from 'ngx-cookie-service'; import { IContactDetails } from '../../../shared/core/IContactDetails'; import { ClientDashboardGrid } from '../../../shared/core/IclientsDashboardGrid'; import { DashboardServicesService } from '../../../shared/services/dashboard-services.service'; import { JobplanServicesService } from '../../../shared/services/jobplan-services.service'; import { ActivityServicesService } from '../../../shared/services/activity-services.service'; import { JobPlanDashboardGrid } from '../../../shared/core/IJobPlanDashboardGrid'; import { IEditJobPlan } from '../../../shared/core/IEditJobPlan'; import { DatePipe } from '@angular/common' @Component({ selector: 'app-add-job-plan', templateUrl: './add-job-plan.component.html', styleUrls: ['./add-job-plan.component.css'] }) export class AddJobPlanComponent implements OnInit { // Varibales to hold the user inputs jobPlanName = ''; staffId: any; Clients: any = []; LinkedStaff: any = []; Templates: any = []; jobTemplateSelected: any = []; startDate: any; startTime: any; dueDate: any; dueTime: any; linkStaffSelected: any = []; contacts: any = []; selectedContacts: IContactDetails[]; contactDisable = true; clientSelected: ClientDashboardGrid[]; data: any; activityFlag: any; typeId = 0; locationId = 0; clientId: number; locations: any = []; locationList: any = []; childrenArray1: any = []; childrenArray2: any = []; // locationDetails: { SubjectsId: any; ClientId: number; SubjectsDescription: any; TopicsList: any; // CategoryList: any; SubCategoryList: any; SuperCategoryList: any; }; childrenArray3: any = []; subjectId: number; topicArray: any = []; categoryArray: any = []; subCategoryArray: any = []; superCategoryArray: any = []; // tslint:disable-next-line:max-line-length locationDetails: { SubjectsId: number; ClientId: number; SubjectsDescription: any; TopicsList: any; CategoryList: any; SubCategoryList: any; SuperCategoryList: any; }; subjectName: any; selectedFile: TreeNode; // Edit job plan editJobPlanFlag: boolean; selectedJobPlanId: boolean; selectedClientDetailsId: boolean; // job plan jobPlanEdit: IEditJobPlan; // Place holder placeholderClient: string = 'Choose client'; checked: boolean = false; placeholderTemplate: string = 'Select'; // saving icon showLoading: boolean = false; // Disable button disableSaveButton = true; constructor(public ref: DynamicDialogRef, public config: DynamicDialogConfig, public dashboardService: DashboardServicesService, private jobplanServices: JobplanServicesService, public dialogService: DialogService, private activitiesService: ActivityServicesService, private messageService: MessageService, private cookieService: CookieService, public datepipe: DatePipe) { // Assing logged userId this.staffId = this.cookieService.get('LoggedUserId'); } ngOnInit() { this.activityFlag = this.config.data.activityFlag; if (this.activityFlag === true) { this.typeId = this.config.data.typeId; this.locationId = this.config.data.locationId; this.clientId = this.config.data.clientId; } // Get flag this.editJobPlanFlag = this.config.data.editJobPlanFlag; // Set edit job plans if (this.editJobPlanFlag === true) { this.selectedJobPlanId = this.config.data.selectedJobPlanId; this.selectedClientDetailsId = this.config.data.selectedClientDetailsId; // Get job plan details debugger this.jobplanServices.GetEditJobPlan(+this.selectedJobPlanId, +this.selectedClientDetailsId, this.staffId, +this.cookieService.get('CompanyId'), 'EDIT').subscribe( result => { if (result !== undefined && result !== null) { this.jobPlanEdit = result; console.log('Edit job plan details', this.jobPlanEdit); this.jobPlanName = this.jobPlanEdit.JobPlanName; this.placeholderClient = this.jobPlanEdit.LinkedClientName; this.checked = this.jobPlanEdit.IsSelectedTemplate; this.placeholderTemplate = this.jobPlanEdit.TemplateName; this.startDate = this.datepipe.transform(this.jobPlanEdit.StardDate, 'yyyy-MM-dd'); this.startTime = this.jobPlanEdit.StartTime; this.dueDate = this.datepipe.transform(this.jobPlanEdit.DueDate, 'yyyy-MM-dd'); this.dueTime = this.jobPlanEdit.DueTime; } } ); } console.log('Param test', this.typeId, this.locationId, this.clientId); this.getAllClients(this.staffId); this.getAllLinkedStaff(); this.getJobtemplates(+this.cookieService.get('CompanyId')); this.data = [ { label: 'Documents', data: 'Documents Folder', expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [{ label: 'Work', data: 'Work Folder', expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', // tslint:disable-next-line:max-line-length children: [{ label: 'Expenses.doc', icon: 'fa fa-file-word-o', data: 'Expenses Document' }, { label: 'Resume.doc', icon: 'fa fa-file-word-o', data: 'Resume Document' }] }, { label: 'Home', data: 'Home Folder', expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [{ label: 'Invoices.txt', icon: 'fa fa-file-word-o', data: 'Invoices for this month' }] }] }, { label: 'Pictures', data: 'Pictures Folder', expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [ { label: 'barcelona.jpg', icon: 'fa fa-file-image-o', data: 'Barcelona Photo' }, { label: 'logo.jpg', icon: 'fa fa-file-image-o', data: 'PrimeFaces Logo' }, { label: 'primeui.png', icon: 'fa fa-file-image-o', data: 'PrimeUI Logo' }] }, { label: 'Movies', data: 'Movies Folder', expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [{ label: 'Al Pacino', data: 'Pacino Movies', children: [{ label: 'Scarface', icon: 'fa fa-file-video-o', data: 'Scarface Movie' }, { label: 'Serpico', icon: 'fa fa-file-video-o', data: 'Serpico Movie' }] }, { label: 'Robert De Niro', data: 'De Niro Movies', children: [{ label: 'Goodfellas', icon: 'fa fa-file-video-o', data: 'Goodfellas Movie' }, { label: 'Untouchables', icon: 'fa fa-file-video-o', data: 'Untouchables Movie' }] }] } ]; } getAllClients(staffId) { this.dashboardService.getAllClientsByStaffId(staffId) .subscribe(res => { this.Clients = res; if (this.activityFlag === true) { // tslint:disable-next-line:prefer-for-of for (let i = 0; i < this.Clients.length; i++) { if (this.clientId.toString() === this.Clients[i].ClientDetailsId.toString()) { this.clientSelected = this.Clients[i]; this.contactDisable = false; this.getContactsByClientId(this.clientId); } } } console.log('Client', this.Clients); }, error => { console.log('Error', error); }); } getAllLinkedStaff() { this.dashboardService.getAllLinkedStaff() .subscribe(res => { console.log('Linked', res); this.LinkedStaff = res; }, error => { console.log('Error', error); } ); } getJobtemplates(companyId: number) { this.jobplanServices.getJobTemplate(companyId) .subscribe(res => { console.log('Templates', res); this.Templates = res; }, error => { console.log('Error', error); } ); } saveJobPlan() { // if (this.activityFlag === true) { const newJobPlanObj: JobPlanDashboardGrid = { JobPlanId: 0, TemplateId: this.jobTemplateSelected.JobTemplateId, JobPlanStatusId: 1, JobPlanName: this.jobPlanName, StartDate: this.startDate, StartTime: this.startTime, EndDate: this.dueDate, EndTime: this.dueTime, Instructions: '', ClientDetails: [{ // tslint:disable-next-line:no-string-literal ClientDetailsId: this.clientSelected['ClientDetailsId'], }], ContactDetails: this.selectedContacts, LinkedStaffs: this.linkStaffSelected }; this.showLoading = true; // console.log('test jp without location', this.clientId, this.locationId, this.typeId); this.jobplanServices.saveJobPlanFromActivity(this.clientId, this.locationId, this.typeId, newJobPlanObj, +this.cookieService.get('CompanyId')) .subscribe(res => { this.showLoading = false; // console.log('Save job plan from activity', res); this.ref.close(res); }, error => console.log('Error', error)); // } else { // const newJobPlanObj: JobPlanDashboardGrid = { // JobPlanId: 0, // TemplateId: this.jobTemplateSelected.JobTemplateId, // JobPlanStatusId: 1, // JobPlanName: this.jobPlanName, // StartDate: this.startDate, // StartTime: this.startTime, // EndDate: this.dueDate, // EndTime: this.dueTime, // Instructions: '', // ClientDetails: [{ // // tslint:disable-next-line:no-string-literal // ClientDetailsId: this.clientSelected['ClientDetailsId'], // }], // ContactDetails: this.selectedContacts, // LinkedStaffs: this.linkStaffSelected // }; // this.jobplanServices.insertNewJobPlan(newJobPlanObj) // .subscribe(res => { // console.log('Insert job', res); // }, error => { console.log('Error', error); } // ); // } } onChangeTemplate(e) { console.log('Change template event', this.jobTemplateSelected.JobTemplateId); } showAddClients() { const ref = this.dialogService.open(AddClientComponent, { header: 'Add New Client', width: '40%' }); ref.onClose.subscribe(() => { // Refresh the clients list }); } showAddContact() { const ref = this.dialogService.open(AddContactComponent, { header: 'Add New Contact', width: '40%' }); ref.onClose.subscribe(() => { // Refresh the clients list }); } // Close Popup cance() { this.ref.close(); this.dialogService.dialogComponentRef.instance.close(); } showAddStaff() { const ref = this.dialogService.open(AddStaffComponent, { header: 'Add New Staff', width: '40%' }); ref.onClose.subscribe(() => { // Refresh the clients list }); } getContactsByClientId(clientId) { this.jobplanServices.getContactsByClientId(clientId) .subscribe(res => { this.contacts = res; console.log('Contacts', res); }, error => { console.log('Error', error); } ); } onSelectClient(e) { this.clientId = e.value.ClientDetailsId; console.log('On select client', e.value.ClientDetailsId); this.contactDisable = false; this.getContactsByClientId(e.value.ClientDetailsId); this.getLocationByClientId(e.value.ClientDetailsId); } getLocationByClientId(clientId) { this.jobplanServices.getLocationsForAddJobplan(clientId) .subscribe(res => { this.locations = res; const addNewObj = { label: 'Add New Subject', data: '', new: true, phaseType: 1, expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [] }; // tslint:disable-next-line:prefer-for-of for (let i = 0; i < this.locations.length; i++) { const addNewObj2 = { label: 'Add New Topic', data: '', new: true, phaseType: 2, expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [] }; this.childrenArray1 = []; this.locations[i].label = this.locations[i].SubjectsDescription; this.locations[i].date = this.locations[i].SubjectsId; this.locations[i].expandedIcon = 'fa fa-folder-open'; this.locations[i].collapsedIcon = 'fa fa-folder'; this.locations[i].new = false; this.locations[i].phaseType = 1; this.childrenArray1.push(addNewObj2); this.locations[i].children = this.childrenArray1; // tslint:disable-next-line:prefer-for-of for (let a = 0; a < this.locations[i].TopicsList.length; a++) { this.childrenArray2 = []; const addNewObj3 = { label: 'Add New Category', data: '', new: true, phaseType: 3, expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [] }; this.locations[i].TopicsList[a].label = this.locations[i].TopicsList[a].TopicDescription; this.locations[i].TopicsList[a].date = this.locations[i].TopicsList[a].TopicId; this.locations[i].TopicsList[a].expandedIcon = 'fa fa-folder-open'; this.locations[i].TopicsList[a].collapsedIcon = 'fa fa-folder'; this.locations[i].TopicsList[a].new = false; this.locations[i].TopicsList[a].phaseType = 2; this.childrenArray1.push(this.locations[i].TopicsList[a]); this.locations[i].children = this.childrenArray1; this.childrenArray2.push(addNewObj3); this.locations[i].TopicsList[a].children = this.childrenArray2; // tslint:disable-next-line:prefer-for-of for (let y = 0; y < this.locations[i].TopicsList[a].CategoryList.length; y++) { this.childrenArray3 = []; const addNewObj4 = { label: 'Add New Sub Category', data: '', new: true, phaseType: 4, expandedIcon: 'fa fa-folder-open', collapsedIcon: 'fa fa-folder', children: [] }; this.locations[i].TopicsList[a].CategoryList[y].label = this.locations[i].TopicsList[a].CategoryList[y].CategorDescription; this.locations[i].TopicsList[a].CategoryList[y].date = this.locations[i].TopicsList[a].CategoryList[y].CategoryId; this.locations[i].TopicsList[a].CategoryList[y].expandedIcon = 'fa fa-folder-open'; this.locations[i].TopicsList[a].CategoryList[y].collapsedIcon = 'fa fa-folder'; this.locations[i].TopicsList[a].CategoryList[y].new = false; this.locations[i].TopicsList[a].CategoryList[y].phaseType = 3; this.locations[i].TopicsList[a].CategoryList[y].children = this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList; this.childrenArray2.push(this.locations[i].TopicsList[a].CategoryList[y]); this.locations[i].TopicsList[a].children = this.childrenArray2; this.childrenArray3.push(addNewObj4); this.locations[i].TopicsList[a].CategoryList[y].children = this.childrenArray3; console.log('Test', this.childrenArray3); // tslint:disable-next-line:prefer-for-of for (let x = 0; x < this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList.length; x++) { // tslint:disable-next-line:max-line-length this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].label = this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].SubCategoryDescription; // tslint:disable-next-line:max-line-length this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].date = this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].SubCategoryId; this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].expandedIcon = 'fa fa-folder-open'; this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].collapsedIcon = 'fa fa-folder'; this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].new = false; this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x].phaseType = 4; this.childrenArray3.push(this.locations[i].TopicsList[a].CategoryList[y].SubCategoryList[x]); this.locations[i].TopicsList[a].CategoryList[y].children = this.childrenArray3; } } } } this.locations.push(addNewObj); console.log('Get locations', this.locations); } , error => (console.log('Error', error))); } getLocationOnClick(val) { console.log('location select', val); if (val.phaseType === 1) { this.subjectId = -1; this.subjectName = val.label; } else if (val.phaseType === 2) { this.subjectId = val.parent.SubjectsId; const topicObj = { SubjectsId: this.subjectId, TopicId: -1, TopicDescription: val.label }; console.log('Location', location); this.topicArray = []; this.topicArray.push(topicObj); this.categoryArray = []; this.subCategoryArray = []; this.superCategoryArray = []; } else if (val.phaseType === 3) { this.subjectId = val.parent.SubjectsId; const categoryObj = { TopicId: val.parent.TopicId, CategoryId: -1, CategorDescription: val.label }; this.categoryArray = []; this.categoryArray.push(categoryObj); this.topicArray = []; this.subCategoryArray = []; this.superCategoryArray = []; } else if (val.phaseType === 4) { this.subjectId = val.parent.SubjectsId; const subCategoryObj = { CategoryId: val.parent.CategoryId, SubCategoryId: -1, SubCategoryDescription: val.label }; this.categoryArray = []; this.subCategoryArray = []; this.subCategoryArray.push(subCategoryObj); this.topicArray = []; this.superCategoryArray = []; } else if (val.phaseType === 5) { this.subjectId = val.parent.SubjectsId; const superCategoryObj = { SubCategoryId: val.parent.CategoryId, SuperCategorId: -1, SuperCategoryDescription: val.label }; this.superCategoryArray = []; this.superCategoryArray.push(superCategoryObj); this.topicArray = []; this.categoryArray = []; this.subCategoryArray = []; } this.locationDetails = { SubjectsId: this.subjectId, ClientId: this.clientId, SubjectsDescription: this.subjectName, TopicsList: this.topicArray, CategoryList: this.categoryArray, SubCategoryList: this.subCategoryArray, SuperCategoryList: this.superCategoryArray }; console.log('location', this.locationDetails); this.activitiesService.saveLocationStructure(this.locationDetails) .subscribe(res => { this.getLocationByClientId(this.clientId); console.log('Save subject responce', res); }, error => console.log('Error', error)); } nodeSelect(node) { this.messageService.add({ severity: 'info', summary: 'Node Selected', detail: node.label }); console.log('Node select', node); if (node.phaseType === 1) { console.log('Test', this.locationId); this.locationId = node.SubjectsId; this.typeId = 1; } else if (node.phaseType === 2) { this.locationId = node.TopicId; this.typeId = 2; } else if (node.phaseType === 3) { this.locationId = node.CategoryId; this.typeId = 3; } else if (node.phaseType === 4) { this.locationId = node.SubCategoryId; this.typeId = 4; } } // Validate Special Charactors omit_special_char(event) { let k; k = event.charCode; // k = event.keyCode; (Both can be used) // return((k > 64 && k < 91) || (k > 96 && k < 123) || k === 8 || k === 32 || (k >= 48 && k <= 57)); // Special Charactor validation return (!(k >= 27 && k <= 31) && !(k >= 33 && k <= 47) && !(k >= 58 && k <= 64) && !(k >= 91 && k <= 96) && !(k >= 123 && k <= 127)); } // load new staff LoadStaffWhenNewlyAdded() { this.getAllLinkedStaff(); } // load new contact LoadContactWhenNewlyAdded() { this.getContactsByClientId(this.clientId); } // LoadClientWhenNewlyAdded LoadClientWhenNewlyAdded() { this.getAllClients(this.staffId); } // validate validateFields() { if (this.jobPlanName !== '') { this.disableSaveButton = false; } else { this.disableSaveButton = true; } } }