280d21937591b2b1d438359dfb6674dbd2c7dd66
[usecase-ui.git] /
1 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
2 import { TRANSFRER_FORM_ITEMS, CORE_FORM_ITEMS, ADDRESS , NexthopInfo_Options } from '@src/constants/constants'
3
4 @Component({
5         selector: 'app-subnet-params-model',
6         templateUrl: './subnet-params-model.component.html',
7         styleUrls: ['./subnet-params-model.component.less']
8 })
9 export class SubnetParamsModelComponent implements OnInit {
10
11         @Input() showModel: boolean;
12         @Input() detailData: any;
13         @Input() title: string;
14         @Output() cancel = new EventEmitter<boolean>();
15         @Output() paramsDataChange = new EventEmitter<any>();
16
17         transferFormItems = TRANSFRER_FORM_ITEMS;
18         coreFormItems :object ={};
19         areaList: any[] = [];
20     // 2020.08.17  Add 3 parameters for Endpoint, Comment: The following code
21     NexthopInfoOptions = NexthopInfo_Options;
22     EndpointInputs: any[] = [];
23     EndpointEnable: boolean = false;  // Whether to enable the three parameters of Endpoint
24     //  Comment: Above code
25
26         constructor() { }
27
28         ngOnInit() { }
29
30         ngOnChanges() {
31         this.coreFormItems = this.title === 'An'?CORE_FORM_ITEMS.An:CORE_FORM_ITEMS.Cn;
32
33         // -------> 2020.08.17  Add 3 parameters for Endpoint, Comment: The following code
34         if(this.EndpointEnable){
35             this.EndpointInputs = this.title === 'An'
36                 ?this.detailData["an_Endpoint"]
37                 :this.title === 'Cn'
38                     ?this.detailData["cn_Endpoint"]
39                     :[];
40                 }
41         //-------> Comment: Above code
42                 if (this.title === 'An') {
43                         this.AreaFormatting();
44                 }
45         }
46
47         AreaFormatting () {
48                 let areaList = [...this.detailData.an_coverage_area_ta_list];
49                 this.areaList = areaList.map ( (item: any) => {
50                         let arr = item.split(';');
51                         item = arr.map( (ite, index) => {
52                                 let key: string;
53                                 if (!index) {
54                                         key = 'province';
55                                 } else if (index === 1){
56                                         key = 'city'
57                                 } else {
58                                         key = 'district'
59                                 }
60                                 const obj: any = {};
61                                 obj.key = key;
62                                 obj.selected = ite
63                                 obj.options = [{name: ite, id: ite}]
64                                 return obj
65                         })
66                         return item;
67                 })
68         }
69
70         creatAreaList (): void {
71                 let arr = [
72                         {
73                                 key: 'province',
74                                 selected: '',
75                                 options: []
76                         },
77                         {
78                                 key: 'city',
79                                 selected: '',
80                                 options: []
81                         },
82                         {
83                                 key: 'district',
84                                 selected: '',
85                                 options: []
86                         }
87                 ]
88                 this.areaList.push(arr)
89         }
90
91         deleteAreaList (index: number): void {
92                 this.areaList.splice(index,1);
93         }
94
95         handleChange(area: any[], areaItem: any): void{
96                 if (areaItem.key === 'province' && areaItem.options.length <= 1) {
97                         areaItem.options = ADDRESS;
98                 } else if (areaItem.key === 'city' && areaItem.options.length <= 1) {
99                         ADDRESS.forEach( item => {
100                                 if(item.name === area[0].selected) {
101                                         areaItem.options = item.city;
102                                 }
103                         })
104                 }else if (areaItem.key === 'district' && areaItem.options.length <= 1) {
105                         ADDRESS.forEach( (item: any) => {
106                                 item.city.forEach(city => {
107                                         if (city.name === area[1].selected) {
108                                                 areaItem.options = city.county;
109                                         }
110                                 })
111                         })
112                 }
113         }
114
115         handleChangeSelected(area: any[], areaItem: any) {
116                 if (areaItem.key === 'province') {
117                         area[1].selected = ''
118                         area[1].options = [];
119                         area[2].selected = '';
120                         area[2].options = [];
121                 } else if (areaItem.key === 'city') {
122                         area[2].selected = '';
123                         area[2].options = [];
124                 }
125         }
126
127         handleCancel() {
128                 this.showModel = false
129                 this.cancel.emit(this.showModel)
130         }
131
132         handleOk(): void {
133                 let params: object;
134                 if (this.title === 'An') {
135                         const an_coverage_area_ta_list: string[] = [];
136                         this.areaList.forEach( item => {
137                                 let str: string = '';
138                                 item.forEach( area => {
139                                         str += area.selected + ';';
140                                 })
141                                 an_coverage_area_ta_list.push(str.substring(0, str.length-1));
142                         })
143                         params = {...this.detailData, an_coverage_area_ta_list}
144                 } else {
145                         params = {...this.detailData}
146                 }
147                 this.paramsDataChange.emit(params)
148                 this.handleCancel()
149         }
150
151 }