1048147e0Sopenharmony_ci/**
2048147e0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3048147e0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4048147e0Sopenharmony_ci * you may not use this file except in compliance with the License.
5048147e0Sopenharmony_ci * You may obtain a copy of the License at
6048147e0Sopenharmony_ci *
7048147e0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8048147e0Sopenharmony_ci *
9048147e0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10048147e0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11048147e0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12048147e0Sopenharmony_ci * See the License for the specific language governing permissions and
13048147e0Sopenharmony_ci * limitations under the License.
14048147e0Sopenharmony_ci */
15048147e0Sopenharmony_ciimport prompt from '@system.prompt';
16048147e0Sopenharmony_ci
17048147e0Sopenharmony_ciimport HiLog from '../../utils/HiLog';
18048147e0Sopenharmony_ciimport common from '../../data/commonData';
19048147e0Sopenharmony_ciimport ContactsService from '../../service/ContactsService';
20048147e0Sopenharmony_ciimport commonService from '../../service/CommonService'
21048147e0Sopenharmony_ciimport commonCtrl from '../../pages/conversation/common'
22048147e0Sopenharmony_ciimport LooseObject from '../../data/LooseObject'
23048147e0Sopenharmony_ci
24048147e0Sopenharmony_ciconst TAG = 'ReceiveController';
25048147e0Sopenharmony_ci
26048147e0Sopenharmony_ciexport default class ReceiveController {
27048147e0Sopenharmony_ci    private static sInstance: ReceiveController;
28048147e0Sopenharmony_ci    commonCtrl = commonCtrl.getInstance();
29048147e0Sopenharmony_ci    refresh: boolean = false;
30048147e0Sopenharmony_ci    // Recipient information (selected)
31048147e0Sopenharmony_ci    selectContacts: Array<any> = [];
32048147e0Sopenharmony_ci    contacts: Array<any> = [];
33048147e0Sopenharmony_ci    // Recipient list information (all)
34048147e0Sopenharmony_ci    contactsTemp: Array<any> = [];
35048147e0Sopenharmony_ci    // Recipient Content
36048147e0Sopenharmony_ci    myText: string = '';
37048147e0Sopenharmony_ci    // true: focus editing status (gray); false: no focus (blue)
38048147e0Sopenharmony_ci    isInputStatus: boolean = true;
39048147e0Sopenharmony_ci    // true Show Search List
40048147e0Sopenharmony_ci    isShowSearch: boolean = true;
41048147e0Sopenharmony_ci    strSelectContact: string = '';
42048147e0Sopenharmony_ci    styleTextarea: string = 'select-contact-textarea';
43048147e0Sopenharmony_ci    hasBlur: boolean = false;
44048147e0Sopenharmony_ci    // List pagination, number of pages
45048147e0Sopenharmony_ci    page: number = 0;
46048147e0Sopenharmony_ci    // List pagination, quantity
47048147e0Sopenharmony_ci    limit: number = 10;
48048147e0Sopenharmony_ci    // Total number of contacts
49048147e0Sopenharmony_ci    totalMessage: number = 0;
50048147e0Sopenharmony_ci
51048147e0Sopenharmony_ci    static getInstance() {
52048147e0Sopenharmony_ci        if (ReceiveController.sInstance == null) {
53048147e0Sopenharmony_ci            ReceiveController.sInstance = new ReceiveController();
54048147e0Sopenharmony_ci        }
55048147e0Sopenharmony_ci        return ReceiveController.sInstance;
56048147e0Sopenharmony_ci    }
57048147e0Sopenharmony_ci
58048147e0Sopenharmony_ci    onInit(call) {
59048147e0Sopenharmony_ci        HiLog.i(TAG, 'onInit()');
60048147e0Sopenharmony_ci        this.selectContacts = this.commonCtrl.paramContact.transmitContacts;
61048147e0Sopenharmony_ci        if (this.selectContacts.length > 0) {
62048147e0Sopenharmony_ci            let that = this;
63048147e0Sopenharmony_ci            setTimeout(function () {
64048147e0Sopenharmony_ci                that.setContactValue(call);
65048147e0Sopenharmony_ci            }, 200);
66048147e0Sopenharmony_ci            this.isShowSearch = false;
67048147e0Sopenharmony_ci            this.setInputStatus(false);
68048147e0Sopenharmony_ci        }
69048147e0Sopenharmony_ci        this.requestItem();
70048147e0Sopenharmony_ci        this.refresh = !this.refresh;
71048147e0Sopenharmony_ci    }
72048147e0Sopenharmony_ci
73048147e0Sopenharmony_ci    onBackPress() {
74048147e0Sopenharmony_ci        this.InputTextClear();
75048147e0Sopenharmony_ci        return true;
76048147e0Sopenharmony_ci    }
77048147e0Sopenharmony_ci
78048147e0Sopenharmony_ci    InputTextClear() {
79048147e0Sopenharmony_ci        this.myText = '';
80048147e0Sopenharmony_ci        this.setInputStatus(true);
81048147e0Sopenharmony_ci        this.commonCtrl.paramContact.transmitContacts = [];
82048147e0Sopenharmony_ci        HiLog.i(TAG,'InputTextClear');
83048147e0Sopenharmony_ci    }
84048147e0Sopenharmony_ci
85048147e0Sopenharmony_ci    requestItem() {
86048147e0Sopenharmony_ci        let count = this.page * this.limit;
87048147e0Sopenharmony_ci        if (this.page === 0) {
88048147e0Sopenharmony_ci            this.page++;
89048147e0Sopenharmony_ci            this.queryContacts();
90048147e0Sopenharmony_ci        } else if (count < this.totalMessage && this.contacts.length > (this.page - 1) * this.limit) {
91048147e0Sopenharmony_ci            // The restriction on Contacts is to prevent multiple refresh requests during initialization.
92048147e0Sopenharmony_ci            this.page++;
93048147e0Sopenharmony_ci            this.queryContacts();
94048147e0Sopenharmony_ci        }
95048147e0Sopenharmony_ci    }
96048147e0Sopenharmony_ci
97048147e0Sopenharmony_ci    queryContacts() {
98048147e0Sopenharmony_ci        let actionData = {
99048147e0Sopenharmony_ci            page: this.page,
100048147e0Sopenharmony_ci            limit: this.limit
101048147e0Sopenharmony_ci        };
102048147e0Sopenharmony_ci        // Querying Contacts
103048147e0Sopenharmony_ci        ContactsService.getInstance().queryContact(actionData, contacts => {
104048147e0Sopenharmony_ci            if (common.int.SUCCESS == contacts.code) {
105048147e0Sopenharmony_ci                let response = this.contacts.concat(contacts.response);
106048147e0Sopenharmony_ci                this.contacts = [];
107048147e0Sopenharmony_ci                this.contacts = response;
108048147e0Sopenharmony_ci                this.contactsTemp = this.contacts.slice(0);
109048147e0Sopenharmony_ci            } else {
110048147e0Sopenharmony_ci                HiLog.w(TAG, 'queryContacts, fail');
111048147e0Sopenharmony_ci            }
112048147e0Sopenharmony_ci        }, null);
113048147e0Sopenharmony_ci        // Number of statistics
114048147e0Sopenharmony_ci        ContactsService.getInstance().queryContactSizeByCondition(actionData, res => {
115048147e0Sopenharmony_ci            if (res.code == common.int.SUCCESS) {
116048147e0Sopenharmony_ci                this.totalMessage = res.abilityResult;
117048147e0Sopenharmony_ci            }
118048147e0Sopenharmony_ci        }, null);
119048147e0Sopenharmony_ci    }
120048147e0Sopenharmony_ci
121048147e0Sopenharmony_ci    searchContacts(textValue, callback) {
122048147e0Sopenharmony_ci        HiLog.i(TAG, 'searchContacts start');
123048147e0Sopenharmony_ci        let actionData = {
124048147e0Sopenharmony_ci            telephone: textValue,
125048147e0Sopenharmony_ci        };
126048147e0Sopenharmony_ci        ContactsService.getInstance().queryContactViewByCondition(actionData, res => {
127048147e0Sopenharmony_ci            if (res.code == common.int.SUCCESS) {
128048147e0Sopenharmony_ci                this.contacts = res.abilityResult;
129048147e0Sopenharmony_ci            } else {
130048147e0Sopenharmony_ci                HiLog.w(TAG, 'searchContacts fail');
131048147e0Sopenharmony_ci            }
132048147e0Sopenharmony_ci            callback(res.code);
133048147e0Sopenharmony_ci        }, null);
134048147e0Sopenharmony_ci    }
135048147e0Sopenharmony_ci
136048147e0Sopenharmony_ci    // Filter search terms to match contacts
137048147e0Sopenharmony_ci    filterContacts(textValue) {
138048147e0Sopenharmony_ci        try {
139048147e0Sopenharmony_ci            this.contacts = this.contactsTemp.filter((contact) => {
140048147e0Sopenharmony_ci                if (contact.contactName && contact.contactName.toLowerCase().search(textValue) != -1) {
141048147e0Sopenharmony_ci                    HiLog.i(TAG, 'filterContacts, contactName');
142048147e0Sopenharmony_ci                    return true;
143048147e0Sopenharmony_ci                } else if (contact.telephone && contact.telephone.toLowerCase().search(textValue) != -1) {
144048147e0Sopenharmony_ci                    HiLog.i(TAG, 'filterContacts, telephone');
145048147e0Sopenharmony_ci                    return true;
146048147e0Sopenharmony_ci                }
147048147e0Sopenharmony_ci                return false;
148048147e0Sopenharmony_ci            });
149048147e0Sopenharmony_ci        } catch (Error) {
150048147e0Sopenharmony_ci            HiLog.i(TAG, `error message: ${JSON.stringify(Error)}`);
151048147e0Sopenharmony_ci        }
152048147e0Sopenharmony_ci    }
153048147e0Sopenharmony_ci
154048147e0Sopenharmony_ci    isPhoneNumber(str) {
155048147e0Sopenharmony_ci        // Determine whether the value is a number.
156048147e0Sopenharmony_ci        let reg = /^\d{1,}$/;
157048147e0Sopenharmony_ci        let pattern = new RegExp(reg);
158048147e0Sopenharmony_ci        return pattern.test(str);
159048147e0Sopenharmony_ci    }
160048147e0Sopenharmony_ci
161048147e0Sopenharmony_ci    setInputStatus(flag) {
162048147e0Sopenharmony_ci        this.isInputStatus = flag;
163048147e0Sopenharmony_ci        if (!flag) {
164048147e0Sopenharmony_ci            this.strSelectContact = this.setShowContactName();
165048147e0Sopenharmony_ci        }
166048147e0Sopenharmony_ci    }
167048147e0Sopenharmony_ci
168048147e0Sopenharmony_ci    checkReceive(call) {
169048147e0Sopenharmony_ci        HiLog.i(TAG, 'checkReceive, isInputStatus: ' + this.isInputStatus);
170048147e0Sopenharmony_ci        if (this.myText.trim() == common.string.EMPTY_STR) {
171048147e0Sopenharmony_ci            this.strSelectContact = this.setShowContactName();
172048147e0Sopenharmony_ci            this.isShowSearch = false;
173048147e0Sopenharmony_ci            return;
174048147e0Sopenharmony_ci        }
175048147e0Sopenharmony_ci        this.hasBlur = true;
176048147e0Sopenharmony_ci        if (this.isPhoneNumber(this.myText)) {
177048147e0Sopenharmony_ci            // Get information from the contact list
178048147e0Sopenharmony_ci            let that = this;
179048147e0Sopenharmony_ci            let selectContact: LooseObject = {};
180048147e0Sopenharmony_ci            let hasSelect = false;
181048147e0Sopenharmony_ci            for (let index in this.contacts) {
182048147e0Sopenharmony_ci                let contact = this.contacts[index];
183048147e0Sopenharmony_ci                if (contact.telephone == that.myText) {
184048147e0Sopenharmony_ci                    selectContact.headImage = 'icon/user_avatar_full_fill.svg';
185048147e0Sopenharmony_ci                    selectContact.contactName = contact.contactName;
186048147e0Sopenharmony_ci                    selectContact.telephone = contact.telephone;
187048147e0Sopenharmony_ci                    selectContact.telephoneFormat = contact.telephone;
188048147e0Sopenharmony_ci                    selectContact.select = false;
189048147e0Sopenharmony_ci                    hasSelect = true;
190048147e0Sopenharmony_ci                    break;
191048147e0Sopenharmony_ci                }
192048147e0Sopenharmony_ci            }
193048147e0Sopenharmony_ci            if (!hasSelect) {
194048147e0Sopenharmony_ci                selectContact.headImage = common.string.EMPTY_STR;
195048147e0Sopenharmony_ci                selectContact.contactName = common.string.EMPTY_STR;
196048147e0Sopenharmony_ci                selectContact.telephone = that.myText;
197048147e0Sopenharmony_ci                selectContact.telephoneFormat = that.myText;
198048147e0Sopenharmony_ci                selectContact.select = false;
199048147e0Sopenharmony_ci            }
200048147e0Sopenharmony_ci            HiLog.i(TAG, 'checkReceive, isPhoneNumber yes');
201048147e0Sopenharmony_ci            this.selectContacts.push(selectContact);
202048147e0Sopenharmony_ci            this.refresh = !this.refresh;
203048147e0Sopenharmony_ci            this.setInputStatus(false);
204048147e0Sopenharmony_ci            this.isShowSearch = false;
205048147e0Sopenharmony_ci            this.setContactValue(call);
206048147e0Sopenharmony_ci        } else {
207048147e0Sopenharmony_ci            HiLog.i(TAG, 'checkReceive, isPhoneNumber no');
208048147e0Sopenharmony_ci            prompt.showToast({
209048147e0Sopenharmony_ci                // Invalid Recipient
210048147e0Sopenharmony_ci                // @ts-ignore
211048147e0Sopenharmony_ci                message: $r('app.string.invalid_receive', this.myText),
212048147e0Sopenharmony_ci                duration: 1000,
213048147e0Sopenharmony_ci            });
214048147e0Sopenharmony_ci            this.myText = '';
215048147e0Sopenharmony_ci            this.isShowSearch = true;
216048147e0Sopenharmony_ci            this.setContactValue(call);
217048147e0Sopenharmony_ci        }
218048147e0Sopenharmony_ci    }
219048147e0Sopenharmony_ci
220048147e0Sopenharmony_ci    searchChange(text, call) {
221048147e0Sopenharmony_ci        HiLog.d(TAG, 'searchChange, start');
222048147e0Sopenharmony_ci        if (this.checkSingle()) {
223048147e0Sopenharmony_ci            this.setInputStatus(false);
224048147e0Sopenharmony_ci            this.isShowSearch = false;
225048147e0Sopenharmony_ci            return;
226048147e0Sopenharmony_ci        }
227048147e0Sopenharmony_ci        this.myText = text;
228048147e0Sopenharmony_ci        if (!this.isInputStatus) {
229048147e0Sopenharmony_ci            HiLog.w(TAG, 'searchChange, isInputStatus false');
230048147e0Sopenharmony_ci            return;
231048147e0Sopenharmony_ci        }
232048147e0Sopenharmony_ci        this.searchContacts(this.myText, code => {
233048147e0Sopenharmony_ci            if (code == common.int.SUCCESS && this.myText.trim() != '') {
234048147e0Sopenharmony_ci                this.setContactValue(call);
235048147e0Sopenharmony_ci                this.dealSearchData();
236048147e0Sopenharmony_ci                this.setContactValue(call);
237048147e0Sopenharmony_ci            } else {
238048147e0Sopenharmony_ci                this.setContactValue(call);
239048147e0Sopenharmony_ci            }
240048147e0Sopenharmony_ci        });
241048147e0Sopenharmony_ci    }
242048147e0Sopenharmony_ci
243048147e0Sopenharmony_ci    dealSearchData() {
244048147e0Sopenharmony_ci        if (this.myText.trim() == '') {
245048147e0Sopenharmony_ci            this.contacts = this.contactsTemp.slice(0);
246048147e0Sopenharmony_ci        } else {
247048147e0Sopenharmony_ci            let textValue = this.myText.trim().toLowerCase();
248048147e0Sopenharmony_ci            // Filtering logic
249048147e0Sopenharmony_ci            this.filterContacts(textValue);
250048147e0Sopenharmony_ci        }
251048147e0Sopenharmony_ci    }
252048147e0Sopenharmony_ci
253048147e0Sopenharmony_ci    setContactValue(call) {
254048147e0Sopenharmony_ci        // Send recipient information to the invoked parent component.
255048147e0Sopenharmony_ci        call({
256048147e0Sopenharmony_ci            // Select the content of the text box before the contact.
257048147e0Sopenharmony_ci            contactValue: this.myText,
258048147e0Sopenharmony_ci            // Selected recipient information
259048147e0Sopenharmony_ci            selectContacts: this.selectContacts,
260048147e0Sopenharmony_ci            // Whether the focus is lost
261048147e0Sopenharmony_ci            hasBlur: this.hasBlur
262048147e0Sopenharmony_ci        });
263048147e0Sopenharmony_ci    }
264048147e0Sopenharmony_ci
265048147e0Sopenharmony_ci    addContact(index, call) {
266048147e0Sopenharmony_ci        let curItem = this.contacts[index];
267048147e0Sopenharmony_ci        if (this.checkSingle()) {
268048147e0Sopenharmony_ci            return;
269048147e0Sopenharmony_ci        }
270048147e0Sopenharmony_ci        if (curItem == undefined) {
271048147e0Sopenharmony_ci            HiLog.e(TAG, 'addContact contact is null');
272048147e0Sopenharmony_ci            return;
273048147e0Sopenharmony_ci        }
274048147e0Sopenharmony_ci        if (curItem != undefined && curItem.telephone != undefined && curItem.telephone.toString().trim() == '') {
275048147e0Sopenharmony_ci            prompt.showToast({
276048147e0Sopenharmony_ci                // Invalid Recipient
277048147e0Sopenharmony_ci                // @ts-ignore
278048147e0Sopenharmony_ci                message: $r('app.string.invalid_receive', this.myText),
279048147e0Sopenharmony_ci                duration: 1000,
280048147e0Sopenharmony_ci            });
281048147e0Sopenharmony_ci            return;
282048147e0Sopenharmony_ci        }
283048147e0Sopenharmony_ci        this.selectContacts.push(curItem);
284048147e0Sopenharmony_ci        this.contactsTemp = this.contactsTemp.filter((item) => {
285048147e0Sopenharmony_ci            if (item.telephone == undefined || curItem.telephone == undefined) {
286048147e0Sopenharmony_ci                return;
287048147e0Sopenharmony_ci            }
288048147e0Sopenharmony_ci            HiLog.w(TAG, 'addContact');
289048147e0Sopenharmony_ci            return item.telephone != curItem.telephone
290048147e0Sopenharmony_ci        });
291048147e0Sopenharmony_ci        this.contacts.splice(index, 1);
292048147e0Sopenharmony_ci        HiLog.i(TAG, 'addContact, length: ' + this.selectContacts.length);
293048147e0Sopenharmony_ci        this.myText = '';
294048147e0Sopenharmony_ci        if (this.selectContacts.length == 1) {
295048147e0Sopenharmony_ci            this.setInputStatus(false);
296048147e0Sopenharmony_ci            this.isShowSearch = false;
297048147e0Sopenharmony_ci            this.setContactValue(call);
298048147e0Sopenharmony_ci        } else {
299048147e0Sopenharmony_ci            this.setInputStatus(true);
300048147e0Sopenharmony_ci            this.isShowSearch = true;
301048147e0Sopenharmony_ci            this.setContactValue(call);
302048147e0Sopenharmony_ci        }
303048147e0Sopenharmony_ci        HiLog.i(TAG, 'addContact, isInputStatus: ' + this.isInputStatus);
304048147e0Sopenharmony_ci    }
305048147e0Sopenharmony_ci
306048147e0Sopenharmony_ci    setShowContactName() {
307048147e0Sopenharmony_ci        if (this.selectContacts.length == 0) {
308048147e0Sopenharmony_ci            return '';
309048147e0Sopenharmony_ci        }
310048147e0Sopenharmony_ci        let myName = this.selectContacts[0]?.contactName?.trim();
311048147e0Sopenharmony_ci        if (!myName) {
312048147e0Sopenharmony_ci            myName = ''
313048147e0Sopenharmony_ci        }
314048147e0Sopenharmony_ci        let telephone = this.selectContacts[0]?.telephone
315048147e0Sopenharmony_ci        if (telephone === undefined || telephone === null) {
316048147e0Sopenharmony_ci            HiLog.e(TAG, 'setShowContactName fail');
317048147e0Sopenharmony_ci            return '';
318048147e0Sopenharmony_ci        }
319048147e0Sopenharmony_ci        if (!this.isPhoneNumber(telephone)) {
320048147e0Sopenharmony_ci            myName = telephone.replace(new RegExp(/e|-|#|\*|\./, 'g'), common.string.EMPTY_STR);
321048147e0Sopenharmony_ci        } else {
322048147e0Sopenharmony_ci            if (myName == '') {
323048147e0Sopenharmony_ci                myName = telephone;
324048147e0Sopenharmony_ci            }
325048147e0Sopenharmony_ci        }
326048147e0Sopenharmony_ci        if (this.selectContacts.length >= 2) {
327048147e0Sopenharmony_ci            // name and other numbers
328048147e0Sopenharmony_ci            return $r('app.string.and_others', myName, this.selectContacts.length - 1)
329048147e0Sopenharmony_ci        } else {
330048147e0Sopenharmony_ci            return myName
331048147e0Sopenharmony_ci        }
332048147e0Sopenharmony_ci    }
333048147e0Sopenharmony_ci
334048147e0Sopenharmony_ci    myContactFocus() {
335048147e0Sopenharmony_ci        HiLog.i(TAG, 'myContactFocus, start');
336048147e0Sopenharmony_ci        this.myText = common.string.EMPTY_STR;
337048147e0Sopenharmony_ci        this.setInputStatus(true);
338048147e0Sopenharmony_ci        this.isShowSearch = true;
339048147e0Sopenharmony_ci    }
340048147e0Sopenharmony_ci
341048147e0Sopenharmony_ci    myContactClick() {
342048147e0Sopenharmony_ci        HiLog.i(TAG, 'myContactClick, start');
343048147e0Sopenharmony_ci        if (!this.isInputStatus) {
344048147e0Sopenharmony_ci            this.myText = common.string.EMPTY_STR;
345048147e0Sopenharmony_ci            this.setInputStatus(true);
346048147e0Sopenharmony_ci            this.isShowSearch = true;
347048147e0Sopenharmony_ci            // The TextArea control does not support focus.
348048147e0Sopenharmony_ci            //      this.$element('receiveTxt').focus({
349048147e0Sopenharmony_ci            //        focus: true
350048147e0Sopenharmony_ci            //      });
351048147e0Sopenharmony_ci        }
352048147e0Sopenharmony_ci    }
353048147e0Sopenharmony_ci
354048147e0Sopenharmony_ci    nameClick(idx, call) {
355048147e0Sopenharmony_ci        HiLog.i(TAG, 'click-->' + idx);
356048147e0Sopenharmony_ci        if (this.selectContacts[idx] == null || this.selectContacts[idx] == undefined) {
357048147e0Sopenharmony_ci            return;
358048147e0Sopenharmony_ci        }
359048147e0Sopenharmony_ci        if (this.selectContacts[idx].select && this.selectContacts[idx].select != undefined) {
360048147e0Sopenharmony_ci            let item = this.selectContacts.splice(idx, 1);
361048147e0Sopenharmony_ci            // Deleted items are added to the collection to be searched.
362048147e0Sopenharmony_ci            this.contactsTemp.push(item);
363048147e0Sopenharmony_ci            if (item[0] != undefined && item[0].telephoneFormat != undefined &&
364048147e0Sopenharmony_ci            item[0].telephoneFormat.toString().trim() != '') {
365048147e0Sopenharmony_ci                this.contacts.push(item[0]);
366048147e0Sopenharmony_ci            }
367048147e0Sopenharmony_ci            this.refresh = !this.refresh;
368048147e0Sopenharmony_ci            this.setContactValue(call);
369048147e0Sopenharmony_ci            return;
370048147e0Sopenharmony_ci        }
371048147e0Sopenharmony_ci        for (let element of this.selectContacts) {
372048147e0Sopenharmony_ci            element.select = false;
373048147e0Sopenharmony_ci        }
374048147e0Sopenharmony_ci        this.selectContacts[idx].select = true;
375048147e0Sopenharmony_ci        this.refresh = !this.refresh;
376048147e0Sopenharmony_ci    }
377048147e0Sopenharmony_ci
378048147e0Sopenharmony_ci    clickToContacts(call) {
379048147e0Sopenharmony_ci        var actionData: LooseObject = {};
380048147e0Sopenharmony_ci        actionData.pageFlag = common.contactPage.PAGE_FLAG_SINGLE_CHOOSE;
381048147e0Sopenharmony_ci        this.jumpToContactForResult(actionData, call);
382048147e0Sopenharmony_ci    }
383048147e0Sopenharmony_ci    // Tap a contact's avatar to go to the contact details page.
384048147e0Sopenharmony_ci    titleBarAvatar(index) {
385048147e0Sopenharmony_ci        var actionData: LooseObject = {};
386048147e0Sopenharmony_ci        actionData.phoneNumber = this.contacts[index]?.telephone;
387048147e0Sopenharmony_ci        actionData.pageFlag = common.contactPage.PAGE_FLAG_CONTACT_DETAILS;
388048147e0Sopenharmony_ci        this.jumpToContact(actionData);
389048147e0Sopenharmony_ci    }
390048147e0Sopenharmony_ci    // Switching to the Contacts app
391048147e0Sopenharmony_ci    jumpToContact(actionData) {
392048147e0Sopenharmony_ci        let str = commonService.commonContactParam(actionData);
393048147e0Sopenharmony_ci        globalThis.mmsContext.startAbility(str).then((data) => {
394048147e0Sopenharmony_ci        }).catch((error) => {
395048147e0Sopenharmony_ci            HiLog.i(TAG, 'jumpToContact failed');
396048147e0Sopenharmony_ci        });
397048147e0Sopenharmony_ci    }
398048147e0Sopenharmony_ci    // Switching to the Contacts app
399048147e0Sopenharmony_ci    async jumpToContactForResult(actionData, call) {
400048147e0Sopenharmony_ci        let str = commonService.commonContactParam(actionData);
401048147e0Sopenharmony_ci        var data = await globalThis.mmsContext.startAbilityForResult(str);
402048147e0Sopenharmony_ci        if (data.resultCode == 0) {
403048147e0Sopenharmony_ci            this.dealContactParams(data.want.parameters.contactObjects, call);
404048147e0Sopenharmony_ci        }
405048147e0Sopenharmony_ci    }
406048147e0Sopenharmony_ci
407048147e0Sopenharmony_ci    dealContactParams(contactObjects, call) {
408048147e0Sopenharmony_ci        this.selectContacts = [];
409048147e0Sopenharmony_ci        let params = JSON.parse(contactObjects);
410048147e0Sopenharmony_ci        if (this.checkSingle()) {
411048147e0Sopenharmony_ci            return;
412048147e0Sopenharmony_ci        }
413048147e0Sopenharmony_ci        for (let element of params) {
414048147e0Sopenharmony_ci            let selectContact: LooseObject = {};
415048147e0Sopenharmony_ci            selectContact.headImage = 'icon/user_avatar_full_fill.svg';
416048147e0Sopenharmony_ci            selectContact.contactName = element.contactName;
417048147e0Sopenharmony_ci            selectContact.telephone = element.telephone;
418048147e0Sopenharmony_ci            selectContact.telephoneFormat = element.telephone;
419048147e0Sopenharmony_ci            selectContact.select = false;
420048147e0Sopenharmony_ci            this.selectContacts.push(selectContact);
421048147e0Sopenharmony_ci        }
422048147e0Sopenharmony_ci        if (params.length > 1 && this.checkSingle()) {
423048147e0Sopenharmony_ci            this.selectContacts = [];
424048147e0Sopenharmony_ci            return;
425048147e0Sopenharmony_ci        }
426048147e0Sopenharmony_ci        if (this.selectContacts.length > 0) {
427048147e0Sopenharmony_ci            this.deleteRepetitionContacts(this.contacts, this.selectContacts);
428048147e0Sopenharmony_ci            this.setInputStatus(false);
429048147e0Sopenharmony_ci            this.isShowSearch = false;
430048147e0Sopenharmony_ci            this.setContactValue(call);
431048147e0Sopenharmony_ci        }
432048147e0Sopenharmony_ci        this.commonCtrl.paramContact.isSelectContact = false;
433048147e0Sopenharmony_ci        this.commonCtrl.paramContact.isNewRecallMessagesFlag = false;
434048147e0Sopenharmony_ci    }
435048147e0Sopenharmony_ci
436048147e0Sopenharmony_ci    deleteRepetitionContacts(contacts, selectContacts) {
437048147e0Sopenharmony_ci        let indexs = [];
438048147e0Sopenharmony_ci        let count = 0;
439048147e0Sopenharmony_ci        for (let item of contacts) {
440048147e0Sopenharmony_ci            let telephone = item.telephone;
441048147e0Sopenharmony_ci            for (let selectContact of selectContacts) {
442048147e0Sopenharmony_ci                if (telephone == selectContact.telephone) {
443048147e0Sopenharmony_ci                    indexs.push(count);
444048147e0Sopenharmony_ci                    break;
445048147e0Sopenharmony_ci                }
446048147e0Sopenharmony_ci            }
447048147e0Sopenharmony_ci            count++;
448048147e0Sopenharmony_ci        }
449048147e0Sopenharmony_ci        let selectContactIndexs = [];
450048147e0Sopenharmony_ci        for (let i = 0; i < selectContacts.length; i++) {
451048147e0Sopenharmony_ci            let telephone = selectContacts[i].telephone;
452048147e0Sopenharmony_ci            for (let j = i + 1; j < selectContacts.length; j++) {
453048147e0Sopenharmony_ci                if (telephone == selectContacts[j].telephone) {
454048147e0Sopenharmony_ci                    selectContactIndexs.push(i);
455048147e0Sopenharmony_ci                    break;
456048147e0Sopenharmony_ci                }
457048147e0Sopenharmony_ci            }
458048147e0Sopenharmony_ci        }
459048147e0Sopenharmony_ci        if (indexs.length > 0) {
460048147e0Sopenharmony_ci            for (let index of indexs) {
461048147e0Sopenharmony_ci                contacts.splice(index, 1);
462048147e0Sopenharmony_ci            }
463048147e0Sopenharmony_ci        }
464048147e0Sopenharmony_ci        if (selectContactIndexs.length > 0) {
465048147e0Sopenharmony_ci            for (let index of selectContactIndexs) {
466048147e0Sopenharmony_ci                selectContacts.splice(index, 1);
467048147e0Sopenharmony_ci            }
468048147e0Sopenharmony_ci        }
469048147e0Sopenharmony_ci    }
470048147e0Sopenharmony_ci
471048147e0Sopenharmony_ci    // Currently, only one recipient is supported. You can enter only one recipient first.
472048147e0Sopenharmony_ci    checkSingle() {
473048147e0Sopenharmony_ci        if (this.selectContacts.length > 0) {
474048147e0Sopenharmony_ci            prompt.showToast({
475048147e0Sopenharmony_ci                // Invalid Recipient
476048147e0Sopenharmony_ci                // @ts-ignore
477048147e0Sopenharmony_ci                message: '只支持单个收件人',
478048147e0Sopenharmony_ci                duration: 1000,
479048147e0Sopenharmony_ci            });
480048147e0Sopenharmony_ci            return true;
481048147e0Sopenharmony_ci        } else {
482048147e0Sopenharmony_ci            return false;
483048147e0Sopenharmony_ci        }
484048147e0Sopenharmony_ci    }
485048147e0Sopenharmony_ci}