Li Jun
2022-04-06 fb04e7c01d119c60632b4298d18fd93f3ccb3d79
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
<apex:page controller="SearchContactController" id="page">
    <apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}" />
    <apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" />
    <apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}" />
    <apex:includeScript value="{!URLFOR($Resource.PleaseWaitDialog)}" />
    <apex:stylesheet value="{!URLFOR($Resource.blockUIcss)}" />
 
    <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 = '';
            function searchAWSContact(){
                console.log('Search process!');
                //1. reset table;
                blockme();             
                resetTable();
                //2. get contact name value 
                searchContactName = document.getElementById('page:form:lksrch').value;
                //3. check searchContactName.length>=2
                if(searchContactName.length>=2){
                    queryLeadFromAWS();
                }else{
                    alert("请输入至少2个关键字");
                    unblockUI();
                }
 
            }
            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 = [];
                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) => {
                    console.log('JSON Result from aws:'+JSON.stringify(result));
                    if(result.object&&result.object.length>0){
                        initContactTable(result);
                    }else{
                        unblockUI();
                        alert('没有查到该联系人')
                    }               
                })
            }
            
            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;
                window.open('\\'+obj.currentTarget.id,'_blank');
                console.log('Contact Infor:'+JSON.stringify(contactInfo));
            }
 
            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');
                let colsHeader = ['姓名','客户名称','邮箱', '电话','手机号'];
                tableBody.appendChild(headerTR);
                for (let i = 0; i < colsHeader.length; i++) {
                    let td = document.createElement('TH');
                    td.width = '75';
                    td.appendChild(document.createTextNode(colsHeader[i]));
                    headerTR.appendChild(td);
                }
                //3. Init the AWS data
                for (let i = 0; i < contactInfoList.length; i++) {
                    let contactInfoTemp = contactInfoList[i]
                    if(contactsInfo[contactInfoTemp.AWSDataId] != null){
                        let tr = document.createElement('TR');
                        tableBody.appendChild(tr);
                        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]]!=null?contactInfoTemp[cols[j]]:''));
                            if (cols[j] == 'Name') {
                                td.addEventListener("click", function (obj) {
                                    redirectToParentPage(obj);
                                });
                            }
                            tr.appendChild(td);
                        }
                    }
                }
                myTableDiv.appendChild(table);
                unblockUI();
            }
            function initContactTable(data) {            
                let cols = ['Name','AccountName','Email', 'Phone','MobilePhone'];
                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.MobilePhone = data.object[i].mobilePhone;
                        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('Contact Info from AWS:'+JSON.stringify(contactInfoList));
                //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,"\""));
                                console.log('Contact Info from SF:'+JSON.stringify(contactsInfo));
                                if(Object.keys(contactsInfo).length>0){
                                    for(let i=0;i<contactInfoList.length;i++){
                                        let contactFromSF = contactsInfo[contactInfoList[i]['AWSDataId']];
                                        if(contactFromSF){
                                            contactInfoList[i].sfRecordId = contactFromSF['Id'];
                                            contactInfoList[i].AccountName = contactFromSF['Account']['Name'];
                                        }
                                    }
                                }
                               
                                refreshTable(cols,contactInfoList);
                            }else{
                                alert('没查询到该联系人');
                                console.log('No result');
                                refreshTable(cols,[]);
                            }                                               
                        }
                    },
                    { 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>联系人搜索</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">请输入联系人的姓名进行搜索。
                    </div>
                </div>
            </div>
            <div id="QueryResult">
            </div>
        </apex:form>
    </body>
</apex:page>