1a6b95e24150c7ecea1219cefcfdd25fce19ddc4
[usecase-ui.git] /
1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2 import { IntentManagementService } from '../../../core/services/intentManagement.service';
3 import { Util } from '../../../shared/utils/utils';
4
5 @Component({
6   selector: 'app-input-intent-management',
7   templateUrl: './input-intent-management.component.html',
8   styleUrls: ['../intent-management.component.less']
9 })
10 export class InputIntentManagementComponent implements OnInit {
11
12   constructor(
13     private myhttp: IntentManagementService,
14     private Util: Util
15   ) { }
16   defaultParams:Object={
17     intentId:'',
18     intentName:'',
19     intentExpectations:[
20       {
21         expectationId:'',
22         expectationName:'',
23         expectationType:'',
24         expectationObject:{
25           objectType:'',
26           objectInstance:''
27         },
28         expectationTargets:[
29           {
30             targetId:'',
31             targetName:'',
32             targetCondition:{
33               conditionId:'',
34               conditionName:'',
35               operator:'',
36               conditionValue:'',
37               conditionList:null
38             }
39           }
40         ]
41       }
42     ],
43   };
44   currentIndex:number = -1;
45   intentExpectationShow: boolean = false;
46   editExpectationTableList: Object={};
47
48   listOfData: any[] = [];
49
50   @Input() showModel: boolean;
51   @Input() editIntentTableData;
52   @Output() modalOpreation = new EventEmitter();
53
54   ngOnInit() {}
55   ngOnChanges() {
56     if (this.showModel) {
57       if (JSON.stringify(this.editIntentTableData)!=='{}') {
58         this.defaultParams=this.editIntentTableData
59         this.listOfData=this.editIntentTableData['intentExpectations']
60       }
61     }
62         }
63
64   handleCancel(): void {
65     this.showModel = false;
66     this.clearIntentData()
67     this.modalOpreation.emit({ "cancel": true });
68   }
69   handleOk(): void {
70     if(JSON.stringify(this.editIntentTableData)==='{}'){
71       this.defaultParams['intentId']=this.Util.getUuid()
72       this.createIntentInstance()
73     }else{
74       this.editIntentInstanceData()
75     }
76     this.clearIntentData()
77   }
78   clearIntentData(): void{
79     this.defaultParams = {
80       intentId:'',
81       intentName:'',
82       intentExpectations:[
83         {
84           expectationId:'',
85           expectationName:'',
86           expectationType:'',
87           expectationObject:{
88             objectType:'',
89             objectInstance:''
90           },
91           expectationTargets:[
92             {
93               targetId:'',
94               targetName:'',
95               targetCondition:{
96                 conditionId:'',
97                 conditionName:'',
98                 operator:'',
99                 conditionValue:'',
100                 conditionList:null
101               }
102             }
103           ]
104         }
105       ]
106     };
107     this.listOfData=[]
108   }
109
110   inputIntentExpectationShow(): void {
111     this.intentExpectationShow = true;
112   }
113   inputIntentExpectationClose($event: any): void {
114     this.intentExpectationShow = false;
115     this.editExpectationTableList={}
116     if ($event.cancel) {
117         return;
118     }
119     if(this.currentIndex>-1){
120       this.listOfData[this.currentIndex]=$event.param
121       this.currentIndex=-1
122     }else{
123       this.listOfData.push($event.param)
124     }
125     this.defaultParams['intentExpectations']=this.listOfData
126   }
127   editExpectationList(data,i): void {
128     this.editExpectationTableList=JSON.parse(JSON.stringify(data))
129     this.currentIndex=i
130     this.intentExpectationShow = true
131   }
132   deleteExpectationList(i): void{
133     this.listOfData.splice(i,1)
134   }
135
136   createIntentInstance(): void {
137     this.myhttp.createIntentManagement({
138       ...this.defaultParams
139     }).subscribe(
140       (response) => {
141         this.showModel = false;
142         this.modalOpreation.emit({ "cancel": false });
143       },
144       (err) => {
145         console.log(err);
146       }
147     )
148   }
149   editIntentInstanceData(): void {
150     let id = this.defaultParams['intentId'];
151     let obj = this.defaultParams;
152     this.myhttp.updateIntentManagementData(id,obj).subscribe(
153       (response) => {
154         this.showModel = false;
155         this.modalOpreation.emit({ "cancel": false});
156       },
157       (err) => {
158         console.log(err);
159       }
160     )
161   }
162 }