import {Component, OnInit, Input, Inject, ChangeDetectorRef} from '@angular/core'; import {FormFieldsService} from './form-fields.service'; import {MatDialog, MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog'; import {Observable} from 'rxjs'; import {MatSnackBar} from '@angular/material/snack-bar'; export interface DialogData { name: string; } @Component({ selector: 'app-form-fields', templateUrl: './form-fields.component.html', styleUrls: ['./form-fields.component.css'] }) export class FormFieldsComponent implements OnInit { @Input('reportId') reportId1: string; formFieldsListObj: any[]; postMoveUpwrdObj: {}; postDownwardObj: {}; orderSeqOfDown: number; orderSeqOfUp: number; showDialog: boolean; closable: boolean; sqlAsDefaultValue: Boolean; showDefaultSQLOption: boolean; verifyFieldValueAs: number; predefinedValueList: any[]; predefinedValue: string; tabId: string; tabName: string; validationType: string; visible: string; orderSeq: number; message: any; groupFormField: boolean; fieldType: string; fieldSQL: string; fieldName: string; fieldId: string; fieldDefaultSQL: string; errorStackTrace: any; errorMessage: any; defaultValue: any; finalPOSTObj: any; mode: string; showSpinner: boolean; showDialog1: boolean; queryObj: {}; validateResponseString: string; showVerifySpinner: boolean; Groups: any; isDefaultSql: boolean; showConfirmButton: boolean; showReOrderButton: boolean; orderSeqArray: any; showDateRange: any; rangeStartDate :any; rangeEndDate :any; rangeStartDateSQL :any; rangeEndDateSQL:any; isDefaultSqlRange:boolean; isFFDate: boolean; constructor(private _formFieldsService: FormFieldsService, public dialog: MatDialog, private _snackBar: MatSnackBar,private changeDetectorRefs: ChangeDetectorRef) { this.formFieldsListObj = []; this.postMoveUpwrdObj = {}; this.postDownwardObj = {}; this.showDialog = false; this.showDefaultSQLOption = false; this.closable = true; this.predefinedValueList = []; this.finalPOSTObj = {}; this.showSpinner = false; this.showDialog1 = false; this.queryObj = {}; this.showVerifySpinner = false; this.Groups = []; this.isDefaultSql = false; this.showReOrderButton = false; this.orderSeqArray = []; this.showDateRange = false; this.isDefaultSqlRange = false; this.rangeStartDate =''; this.rangeEndDate=''; this.rangeStartDateSQL =''; this.rangeEndDateSQL=''; this.isFFDate = false; } ngOnInit() { this._formFieldsService.getListOfFormFields() .subscribe((responseFormFields) => { for (let m = 0; m < responseFormFields.length; m++) { this.formFieldsListObj.push(responseFormFields[m]); } this.formFieldsListObj = this.formFieldsListObj.sort(function (a, b) { if ((a['orderSeq']) === (b['orderSeq'])) { return a; } else { return (a['orderSeq']) - (b['orderSeq']); } }); for (let ff = 0; ff < this.formFieldsListObj.length; ff++) { let val = this.formFieldsListObj[ff]['orderSeq']; this.orderSeqArray.push(val); } if (this.orderSeqArray.includes(null) || this.orderSeqArray.length !== new Set(this.orderSeqArray).size) { this.reOrder(); } this._formFieldsService.getFormFieldGroupsData(this.reportId1) .subscribe((responseGroupsData) => { this.Groups = JSON.parse(responseGroupsData['formFieldGroupsJSON']); this.Groups = this.Groups.sort(function (a, b) { if ((a['orderSeq']) === (b['orderSeq'])) { return a; } else { return (a['orderSeq']) - (b['orderSeq']); } }); }); }); } isLast(n: number) { if (n == this.formFieldsListObj.length - 1) { return true; } else { return false; } } isFirst(n: number) { if (n == 0) { return true; } else { return false; } } moveUpward(n: number) { this.showSpinner = true; this._formFieldsService.getFormFieldData(this.formFieldsListObj[n]['id']) .subscribe((responseGetFormFieldn) => { this.postMoveUpwrdObj = responseGetFormFieldn; this._formFieldsService.getFormFieldData(this.formFieldsListObj[n - 1]['id']) .subscribe((responseGetFormFieldnmin1) => { this.postDownwardObj = responseGetFormFieldnmin1; this.orderSeqOfDown = this.postMoveUpwrdObj['orderSeq']; this.orderSeqOfUp = this.postDownwardObj['orderSeq']; this.postMoveUpwrdObj['orderSeq'] = this.orderSeqOfUp; this.postDownwardObj['orderSeq'] = this.orderSeqOfDown; this._formFieldsService.postFormFieldData(this.postDownwardObj, this.reportId1) .subscribe((responsePostn) => { if (responsePostn['message']) { this._formFieldsService.postFormFieldData(this.postMoveUpwrdObj, this.reportId1) .subscribe((responsePostnmin1) => { if (responsePostnmin1['message']) { this.formFieldsListObj = []; this._formFieldsService.getListOfFormFields() .subscribe((finalListResponse) => { for (let k = 0; k < finalListResponse.length; k++) { // this.formFieldsListObj.push(finalListResponse[k]); for (let m = 0; m < finalListResponse.length; m++) { if (finalListResponse[m]['orderSeq'] == k + 1) { this.formFieldsListObj.push(finalListResponse[m]); } } } this.showSpinner = false; this.updateGroupOnOrderChange(); }); } }); } }); }); }); } updateGroupOnOrderChange() { let formfieldsMap: Map = new Map(); for (let i = 0; i < this.formFieldsListObj.length; i++) { formfieldsMap.set(this.formFieldsListObj[i]['id'], this.formFieldsListObj[i]['orderSeq']); } for (let k = 0; k < this.Groups.length; k++) { let element = this.Groups[k]; for (let l = 0; l < element['formFieldList'].length; l++) { element['formFieldList'][l]['orderSeq'] = formfieldsMap.get(element['formFieldList'][l]['id']); } } this.saveFormFieldGroups(); } moveDownward(n: number) { this.showSpinner = true; this._formFieldsService.getFormFieldData(this.formFieldsListObj[n]['id']) .subscribe((responseGetFormFieldn) => { this.postMoveUpwrdObj = responseGetFormFieldn; this._formFieldsService.getFormFieldData(this.formFieldsListObj[n + 1]['id']) .subscribe((responseGetFormFieldnmin1) => { this.postDownwardObj = responseGetFormFieldnmin1; this.orderSeqOfDown = this.postMoveUpwrdObj['orderSeq']; this.orderSeqOfUp = this.postDownwardObj['orderSeq']; this.postMoveUpwrdObj['orderSeq'] = this.orderSeqOfUp; this.postDownwardObj['orderSeq'] = this.orderSeqOfDown; this._formFieldsService.postFormFieldData(this.postDownwardObj, this.reportId1) .subscribe((responsePostn) => { if (responsePostn['message']) { this._formFieldsService.postFormFieldData(this.postMoveUpwrdObj, this.reportId1) .subscribe((responsePostnmin1) => { if (responsePostnmin1['message']) { this.formFieldsListObj = []; this._formFieldsService.getListOfFormFields() .subscribe((finalListResponse) => { for (let k = 0; k < finalListResponse.length; k++) { for (let m = 0; m < finalListResponse.length; m++) { if (finalListResponse[m]['orderSeq'] == k + 1) { this.formFieldsListObj.push(finalListResponse[m]); } } } this.showSpinner = false; this.updateGroupOnOrderChange(); }); } }); } }); }); }); } ngDoCheck() { if (this.sqlAsDefaultValue) { this.showDefaultSQLOption = true; } else { this.showDefaultSQLOption = false; } } setDisplayMode(displayMode: string) { } editGroup(group: any) { this.openEditDialog(group); } setDate($event : any){ const mnth = ('0' + ($event.getMonth() + 1)).slice(-2); const day = ('0' + $event.getDate()).slice(-2); let temp = [mnth, day, $event.getFullYear()].join('/'); temp = temp+' '+ '00:00:00'; return temp; } edit(id: string) { this._formFieldsService.getFormFieldData(id) .subscribe((responseFormFieldData) => { this.validationType = responseFormFieldData["validationType"]; this.showDateRange = false; if(this.validationType ==='DATE'){ this.showDateRange = true; if (responseFormFieldData['rangeStartDate'] != null) { if (responseFormFieldData['rangeStartDate'] != '') this.rangeStartDate = new Date(responseFormFieldData['rangeStartDate']); else { this.rangeStartDate = ''; } } else { this.rangeStartDate = ''; } if (responseFormFieldData['rangeEndDate'] != null) { if (responseFormFieldData['rangeEndDate'] != '') this.rangeEndDate = new Date(responseFormFieldData['rangeEndDate']); else { this.rangeEndDate = ''; } } else { this.rangeEndDate = ''; } this.rangeStartDateSQL = responseFormFieldData['rangeStartDateSQL']; this.rangeEndDateSQL = responseFormFieldData['rangeEndDateSQL']; if ((this.rangeStartDate == '' && this.rangeEndDate == '')) { this.isDefaultSqlRange = true; } else { this.isDefaultSqlRange = false; } if ((this.rangeStartDate == '' && this.rangeEndDate == '' && this.rangeEndDateSQL == '' && this.rangeStartDateSQL == '')) { this.isDefaultSqlRange = false; } } if (responseFormFieldData['visible'] === true) { this.visible = 'YES'; } else { this.visible = 'NO'; } this.orderSeq = responseFormFieldData['orderSeq']; this.message = responseFormFieldData['message']; this.groupFormField = responseFormFieldData['groupFormField']; this.fieldType = responseFormFieldData['fieldType']; this.fieldSQL = responseFormFieldData['fieldSQL']; this.fieldName = responseFormFieldData['fieldName']; this.fieldId = responseFormFieldData['fieldId']; this.fieldDefaultSQL = responseFormFieldData['fieldDefaultSQL']; this.errorStackTrace = responseFormFieldData['errorStackTrace']; this.errorMessage = responseFormFieldData['errorMessage']; this.defaultValue = responseFormFieldData['defaultValue']; if (this.fieldDefaultSQL != null || this.fieldDefaultSQL != '') { this.isDefaultSql = true; } else { this.isDefaultSql = false; } if (this.defaultValue == null || this.defaultValue == '') { this.isDefaultSql = true; } else { this.isDefaultSql = false; } if ((this.fieldDefaultSQL == null || this.fieldDefaultSQL == '') && (this.defaultValue == null || this.defaultValue == '')) { this.isDefaultSql = false; } if (responseFormFieldData['predefinedValueList'] == null) { this.predefinedValueList = []; } else { this.predefinedValueList = responseFormFieldData['predefinedValueList']; } this.showDialog = true; this.closable = true; this.mode = 'Edit'; }); } add() { this.showDialog = true; this.closable = true; this.validationType = ''; this.visible = 'YES'; this.message = ''; this.groupFormField = false; this.fieldType = ''; this.fieldSQL = ''; this.fieldName = ''; this.fieldId = ''; this.fieldDefaultSQL = ''; this.errorStackTrace = ''; this.errorMessage = ''; this.isDefaultSql = false; this.predefinedValueList = []; this.defaultValue = ''; this.orderSeq = null; this.showDialog = true; this.closable = true; this.isDefaultSqlRange = false; this.rangeStartDate =''; this.rangeEndDate=''; this.rangeStartDateSQL =''; this.rangeEndDateSQL=''; this.showDateRange=''; this.mode = 'Add'; } close() { this.showDialog = !this.showDialog; this.closable = false; } closeValidateModal() { this.showDialog1 = !this.showDialog1; this.showVerifySpinner = false; } addToList(attrib: string) { this.predefinedValueList.push( { 'id': attrib, 'name': attrib, 'selected': false }); } deleteFromList(attrib: string) { for (let k = 0; k < this.predefinedValueList.length; k++) { if (this.predefinedValueList[k]['id'] == attrib) { this.predefinedValueList.splice(k, 1); } } } save() { this.showSpinner = true; this.showDialog = false; this.finalPOSTObj['validationType'] = this.validationType; if (this.validationType === 'DATE') { if (this.rangeStartDate != null && this.rangeStartDate != '') { this.finalPOSTObj['rangeStartDate'] = this.setDate(this.rangeStartDate); } else { this.finalPOSTObj['rangeStartDate'] = ''; } if (this.rangeEndDate != null && this.rangeEndDate != '') { this.finalPOSTObj['rangeEndDate'] = this.setDate(this.rangeEndDate); } else { this.finalPOSTObj['rangeEndDate'] = ''; } this.finalPOSTObj['rangeStartDateSQL'] = this.rangeStartDateSQL; this.finalPOSTObj['rangeEndDateSQL'] = this.rangeEndDateSQL; } if (this.isDefaultSqlRange) { this.finalPOSTObj['rangeStartDate'] = ''; this.finalPOSTObj['rangeEndDate'] = ''; } if (!this.isDefaultSqlRange) { this.finalPOSTObj['rangeStartDateSQL'] = ''; this.finalPOSTObj['rangeEndDateSQL'] = ''; } if (this.visible == 'YES') { this.finalPOSTObj['visible'] = true; } else { this.finalPOSTObj['visible'] = false; } this.finalPOSTObj['orderSeq'] = this.orderSeq; this.finalPOSTObj['message'] = this.message; this.finalPOSTObj['groupFormField'] = this.groupFormField; this.finalPOSTObj['fieldType'] = this.fieldType; this.finalPOSTObj['fieldSQL'] = this.fieldSQL; this.finalPOSTObj['fieldName'] = this.fieldName; this.finalPOSTObj['fieldId'] = this.fieldId; this.finalPOSTObj['errorStackTrace'] = this.errorStackTrace; this.finalPOSTObj['errorMessage'] = this.errorMessage; this.finalPOSTObj['lastUpdatedFieldId'] = this.fieldId; this.finalPOSTObj['fieldDefaultSQL'] = this.fieldDefaultSQL; this.finalPOSTObj['defaultValue'] = this.defaultValue; this.finalPOSTObj['defaultValue'] = this.defaultValue; if (this.fieldDefaultSQL != '' && this.fieldDefaultSQL != null) { this.finalPOSTObj['defaultValue'] = ''; } this.finalPOSTObj['predefinedValueList'] = this.predefinedValueList; if (this.mode == 'Edit') { this._formFieldsService.postFormFieldData(this.finalPOSTObj, this.reportId1) .subscribe((responsePost) => { if (responsePost['message']) { this._formFieldsService.getListOfFormFields() .subscribe((responseFormFields) => { this.formFieldsListObj = []; let m = 0; while (responseFormFields[m]) { this.formFieldsListObj.push(responseFormFields[m]); m++; } this.showSpinner = false; }); } }); } else { this._formFieldsService.addFormFieldData(this.finalPOSTObj, this.reportId1) .subscribe((responsePost) => { if (responsePost['message']) { this._formFieldsService.getListOfFormFields() .subscribe((responseFormFields) => { this.formFieldsListObj = []; let m = 0; while (responseFormFields[m]) { this.formFieldsListObj.push(responseFormFields[m]); m++; } this.showSpinner = false; }); } }); } } delete(id: string) { this.showSpinner = true; this._formFieldsService.deleteFormField(id) .subscribe((responseDeleted) => { if (responseDeleted['message'] == 'Formfield Deleted') { this._formFieldsService.getListOfFormFields() .subscribe((responseFormFields) => { this.formFieldsListObj = []; let m = 0; while (responseFormFields[m]) { this.formFieldsListObj.push(responseFormFields[m]); m++; } this.showSpinner = false; }); } }); } verify(SQLType: string) { this.showVerifySpinner = true; this.showDialog1 = true; if (SQLType == 'Default') { this.queryObj = { query: this.fieldDefaultSQL }; this._formFieldsService.verifySQL(this.queryObj) .subscribe((responseDefaultSQL) => { this.validateResponseString = responseDefaultSQL['data']['elements']; this.showVerifySpinner = false; }); } else if (SQLType == 'startDateRange' || SQLType =='endDateRange'){ if(SQLType == 'startDateRange'){ this.queryObj = { query: this.rangeStartDateSQL }; } if(SQLType == 'endDateRange'){ this.queryObj = { query: this.rangeEndDateSQL }; } this._formFieldsService.verifySQL(this.queryObj) .subscribe((responseDefaultSQL) => { this.validateResponseString = responseDefaultSQL['data']['elements']; this.showVerifySpinner = false; }); } else { this.queryObj = { query: this.fieldSQL }; this._formFieldsService.verifySQL(this.queryObj) .subscribe((responseSQL) => { this.validateResponseString = responseSQL['data']['elements']; this.showVerifySpinner = false; }); } } deleteFromGroup(formFieldId: String) { for (let k = 0; k < this.Groups.length; k++) { let element = this.Groups[k]; for (let l = 0; l < element["formFieldList"].length; l++) { if (element['formFieldList'][l]['id'] == formFieldId) { this.Groups[k]['formFieldList'].splice(l, 1); } } } } deleteGroup(groupName: string) { for (let k = 0; k < this.Groups.length; k++) { if (this.Groups[k]['name'] == groupName) { this.Groups.splice(k, 1); } } } createGroup() { this.openDialog(); } openDialog() { const dialogRef = this.dialog.open(DialogOverviewExampleDialog, { width: '400px', height: '600px', data: this.formFieldsListObj, }); dialogRef.afterClosed().subscribe(result => { let groupAlreadyExists = false; if (this.Groups == null || this.Groups == undefined) { this.Groups = []; } for (let k = 0; k < this.Groups.length; k++) { if (this.Groups[k]['name'] == result['name']) { groupAlreadyExists = true; } } if (!groupAlreadyExists) { let UpdatedResult = result; for (let i = 0; i < result["formFieldList"].length; i++) { let formFieldId = result['formFieldList'][i]; let j = 0; while (this.formFieldsListObj[j]) { if (this.formFieldsListObj[j]['id'] == formFieldId) { UpdatedResult['formFieldList'][i] = this.formFieldsListObj[j]; } j++; } } if (this.Groups !== null || this.Groups !== undefined) { this.Groups.push(UpdatedResult); } else { this.Groups = []; this.Groups.push(UpdatedResult); } } else { this.openSnackBar('Group Already Exists.', 'Try using another name!'); } }); } reOrder() { this._formFieldsService.reOrderFFValues() .subscribe((response) => { console.log(response); this.formFieldsListObj = []; for (let k = 0; k < response.length; k++) { for (let m = 0; m < response.length; m++) { if (response[m]['orderSeq'] == k + 1) { this.formFieldsListObj.push(response[m]); } } } this.showSpinner = false; this.showReOrderButton = false; this.updateGroupOnOrderChange(); }); } openEditDialog(groupInfo: any) { const multiSelectArray = []; for (let arr = 0; arr < groupInfo['formFieldList'].length; arr++) { multiSelectArray.push(groupInfo['formFieldList'][arr]['id']); } let editGroupInfo = {}; editGroupInfo['formFieldList'] = multiSelectArray; editGroupInfo['name'] = groupInfo['name']; const dialogRef = this.dialog.open(EditGroupDialog, { width: '400px', height: '600px', data: { formFieldsListObj: this.formFieldsListObj, editGroupInfo: editGroupInfo } }); dialogRef.afterClosed().subscribe(result => { for (let k = 0; k < this.Groups.length; k++) { if (this.Groups[k]['name'] == editGroupInfo['name']) { let UpdatedResult = editGroupInfo; for (let i = 0; i < editGroupInfo["formFieldList"].length; i++) { let formFieldId = editGroupInfo['formFieldList'][i]; let j = 0; while (this.formFieldsListObj[j]) { if (this.formFieldsListObj[j]['id'] == formFieldId) { UpdatedResult['formFieldList'][i] = this.formFieldsListObj[j]; } j++; } } this.Groups[k]['formFieldList'] = UpdatedResult['formFieldList']; } } }); } openSnackBar(message: string, action: string) { this._snackBar.open(message, action, { duration: 5000, }); } saveFormFieldGroups() { let formFieldGroupsPostObj = { reportId: this.reportId1, formFieldGroupsJSON: JSON.stringify(this.Groups) }; this._formFieldsService.postFormFieldGroupsData(formFieldGroupsPostObj) .subscribe((responsePost) => { this.openSnackBar('Successfully Saved!', ''); }); } } @Component({ selector: 'app-create-group', templateUrl: './create-group.component.html', }) export class DialogOverviewExampleDialog { createGroupObj: any = {}; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: DialogData) { } onNoClick(): void { this.dialogRef.close(); } } @Component({ selector: 'app-edit-group', templateUrl: './edit-group.component.html', }) export class EditGroupDialog { @Input('mode') mode: string; editGroupInfo: any = {}; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: DialogData) { } onNoClick(): void { this.dialogRef.close(); } }