summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-app-overlay/src/main/webapp/ngapp/src/app/pages/analytics/Report_List/Report/run/run-report-form-fields/run-report-form-fields.component.ts
blob: c0856b44e3aab1b836fd634637c538dc26853f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
import {Component, OnInit, Input, SimpleChange, SystemJsNgModuleLoader, OnDestroy} from '@angular/core';
import {RunService} from '../run.service';
import {ActivatedRoute, Router} from '@angular/router';
import {inlineInterpolate} from '@angular/core/src/view';
import {copyStyles} from '@angular/animations/browser/src/util';
import {split} from 'ts-node';
import * as cloneDeep from 'lodash/cloneDeep';

@Component({
    selector: 'app-run-report-form-fields',
    templateUrl: './run-report-form-fields.component.html',
    styleUrls: ['./run-report-form-fields.component.css']
})
export class RunReportFormFieldsComponent implements OnInit, OnDestroy {
    @Input('formFieldList') formFieldList: {}[];
    @Input('reportId') reportId: string;
    staticFormFieldList: {}[] = [];
    formFieldListValueArr: any[];
    saveFormFieldGroups: any[];
    finalQueryParamsObj: {};
    navigateToRun: boolean;
    reportMode: string;
    queryString: string;
    reportName: string;
    reportSubTitle: string;
    showSpinner: boolean;
    showLabel: boolean;
    runDashboardReport: boolean;
    DashboardReportObj: {}[] = [];
    triggerFormFieldArr = [];
    initialObject = {};
    formFieldGroupObjList: {}[] = [];
    toggleFormFieldRenderArr: {}[] = [];
    groupSelectValue = '';
    oldGroupSelectValue = '';
    unCommonCnt = 0;
    commonCount = 0;
    totalCommonCount = 0;
    totalCount = 0;
    errorMessage = '';
    stackTrace = '';
    error = false;
    iSDashboardReport = '';
    hitCnt = 0;
    directCallQueryParams: any = '';
    calledWithFormFields = false;
    showformFiledSpinner = false;
    actualformFieldValues: any[];
    allowEdit: boolean;
    runReportAgain: boolean;
    tempFieldValues = [];
    firstRun = false;
    saveFormFieldListValueArr: any[];
    saveGroupSelectValue = '';
    avoidDoCheck = false;
    toolTipPosition = 'right';
    isResetAllowed: boolean;
    save: {}[];
    unCommonGropusList: any[];
    commonFormFields: any[];
    formFieldListValueMap: any;
    saveFormFieldListValueMap: any;
    chartType = '';

    constructor(private _runService: RunService, private _route: ActivatedRoute, private _router: Router) {
        this.formFieldListValueArr = [];
        this.saveFormFieldListValueArr = [];
        this.saveFormFieldGroups = [];
        this.finalQueryParamsObj = {};
        this.navigateToRun = false;
        this.queryString = '';
        this.showSpinner = false;
        this.showLabel = false;
        this.runDashboardReport = false;
        this.showformFiledSpinner = false;
        this.runReportAgain = false;
        this.tempFieldValues = [];
        this.isResetAllowed = false;
        this.unCommonGropusList = [];
        this.commonFormFields = [];
        this.formFieldListValueMap = new Map<any, any>();
        this.saveFormFieldListValueMap = new Map<any, any>();
        this.chartType = '';
    }

    ngOnDestroy(): void {
        this.createNewObject();
        this.showSpinner = false;
    }

    createNewObject() {
        this.staticFormFieldList = [];
        this.formFieldListValueArr = [];
        this.saveFormFieldGroups = [];
        this.saveFormFieldListValueArr = [];
        this.finalQueryParamsObj = {};
        this.navigateToRun = false;
        this.reportMode = '';
        this.queryString = '';
        this.reportName = '';
        this.reportSubTitle='';
        this.showSpinner = true;
        this.showLabel = false;
        this.runDashboardReport = false;
        this.DashboardReportObj = [];
        this.triggerFormFieldArr = [];
        this.initialObject = {};
        this.formFieldGroupObjList = [];
        this.toggleFormFieldRenderArr = [];
        this.groupSelectValue = '';
        this.oldGroupSelectValue = '';
        this.unCommonCnt = 0;
        this.totalCommonCount = 0;
        this.commonCount = 0;
        this.totalCount = 0;
        this.errorMessage = '';
        this.stackTrace = '';
        this.error = false;
        this.iSDashboardReport = '';
        this.hitCnt = 0;
        this.directCallQueryParams = '';
        this.calledWithFormFields = false;
        this.showformFiledSpinner = false;
        this.allowEdit = false;
        this.runReportAgain = false;
        this.isResetAllowed = false;
        this.unCommonGropusList = [];
        this.commonFormFields = [];
        this.formFieldListValueMap = new Map<any, any>();
        this.saveFormFieldListValueMap = new Map<any, any>();
        this.chartType = '';
    }

    ngOnInit() {

        this.showSpinner = true;
        this.navigateToRun = false;
        this._route.params.subscribe(params => {
            this.reportId = params['reportId'];
            this.createNewObject();
            this.groupSelectValue = params['groupSelectValue'];
            if (this.groupSelectValue === undefined) {
                this.groupSelectValue = '';
            }
            if (params['queryParameters']) {
                this.directCallQueryParams = params['queryParameters'];
                this.calledWithFormFields = true;
                this.populateQueryParams(params['queryParameters']);
            }
            this.loadPage();
        });

    }

    populateQueryParams(queryParams: any) {
        this.actualformFieldValues = this.directCallQueryParams.substring(1, this.directCallQueryParams.length).split('&');
        for (const ff of this.actualformFieldValues) {
            const formfiledArray = ff.split('=');
            const formFieldId = formfiledArray[0];
            const formFieldObj = formfiledArray[1];
            this.finalQueryParamsObj[formFieldId] = formFieldObj;
        }
        this.queryString = this.directCallQueryParams;
    }

    loadPage() {
        this._runService.getDefinitionPageDetails(+this.reportId)
            .subscribe((responseDefPage) => {
                this.reportName = responseDefPage['reportName'];
                this.reportSubTitle = responseDefPage['reportSubTitle'];
                this.chartType = responseDefPage['chartType'];
                if (responseDefPage['reportType'] !== 'Dashboard') {
                    if (this.calledWithFormFields == false) {
                        this._runService.getReportData(this.reportId)
                            .subscribe((response) => {
                                if (response['errormessage']) {
                                    this.allowEdit = response['allowEdit'];
                                    this.showError(response);
                                } else {
                                    this.allowEdit = response['allowEdit'];
                                    this.reportName = response['reportName'];
                                    if (response['formFieldList'].length > 0) {
                                        this.fetchAndPopulateFormFields(response, this.reportId);
                                        this.showformFiledSpinner = true;
                                    } else {
                                        this.formFieldList = response['formFieldList'];
                                        this.reportMode = 'Regular';
                                        this.navigateToRun = true;
                                        this.showformFiledSpinner = false;

                                    }
                                    this.showSpinner = false;
                                }
                            });
                    } else if (this.calledWithFormFields == true) {
                        this._runService.getReportData(this.reportId)
                            .subscribe((response) => {
                                if (response['errormessage']) {
                                    this.showError(response);
                                } else {
                                    this.error = false;
                                    this.reportName = response['reportName'];
                                    this.allowEdit = response['allowEdit'];
                                    if (response['formFieldList'].length > 0) {
                                        this.setDefaultFieldGroupValueForNonSelected(response['formFieldList']);
                                        this.generateQueryString();
                                        this.fetchAndPopulateFormFields(response, this.reportId);
                                        this.formFieldListValueMap = new Map<any, any>();
                                        for (const ff of response['formFieldList']) {
                                            if (this.finalQueryParamsObj[ff.fieldId]) {
                                                if (ff.validationType == 'DATE') {
                                                    const dateVal = this.finalQueryParamsObj[ff.fieldId];
                                                    this.formFieldListValueMap.set(ff.fieldId, new Date(dateVal.toString().replace(/%2F/g, '/')));
                                                } else if (ff.fieldType == 'LIST_MULTI_SELECT') {
                                                    const multiSelectArray = [];
                                                    let multiVal = this.finalQueryParamsObj[ff.fieldId];
                                                    multiVal = multiVal.toString().replace(/%2F/g, '/');
                                                    multiVal = multiVal.toString().replace('+', ' ');
                                                    multiSelectArray.push(multiVal);
                                                    this.formFieldListValueMap.set(ff.fieldId, multiSelectArray);
                                                } else {
                                                    let multiVal = this.finalQueryParamsObj[ff.fieldId];
                                                    multiVal = multiVal.toString().replace(/%2F/g, '/');
                                                    multiVal = multiVal.toString().split('+').join(' ');
                                                    this.formFieldListValueMap.set(ff.fieldId, multiVal);
                                                }
                                            } else {
                                                if (ff.fieldType == 'LIST_MULTI_SELECT') {
                                                    const multiSelectArray = [];
                                                    this.formFieldListValueMap.set(ff.fieldId, multiSelectArray);
                                                } else {
                                                    this.formFieldListValueMap.set(ff.fieldId, '');
                                                }
                                            }
                                        }
                                        this.error = false;
                                        this.navigateToRun = true;
                                        this.showformFiledSpinner = true;
                                        this.directCallQueryParams = '';
                                    } else {
                                        this.reportMode = 'Regular';
                                        this.navigateToRun = true;
                                        this.showformFiledSpinner = false;

                                    }
                                    this.showSpinner = false;

                                }
                            });
                    } else {
                        this.reportMode = 'FormField';
                        this.navigateToRun = true;
                        this.showSpinner = false;
                        this.showformFiledSpinner = true;
                    }
                } else {
                    if (this.calledWithFormFields == false) {
                        this.iSDashboardReport = 'Dashboard';
                        this.reportName = responseDefPage['reportName'];
                        this.DashboardReportObj = JSON.parse(responseDefPage['dashboardLayoutJSON']);
                        let i = 0;
                        let subReportId = '';
                        const tempDashboardArray = [];
                        for (let dash = 0; dash < this.DashboardReportObj.length ; dash++) {
                            if (this.DashboardReportObj[dash]['hasContent']['hideDisplay'] !== true) {
                                tempDashboardArray.push(this.DashboardReportObj[dash]);
                            }
                        }
                        this.DashboardReportObj = [];
                        this.DashboardReportObj = tempDashboardArray;
                        while (this.DashboardReportObj[i]) {
                            subReportId = this.DashboardReportObj[i]['hasContent']['id'].split('#')[1];
                            i++;
                            break;
                        }
                        this._runService.getReportData(subReportId)
                            .subscribe((response) => {
                                if (response['errormessage']) {
                                    this.showError(response);
                                    this.allowEdit = response['allowEdit'];
                                } else {
                                    this.allowEdit = response['allowEdit'];
                                    if (response['formFieldList'].length > 0) {
                                        this.fetchAndPopulateFormFields(response, subReportId);
                                    } else {
                                        this.navigateToRun = true;
                                    }
                                    this.runDashboardReport = true;
                                    this.showSpinner = false;
                                    this.showformFiledSpinner = true;
                                }
                            });
                        this.showSpinner = false;
                        this.showformFiledSpinner = true;
                    } else if (this.calledWithFormFields == true) {
                        this.iSDashboardReport = 'Dashboard';
                        this.reportName = responseDefPage['reportName'];
                        this.DashboardReportObj = JSON.parse(responseDefPage['dashboardLayoutJSON']);
                        const tempDashboardArray = [];
                        for (let dash = 0; dash < this.DashboardReportObj.length ; dash++) {
                            if (this.DashboardReportObj[dash]['hasContent']['hideDisplay'] !== true) {
                                tempDashboardArray.push(this.DashboardReportObj[dash]);
                            }
                        }
                        this.DashboardReportObj = [];
                        this.DashboardReportObj = tempDashboardArray;
                        let subReportId = '';
                        for (const dashboard of this.DashboardReportObj) {
                            let temp = '';
                            temp = dashboard['hasContent']['id'].split('#')[1];
                            subReportId = temp;
                            this.directCallQueryParams = '';
                            this.runDashboardReport = true;
                            this.navigateToRun = true;
                            this.showSpinner = false;
                        }
                    }
                }
            });
    }

    toggleChangeWhenCalledWithFromFields(){
        this.calledWithFormFields = false;
    }

    async ngDoCheck() {

        if (this.formFieldList !== undefined) {

            if (this.groupSelectValue !== this.oldGroupSelectValue) {
                //  this.formFieldListValueArr.splice(this.commonCount-1, this.formFieldListValueArr.length)
                this.removePrevioustoggleGroupData();
                this.oldGroupSelectValue = this.groupSelectValue;
                this.formFieldGroupObjList = [];
                this.saveFormFieldGroups = [];
                this.formFieldGroupObjList = cloneDeep(this.save);
                this.saveFormFieldGroups = cloneDeep(this.save);
                for (const formFieldGroupObjItem of this.formFieldGroupObjList) {
                    if (formFieldGroupObjItem['name'] == this.groupSelectValue) {
                        this.toggleFormFieldRenderArr = formFieldGroupObjItem['formFieldList'];
                    }

                }
                if (this.toggleFormFieldRenderArr.length > 0) {
                    for (let i = 0; i < this.toggleFormFieldRenderArr.length; i++) {
                        const formFieldObj = this.toggleFormFieldRenderArr[i];
                        if (formFieldObj['triggerOtherFormFields'] === true) {
                            const formFieldId = formFieldObj['fieldId'];
                            this.triggerFormFieldArr.push(formFieldId);
                            this.initialObject[formFieldId] = '1';
                            this.finalQueryParamsObj[formFieldId] = '1';
                        }
                    }
                }

                if(this.calledWithFormFields != true){
                    for (const formFieldGroupObjItem of this.saveFormFieldGroups) {
                        if (formFieldGroupObjItem['name'] == this.groupSelectValue) {
                            this.toggleFormFieldRenderArr = formFieldGroupObjItem['formFieldList'];
                            for (let ffGrpValue = 0; ffGrpValue < formFieldGroupObjItem['formFieldList'].length; ffGrpValue++) {
                                if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length > 0) {
                                    for (let ffValue = 0; ffValue < formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length; ffValue++) {
                                        if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldType'] == 'LIST_BOX' && formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length > 0) {
                                            let isAdded = false;
                                            for (let ffValue = 0; ffValue < formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length; ffValue++) {
                                                if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['defaultValue'] == true) {
                                                    this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['id']);
                                                    isAdded = true;
                                                }
                                            }
                                            if (!isAdded) {
                                                this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], '');
                                            }
                                        } else if ((formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldType'] == 'LIST_MULTI_SELECT' || formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldType'] == 'TEXT') && formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length > 0) {
                                            let isAdded = false;
                                            for (let ffValue = 0; ffValue < formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length; ffValue++) {

                                                if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['defaultValue'] == true) {
                                                    const multiSelectArray = [];
                                                    multiSelectArray.push(formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['id']);
                                                    this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], multiSelectArray);
                                                    isAdded = true;
                                                }
                                            }
                                            if (!isAdded) {
                                                this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], '');
                                            }
                                        } else if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldType'] == 'Select Field Type' && formFieldGroupObjItem['formFieldList'][ffGrpValue]['validationType'] == 'NONE') {
                                            let isAdded = false;
                                            for (let ffValue = 0; ffValue < formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length; ffValue++) {

                                                if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['defaultValue'] == true) {
                                                    this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['id']);
                                                    isAdded = true;
                                                }
                                            }
                                            if (!isAdded) {
                                                this.formFieldListValueArr[this.totalCommonCount + ffGrpValue] = '';
                                            }
                                        } else {
                                            this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], '');
                                        }
                                    }
                                } else {
                                    this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], '');
                                }
                            }
                        }
                    }
                }
                if (this.formFieldGroupObjList.length > 0) {
                    for (let ffl = 0; ffl < this.formFieldList.length; ffl++) {
                        for (let ffgl = 0; ffgl < this.formFieldGroupObjList.length; ffgl++) {
                            const inList = this.formFieldGroupObjList[ffgl]['formFieldList'];

                            for (let inl = 0; inl < inList.length; inl++) {
                                let cntr = 0;
                                if (inList[inl]['fieldId'] == this.formFieldList[ffl]['fieldId']) {
                                    cntr++;
                                }
                                if (cntr == 0) {
                                    this.finalQueryParamsObj[inList[inl]['fieldId']] = '-1';
                                }
                            }

                        }
                    }
                }
            }
            for (const tffr of this.toggleFormFieldRenderArr) {
                if (this.formFieldListValueMap.get(tffr['fieldId']) || this.formFieldListValueMap.get(tffr['fieldId']) == "") {
                    if (tffr['validationType'] == 'DATE') {
                        this.finalQueryParamsObj[tffr['fieldId']] = this.convertDate(this.formFieldListValueMap.get(tffr['fieldId']));
                    } else if (this.formFieldListValueMap.get(tffr['fieldId']) == "") {

                        if (tffr['fieldType'] == 'LIST_MULTI_SELECT') {
                            let multiSelectValue = '';
                            for (let ffValue = 0; ffValue < tffr['formFieldValues'].length; ffValue++) {
                                multiSelectValue = multiSelectValue + tffr['formFieldValues'][ffValue]['id'] + '|';
                            }
                            if (multiSelectValue != '') {
                                multiSelectValue = multiSelectValue.substring(0, multiSelectValue.length - 1);
                            }
                            this.finalQueryParamsObj[tffr['fieldId']] = multiSelectValue;

                        } else {
                            this.finalQueryParamsObj[tffr['fieldId']] = '';
                        }
                    } else {
                        this.finalQueryParamsObj[tffr['fieldId']] = this.formFieldListValueMap.get(tffr['fieldId']);
                        for (const trigElement of this.triggerFormFieldArr) {
                            if (this.initialObject[trigElement] !== this.finalQueryParamsObj[trigElement]) {
                                this.initialObject[trigElement] = this.finalQueryParamsObj[trigElement];
                                this.generateQueryString();
                                await this.delay(1000);
                                console.log('Afterp:  ' + new Date().toString());
                                this._runService.refreshFormFields(this.reportId, this.queryString)
                                    .subscribe((responseRefreshFF) => {
                                        for (let rrff = 0; rrff < responseRefreshFF['formFieldList'].length; rrff++) {
                                            for (let innerTFRR = 0; innerTFRR < this.toggleFormFieldRenderArr.length; innerTFRR++) {
                                                if (responseRefreshFF['formFieldList'][rrff]['fieldId'] == this.toggleFormFieldRenderArr[innerTFRR]['fieldId']) {
                                                    this.toggleFormFieldRenderArr[innerTFRR] = responseRefreshFF['formFieldList'][rrff];
                                                }
                                            }
                                        }
                                    });
                                this.initialObject[trigElement] = this.finalQueryParamsObj[trigElement];
                            }
                        }

                    }
                }
            }

            if (this.reportMode !== 'Regular' && this.reportMode !== 'Dashboard') {
                for (const ffvalue of this.formFieldList) {
                    if (this.formFieldListValueMap.get(ffvalue['fieldId'])) {
                        if (ffvalue['validationType'] == 'DATE') {
                            this.finalQueryParamsObj[ffvalue['fieldId']] = this.convertDate(this.formFieldListValueMap.get(ffvalue['fieldId']));
                        } else {
                            this.finalQueryParamsObj[ffvalue['fieldId']] = this.formFieldListValueMap.get(ffvalue['fieldId']);
                            for (const trigElement of this.triggerFormFieldArr) {
                                if (this.initialObject[trigElement] !== this.finalQueryParamsObj[trigElement]) {
                                    this.initialObject[trigElement] = this.finalQueryParamsObj[trigElement];
                                    this.generateQueryString();
                                    await this.delay(1000);
                                    console.log('Afterp:  ' + new Date().toString());
                                    this._runService.refreshFormFields(this.reportId, this.queryString)
                                        .subscribe((responseRefreshFF) => {

                                            for (let rrff = 0; rrff < responseRefreshFF['formFieldList'].length; rrff++) {
                                                for (let innerFFL = 0; innerFFL < this.formFieldList.length; innerFFL++) {
                                                    if (responseRefreshFF['formFieldList'][rrff]['fieldId'] == this.formFieldList[innerFFL]['fieldId']) {
                                                        this.formFieldList[innerFFL] = responseRefreshFF['formFieldList'][rrff];
                                                    }
                                                }
                                            }
                                        });
                                }
                            }

                        }
                    }
                }

            }
        }
        if (this.firstRun) {
            this.saveGroupSelectValue = this.groupSelectValue;
            this.saveFormFieldListValueMap = cloneDeep(this.formFieldListValueMap);
        }
        this.firstRun = false;
    }

    convertDate(str) {
        const date = new Date(str),
            mnth = ('0' + (date.getMonth() + 1)).slice(-2),
            day = ('0' + date.getDate()).slice(-2);
        return [mnth, day, date.getFullYear()].join('/');
    }

    getQueryString() {
        if (this.directCallQueryParams !== '') {
            return this.directCallQueryParams;
        } else {
            return this.queryString;
        }
    }

    fetchAndPopulateFormFields(respObj: any, ffReportId: string) {
        this._runService.getFormFieldGroupsData(ffReportId)
            .subscribe((responseFormFieldGroups) => {
                this.firstRun = true;
                this.showformFiledSpinner = false;
                this.formFieldGroupObjList = JSON.parse(responseFormFieldGroups['formFieldGroupsJSON']);
                this.saveFormFieldGroups = JSON.parse(responseFormFieldGroups['formFieldGroupsJSON']);
                this.formFieldList = respObj['formFieldList'];
                this.staticFormFieldList = respObj['formFieldList'];
                this.reportMode = 'FormField';
                this.totalCount = this.formFieldList.length;
                if (this.formFieldGroupObjList !== null) {
                    for (let ffgl = 0; ffgl < this.formFieldGroupObjList.length; ffgl++) {
                        for (let itemFFGL = 0; itemFFGL < this.formFieldGroupObjList[ffgl]['formFieldList'].length; itemFFGL++) {
                            const formFieldGroupItem = this.formFieldGroupObjList[ffgl]['formFieldList'][itemFFGL];
                            for (let fflg = 0; fflg < this.formFieldList.length; fflg++) {
                                if (formFieldGroupItem['id'] == this.formFieldList[fflg]['fieldId']) {
                                    this.formFieldGroupObjList[ffgl]['formFieldList'][itemFFGL] = this.formFieldList[fflg];
                                    this.saveFormFieldGroups[ffgl]['formFieldList'][itemFFGL] = this.formFieldList[fflg];

                                    if (this.formFieldList[fflg]['triggerOtherFormFields'] === true) {
                                        const formFieldId = this.formFieldList[fflg]['fieldId'];
                                        this.triggerFormFieldArr.push(formFieldId);
                                        this.initialObject[formFieldId] = [];
                                        this.finalQueryParamsObj[formFieldId] = [];
                                    }
                                    this.formFieldList.splice(fflg, 1);
                                    this.unCommonGropusList.push(formFieldGroupItem);
                                    this.unCommonCnt++;
                                }
                            }
                        }
                    }
                }
                if(this.calledWithFormFields != true){
                    for (let checkRadio = 0; checkRadio < this.formFieldList.length; checkRadio++) {
                        if (this.formFieldList[checkRadio]['fieldDisplayName'] == 'selectCriteria') {
                            for (let ffValue = 0; ffValue < this.formFieldList[checkRadio]['formFieldValues'].length; ffValue++) {
                                if (this.formFieldList[checkRadio]['formFieldValues'][ffValue]['defaultValue'] == true) {
                                    this.groupSelectValue = this.formFieldList[checkRadio]['formFieldValues'][ffValue]['name'];
                                }
                            }
                            this.oldGroupSelectValue = this.groupSelectValue;
                        }
                    }
                }
                this.commonCount = this.totalCount - this.unCommonCnt;
                this.totalCommonCount = this.commonCount;
                console.log(this.unCommonGropusList);
                console.log(this.formFieldList);
                for (let i = 0; i < this.formFieldList.length; i++) {
                    const formFieldObj = this.formFieldList[i];
                    if (formFieldObj['formFieldValues'] != null && this.calledWithFormFields != true) {
                        if (formFieldObj['validationType'] == 'DATE' && formFieldObj['formFieldValues'].length > 0) {
                            const date = formFieldObj['formFieldValues'][0]['id'];
                            this.formFieldListValueMap.set(formFieldObj['fieldId'], new Date(date));
                        } else if (formFieldObj['fieldType'] == 'LIST_BOX' && formFieldObj['formFieldValues'].length > 0) {
                            let isAdded = false;
                            for (let ffValue = 0; ffValue < formFieldObj['formFieldValues'].length; ffValue++) {
                                if (formFieldObj['formFieldValues'][ffValue]['defaultValue'] == true) {
                                    this.formFieldListValueMap.set(formFieldObj['fieldId'], formFieldObj['formFieldValues'][ffValue]['id']);
                                    isAdded = true;
                                }
                            }
                            if (!isAdded) {
                                this.formFieldListValueMap.set(formFieldObj['fieldId'], '');
                            }
                        } else if ((formFieldObj['fieldType'] == 'LIST_MULTI_SELECT' || formFieldObj['fieldType'] == 'TEXT') && formFieldObj['formFieldValues'].length > 0) {
                            let isAdded = false;
                            for (let ffValue = 0; ffValue < formFieldObj['formFieldValues'].length; ffValue++) {

                                if (formFieldObj['formFieldValues'][ffValue]['defaultValue'] == true) {
                                    const multiSelectArray = [];
                                    multiSelectArray.push(formFieldObj['formFieldValues'][ffValue]['id']);
                                    this.formFieldListValueMap.set(formFieldObj['fieldId'], multiSelectArray);
                                    isAdded = true;
                                }
                            }
                            if (!isAdded) {
                                this.formFieldListValueMap.set(formFieldObj['fieldId'], '');
                            }
                        } else if (formFieldObj['fieldDisplayName'] == 'DefaultRadio') {
                            this.formFieldListValueMap.set(formFieldObj['fieldId'], '');

                        } else {
                            this.formFieldListValueMap.set(formFieldObj['fieldId'], '');
                        }
                    }

                    if (formFieldObj['triggerOtherFormFields'] === true) {
                        const formFieldId = formFieldObj['fieldId'];
                        this.triggerFormFieldArr.push(formFieldId);
                        this.initialObject[formFieldId] = [];
                        this.finalQueryParamsObj[formFieldId] = [];
                    }
                }

                console.log(this.formFieldListValueMap);

                if(this.calledWithFormFields != true){
                    if (this.formFieldGroupObjList !== null) {
                        for (const formFieldGroupObjItem of this.formFieldGroupObjList) {
                            if (formFieldGroupObjItem['name'] == this.groupSelectValue) {
                                this.toggleFormFieldRenderArr = formFieldGroupObjItem['formFieldList'];
                                for (let ffGrpValue = 0; ffGrpValue < formFieldGroupObjItem['formFieldList'].length; ffGrpValue++) {
                                    if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length > 0) {
                                        for (let ffValue = 0; ffValue < formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'].length; ffValue++) {
                                            if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['defaultValue'] == true) {
                                                if (formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldType'] == 'LIST_MULTI_SELECT') {
                                                    const multiSelectArray = [];
                                                    multiSelectArray.push(formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['id']);
                                                    this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], multiSelectArray);
                                                } else {
                                                    this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], formFieldGroupObjItem['formFieldList'][ffGrpValue]['formFieldValues'][ffValue]['id']);
                                                }
                                            }
                                        }
                                    } else {
                                        this.formFieldListValueMap.set(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId'], '');

                                    }
                                }
                            }

                        }
                    }
                }
                console.log('finalMap', this.formFieldListValueMap);
                if (this.formFieldGroupObjList !== null) {
                    if (this.formFieldGroupObjList.length > 0) {
                        for (let ffl = 0; ffl < this.formFieldList.length; ffl++) {
                            for (let ffgl = 0; ffgl < this.formFieldGroupObjList.length; ffgl++) {
                                const inList = this.formFieldGroupObjList[ffgl]['formFieldList'];

                                for (let inl = 0; inl < inList.length; inl++) {
                                    let cntr = 0;
                                    if (inList[inl]['fieldId'] == this.formFieldList[ffl]['fieldId']) {
                                        cntr++;
                                    }
                                    if (cntr == 0) {
                                        this.finalQueryParamsObj[inList[inl]['fieldId']] = '-1';
                                    }
                                }

                            }
                        }
                    }
                }
                this.save = cloneDeep(this.formFieldGroupObjList);
            });
    }

    generateQueryString() {

        this.queryString = '';
        for (let k = 0; k < Object.keys(this.finalQueryParamsObj).length; k++) {
            if (typeof (this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]]) == 'object') {
                const key = Object.keys(this.finalQueryParamsObj)[k];
                let qstr = '';
                let l = 0;
                while (this.finalQueryParamsObj[key][l]) {
                    if (l === 0) {
                        qstr = qstr + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]][l];
                    } else {
                        qstr = qstr + '|' + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]][l];
                    }
                    l++;
                }
                if (qstr !== '') {
                    this.queryString = this.queryString + '&' + Object.keys(this.finalQueryParamsObj)[k] + '=' + qstr;
                }
            } else {
                if (typeof (this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]]) == 'string') {
                        this.queryString = this.queryString + '&' + Object.keys(this.finalQueryParamsObj)[k] + '=' + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]];
                } else {
                    this.queryString = this.queryString + '&' + Object.keys(this.finalQueryParamsObj)[k] + '=' + this.finalQueryParamsObj[Object.keys(this.finalQueryParamsObj)[k]];
                }
            }

        }

    }

    showError(Errresponse: any) {
        this.errorMessage = Errresponse['errormessage'];
        this.stackTrace = Errresponse['stacktrace'];
        this.error = true;
        this.showSpinner = false;
    }

    runReport() {
        this.hitCnt++;
        this.showSpinner = true;
        if (this.iSDashboardReport !== 'Dashboard') {
            if (this.formFieldList.length > 0) {
                this.reportMode = 'FormField';
                this.generateQueryString();
            } else {
                this.reportMode = 'Regular';
            }
            this.showSpinner = false;
        } else {
            this.generateQueryString();
            this.showSpinner = false;
        }
        this.runReportAgain = !this.runReportAgain;
        this.navigateToRun = true;
    }

    editReport(reportId: string) {
        this._router.navigate(['v2/app/reports', 'Edit', reportId]);
    }

    showLabelFn() {
        this.showLabel = !this.showLabel;
    }

    setDefaultFieldGroupValueForNonSelected(formFieldsList: any) {
        this.tempFieldValues = [];
        const map = new Map<string, string>();
        for (const value in this.finalQueryParamsObj) {
            map.set(value, this.finalQueryParamsObj[value]);
        }
        for (const ffl of formFieldsList) {
            if (!map.get(ffl['fieldId']) && ffl['formFieldValues'].length === 1) {
                this.tempFieldValues = ffl['formFieldValues'];
                if (ffl['fieldDisplayName'] == this.tempFieldValues[0]['name']) {
                    this.finalQueryParamsObj[ffl['fieldId']] = '-1';
                }
            }
        }
    }

    resetFormFieldValues() {
        sessionStorage.clear();
        const myItem = localStorage.getItem('id');
        localStorage.clear();
        localStorage.setItem('id', myItem);
        if (this.formFieldGroupObjList != null) {
            this._router.navigateByUrl('v2/app/refresh', {skipLocationChange: true}).then(() =>
                this._router.navigate(['v2/app/run', this.reportId]));
        } else {
            this.avoidDoCheck = true;
            this.formFieldListValueMap = new Map<any, any>();
            this.finalQueryParamsObj = [];
            if (this.calledWithFormFields == false) {
                this.formFieldListValueMap = cloneDeep(this.saveFormFieldListValueMap);
            }

        }
    }

    private delay(ms: number) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    private removePrevioustoggleGroupData() {
        for (const formFieldGroupObjItem of this.formFieldGroupObjList) {
            if (formFieldGroupObjItem['name'] == this.oldGroupSelectValue) {
                for (let ffGrpValue = 0; ffGrpValue < formFieldGroupObjItem['formFieldList'].length; ffGrpValue++) {
                    this.finalQueryParamsObj[formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId']] = '-1';
                    this.formFieldListValueMap.delete(formFieldGroupObjItem['formFieldList'][ffGrpValue]['fieldId']);
                    this.triggerFormFieldArr= [];
                }

            }
        }
    }
}