import { Component, Input, OnInit } from '@angular/core'; import { FormBuilder } from '@angular/forms'; import { IPowerClient } from 'src/app/shared/models/ipower-client.interface'; import { SearchSystemException} from 'src/app/shared/models/request/search-system-exceptions.interface'; import { IpowerClientsService } from 'src/app/shared/services/administration/ipower-clients.service'; @Component({ selector: 'app-application-level-form', templateUrl: './application-level-form.component.html', styleUrls: ['./application-level-form.component.scss'] }) export class ApplicationLevelFormComponent implements OnInit { iPowerClientNameSuggestions: any; iPowerClientCodeSuggestions: any; private iPowerClientSuggestions: IPowerClient[]; private selectedIPowerClient: IPowerClient; searchCriteriaForm = this.fb.group({ processId: [null], clientId: [null], clientName: [null], jobName: [null], dmsFileCode: [null], dateFrom: [null], dateTo: [null], }); constructor(private fb: FormBuilder, private iPowerClientsService: IpowerClientsService) { } ngOnInit(): void { } public formValue(): SearchSystemException { let formValue: SearchSystemException = { processId: this.searchCriteriaForm.get('processId').value, clientId: this.searchCriteriaForm.get('clientId').value, jobName: this.searchCriteriaForm.get('jobName').value, dmsFileCode: this.searchCriteriaForm.get('dmsFileCode').value, dateFrom: this.searchCriteriaForm.get('dateFrom').value, dateTo: this.searchCriteriaForm.get('dateTo').value }; formValue.dateFrom = new Date(Date.UTC(formValue.dateFrom?.getFullYear(), formValue.dateFrom?.getMonth(), formValue.dateFrom?.getDate())); formValue.dateTo = new Date(Date.UTC(formValue.dateTo?.getFullYear(), formValue.dateTo?.getMonth(),formValue.dateTo?.getDate() + 1)); formValue.dateTo?.setMilliseconds(formValue.dateTo?.getMilliseconds() -1); return formValue; } /* * Auto-suggest & Auto-complete IPower Client */ autosuggestIPowerClientName(event): void { if (!event.query || event.query.length < 3) { this.iPowerClientNameSuggestions = []; return; } this.iPowerClientsService.getClientsByNameOnly(event.query).subscribe( (values: IPowerClient[]) => { this.iPowerClientSuggestions = values; const temp: string[] = []; values.map(val => temp.push(val.name)); this.iPowerClientNameSuggestions = temp; }, err => console.error(err) // TODO: Handle this via a Messaging Service ); } autosuggestIPowerClientCode(event): void { if (event.query.length < 3) { this.iPowerClientCodeSuggestions = []; return; } this.iPowerClientsService.getClientsByCodeOnly(event.query).subscribe( (values: IPowerClient[]) => { this.iPowerClientSuggestions = values; const temp: string[] = []; values.map(val => temp.push(val.clientCode)); this.iPowerClientCodeSuggestions = temp; }, err => console.error(err) ); } iPowerClientNameSelected(name: string): void { this.selectedIPowerClient = this.iPowerClientSuggestions.find(client => client.name === name); this.searchCriteriaForm.get('clientId').patchValue(this.selectedIPowerClient ? this.selectedIPowerClient.clientCode : ''); this.searchCriteriaForm.updateValueAndValidity(); } iPowerClientCodeSelected(code: string): void { this.selectedIPowerClient = this.iPowerClientSuggestions.find(client => client.clientCode === code); this.searchCriteriaForm.get('clientName').patchValue(this.selectedIPowerClient ? this.selectedIPowerClient.name : ''); this.searchCriteriaForm.updateValueAndValidity(); } resetForm() { this.searchCriteriaForm.reset(); } }