buli
2022-03-10 a90c9ecfc5118547d0a92b2fee2779eca95e09a5
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
<apex:page controller="SearchContactController" showHeader="false" id="page">
    <apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" />
 
    <head>
    </head>
    <style>
        /* 1. 定义表单样式 To Do Later*/
 
        table {
            border-collapse: collapse;
            border-spacing: 0;
            width: 98%;
            margin-left: 5px;
        }
    </style>
 
    <body id="body" class="lookupTab">
        <script type="text/javascript">
            var staticResources = JSON.parse('{!staticResource}');
            var contactAWSIds = JSON.parse('{!contactAWSIds}');
            var contactsInfo = JSON.parse('{!contactsInfo}');
            var searchContactName = '';
            queryLeadFromAWS();
            function searchAWSContact(){
                console.log('Search process!');
                //1. reset table;             
                resetTable();
                //2. get contact name value 
                searchContactName = document.getElementById('page:form:lksrch').value;
                queryLeadFromAWS();
 
            }
            function resetTable(){
                let queryResult = document.getElementById('QueryResult');
                let table = document.getElementById('table');
                if(table){
                    queryResult.removeChild(table);
                }               
            }
            function preparePayloadForSearchContact(){
                let searchPayload = new Object();
                searchPayload.dataIds = contactAWSIds;
                searchPayload.contactName = searchContactName;
                return JSON.stringify(searchPayload);
            }
            function queryLeadFromAWS() {
                //1. Prepare the payload for contact search
                let requestSearchPayload = preparePayloadForSearchContact();
                console.log('request payload body:'+requestSearchPayload);
                //2. Invoke AWS Service
                fetch(staticResources.searchUrl, {
                    method: 'POST',
                    body: requestSearchPayload,
                    headers: {
                        'Content-Type': 'application/json',
                        'pi-token': staticResources.token
                    }
                }).then((data) => {
                    return data.json();
                }).then((result) => {
                    if(result.object&&result.object.length>0){
                        initContactTable(result);
                    }                   
                })
            }
            
            function redirectToParentPage(obj) {
                var winMain = window.opener;
                if (null == winMain) {
                    winMain = window.parent.opener;
                }
                let value = obj.currentTarget.innerText;
                let contactInfo = new Object();
                contactInfo.Name = obj.currentTarget.innerText;
                contactInfo.ContactId = obj.currentTarget.id;
                var selectedContactNode = winMain.document.getElementById('{!JSENCODE($CurrentPage.parameters.contactId)}');
                selectedContactNode.value = JSON.stringify(contactInfo);
                closeWindow();
            }
 
            function closeWindow() {
                var winMain = window.opener;
                if (null == winMain) {
                    winMain = window.parent.opener;
                }
                winMain.closePopupWindow();
                window.close();
            }
            function refreshTable(cols,contactInfoList){
                let myTableDiv = document.getElementById("QueryResult");
                let table = document.createElement('TABLE');
                table.border = '1';
                table.id = 'table'
                let tableBody = document.createElement('TBODY');
                table.appendChild(tableBody);
                let headerTR = document.createElement('TR');
                tableBody.appendChild(headerTR);
                for (let i = 0; i < cols.length; i++) {
                    let td = document.createElement('TH');
                    td.width = '75';
                    td.appendChild(document.createTextNode(cols[i]));
                    headerTR.appendChild(td);
                }
                //3. Init the AWS data
                for (let i = 0; i < contactInfoList.length; i++) {
                    let tr = document.createElement('TR');
                    tableBody.appendChild(tr);
                    let contactInfoTemp = contactInfoList[i]
                    for (let j = 0; j < cols.length; j++) {
                        let td = document.createElement('TD');
                        td.width = '75';
                        if(j == 0){
                            td.id = contactsInfo[contactInfoTemp.AWSDataId].Id;
                        }                        
                        td.appendChild(document.createTextNode(contactInfoTemp[cols[j]]));
                        if (cols[j] == 'Name') {
                            td.addEventListener("click", function (obj) {
                                redirectToParentPage(obj);
                            });
                        }
                        tr.appendChild(td);
                    }
                }
                myTableDiv.appendChild(table);
            }
            function initContactTable(data) {            
                let cols = ['Name', 'Email', 'Phone'];
                let contactInfoList = [];
                let awsDataIds = [];
                for(var i=0;i<data.object.length;i++){
                    if(data.object[i].dataId){
                        let contactInfo = new Object();
                        contactInfo.Name = data.object[i].lastName;
                        contactInfo.Email = data.object[i].email;
                        contactInfo.Phone = data.object[i].phone;
                        contactInfo.AWSDataId = data.object[i].dataId;
                        awsDataIds.push(contactInfo.AWSDataId);
                        contactInfo.sfRecordId = '';
                        contactInfoList.push(contactInfo);
                    }                    
                }
                let AWSIdToSFIdMapValue = {};
                console.log(awsDataIds);
                //Invoke SF BackEnd
                Visualforce.remoting.Manager.invokeAction(
                    '{!$RemoteAction.SearchContactController.searchContacts}',
                    JSON.stringify(awsDataIds),
                    function (result, event) {
                        if(event.status){
                            if(result.status = 'success'){
                                contactsInfo = JSON.parse(result.message.replace(/(&quot\;)/g,"\""));
                                refreshTable(cols,contactInfoList);
                            }else{
                                console.log('No result');
                            }                                               
                        }
                    },
                    { escape: true }
                );
            }
        </script>
        <apex:form id="form">
            <!-- Search Filter-->
            <div class="lookup">
                <div class="bPageTitle">
                    <div class="ptBody secondaryPalette">
                        <div class="content">
                            <img src="/img/s.gif" alt="" class="pageTitleIcon" title="" />
                            <h1>Lookup</h1>
                        </div>
                    </div>
                </div>
                <div class="pBody">
                    <label class="assistiveText" for="lksrch">Search</label>
                    <apex:inputText id="lksrch" html-placeholder="{!PIPL_Search_Contact_Label}" value="{!searchKeyWord}" />
                    <input value=" Go! " type="Button"  onclick="searchAWSContact()" styleClass="btn" />
                    <div class="bDescription">You can use "*" as a wildcard next to other characters to improve your search results.
                    </div>
                </div>
            </div>
            <div id="QueryResult">
            </div>
        </apex:form>
    </body>
</apex:page>