470 lines
19 KiB
JavaScript
470 lines
19 KiB
JavaScript
import "./style.scss";
|
|
import { createElement } from "../../functions";
|
|
import { r } from "../../utility/lgres";
|
|
import { nullOrEmpty } from "../../utility/strings";
|
|
import { formatUrl, isEmail, isPhone } from "../../utility";
|
|
import { setTooltip } from "../../ui/tooltip";
|
|
import { createIcon } from "../../ui/icon";
|
|
import { createCheckbox } from "../../ui/checkbox";
|
|
import { createBox } from "./lib";
|
|
import { createPopup } from "../../ui/popup";
|
|
|
|
class CustomerCommunication {
|
|
#container;
|
|
#option;
|
|
#contacts;
|
|
#followers;
|
|
#enter;
|
|
#message;
|
|
|
|
constructor(opt) {
|
|
this.#option = opt ?? {};
|
|
}
|
|
|
|
get #autoUpdates() { return this.#container.querySelector('.check-auto-update>input') }
|
|
get autoUpdatesEnabled() { return this.#autoUpdates?.disabled !== true }
|
|
set autoUpdatesEnabled(flag) {
|
|
const element = this.#autoUpdates;
|
|
if (element == null) {
|
|
return;
|
|
}
|
|
if (flag === false) {
|
|
element.disabled = true;
|
|
element.parentElement?.classList?.add('disabled');
|
|
} else {
|
|
element.disabled = false;
|
|
element.parentElement?.classList?.remove('disabled');
|
|
}
|
|
}
|
|
get autoUpdates() { return this.#autoUpdates?.checked }
|
|
set autoUpdates(flag) {
|
|
const element = this.#autoUpdates;
|
|
if (element == null) {
|
|
return;
|
|
}
|
|
element.checked = flag;
|
|
element.dispatchEvent(new Event('change'));
|
|
}
|
|
|
|
get #statusLink() { return this.#container.querySelector('.check-status-link') }
|
|
get statusLinkEnabled() { return this.#statusLink?.disabled !== true }
|
|
set statusLinkEnabled(flag) {
|
|
const element = this.#statusLink;
|
|
if (element == null) {
|
|
return;
|
|
}
|
|
if (flag === false) {
|
|
element.disabled = true;
|
|
element.parentElement?.classList?.add('disabled');
|
|
} else {
|
|
element.disabled = false;
|
|
element.parentElement?.classList?.remove('disabled');
|
|
}
|
|
}
|
|
get statusLink() { return this.#statusLink?.checked }
|
|
set statusLink(flag) {
|
|
const element = this.#statusLink;
|
|
if (element == null) {
|
|
return;
|
|
}
|
|
element.checked = flag;
|
|
element.dispatchEvent(new Event('change'));
|
|
}
|
|
|
|
get text() { return this.#enter?.value }
|
|
set text(s) {
|
|
const element = this.#enter;
|
|
if (element != null) {
|
|
element.value = s
|
|
s = String(nullOrEmpty(s) ? 0 : val.length) + '/3000';
|
|
this.#container.querySelector('.message-bar .prompt-count').innerText = s;
|
|
}
|
|
}
|
|
|
|
#createContactItem(c) {
|
|
if (c.OptOut || c.OptOut_BC) {
|
|
return null;
|
|
}
|
|
const mp = String(c.MobilePhone).trim();
|
|
const email = String(c.Email).trim();
|
|
if (c.ContactPreference === '0' && !isPhone(mp) ||
|
|
c.ContactPreference === '1' && !isEmail(email)) {
|
|
return null;
|
|
}
|
|
const to = c.ContactPreference === '0' ? mp : email;
|
|
return createElement('div', 'contact-item',
|
|
createIcon('fa-light', c.ContactPreference === '0' ? 'comment-lines' : 'envelope'),
|
|
setTooltip(createElement('span', span => {
|
|
span.dataset.to = to;
|
|
span.dataset.name = c.Name;
|
|
span.innerText = to;
|
|
}), to, true)
|
|
);
|
|
}
|
|
|
|
get contacts() {
|
|
return [...this.#contacts.children].map(el => {
|
|
const span = el.querySelector('span');
|
|
return { 'Key': span.dataset.to, 'Value': span.dataset.name };
|
|
});
|
|
}
|
|
set contacts(contacts) {
|
|
this.#contacts.replaceChildren();
|
|
if (contacts?.length > 0) {
|
|
for (let c of contacts) {
|
|
const item = this.#createContactItem(c);
|
|
if (item != null) {
|
|
this.#contacts.appendChild(item);
|
|
}
|
|
}
|
|
this.#message.scrollTop = this.#message.scrollHeight
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {boolean} flag
|
|
*/
|
|
set readonly(flag) {
|
|
this.#option.readonly = flag;
|
|
if (this.#container == null) {
|
|
return;
|
|
}
|
|
this.#container.querySelector('.button-edit-contacts').style.display = flag === true ? 'none' : '';
|
|
this.#container.querySelector('.button-edit-followers').style.display = flag === true ? 'none' : '';
|
|
this.#enter.disabled = flag === true;
|
|
}
|
|
|
|
get followers() {
|
|
return [...this.#followers.children].map(el => {
|
|
const span = el.querySelector('span');
|
|
return { 'Key': span.dataset.to, 'Value': span.dataset.name };
|
|
});
|
|
}
|
|
set followers(followers) {
|
|
this.#followers.replaceChildren();
|
|
if (followers?.length > 0) {
|
|
this.#container.querySelector('.follower-bar').style.display = '';
|
|
for (let f of followers) {
|
|
const item = this.#createContactItem(f);
|
|
if (item != null) {
|
|
this.#followers.appendChild(item);
|
|
}
|
|
}
|
|
} else {
|
|
this.#container.querySelector('.follower-bar').style.display = 'none';
|
|
}
|
|
this.#message.scrollTop = this.#message.scrollHeight
|
|
}
|
|
|
|
create() {
|
|
// functions
|
|
const checkAutoUpdate = createCheckbox({
|
|
className: 'check-auto-update',
|
|
checked: this.#option.autoUpdates,
|
|
checkedNode: createIcon('fa-regular', 'redo-alt'),
|
|
uncheckedNode: createIcon('fa-regular', 'ban'),
|
|
onchange: function () {
|
|
setTooltip(checkAutoUpdate, this.checked ?
|
|
r('autoUpdateEnabled', 'Auto Updates Enabled') :
|
|
r('autoUpdateDisabled', 'Auto Updates Disabled'));
|
|
}
|
|
});
|
|
const checkLink = createCheckbox({
|
|
className: 'check-status-link',
|
|
checked: this.#option.statusLink,
|
|
checkedNode: createIcon('fa-regular', 'link'),
|
|
uncheckedNode: createIcon('fa-regular', 'unlink'),
|
|
onchange: function () {
|
|
setTooltip(checkLink, this.checked ?
|
|
r('statusLinkIncluded', 'Status Link Included') :
|
|
r('statusLinkExcluded', 'Status Link Excluded'));
|
|
}
|
|
});
|
|
const container = createBox(
|
|
createElement('div', null,
|
|
createElement('div', div => div.innerText = r('messages', 'Customer Communication')),
|
|
createElement('div', div => {
|
|
div.className = 'title-company';
|
|
if (nullOrEmpty(this.#option.companyName)) {
|
|
div.style.display = 'none';
|
|
} else {
|
|
div.innerText = this.#option.companyName;
|
|
}
|
|
})
|
|
),
|
|
[
|
|
setTooltip(checkAutoUpdate, r('autoUpdateEnabled', 'Auto Updates Enabled')),
|
|
setTooltip(checkLink, r('statusLinkExcluded', 'Status Link Excluded'))
|
|
]
|
|
);
|
|
// contacts
|
|
const readonly = this.#option.readonly;
|
|
const contacts = createElement('div');
|
|
container.append(
|
|
createElement('div', 'contact-bar',
|
|
createElement('div', 'bar-icon',
|
|
createIcon('fa-solid', 'user-circle', {
|
|
'fill': 'lightgray'
|
|
})
|
|
),
|
|
createElement('div', 'bar-list',
|
|
contacts,
|
|
createElement('button', button => {
|
|
button.className = 'roundbtn button-edit-contacts';
|
|
button.style.backgroundColor = 'rgb(1, 199, 172)';
|
|
if (readonly === true) {
|
|
button.style.display = 'none';
|
|
}
|
|
button.appendChild(createIcon('fa-solid', 'user-edit'));
|
|
setTooltip(button, r('editContacts', 'Edit Contacts'));
|
|
button.addEventListener('click', () => {
|
|
// TODO:
|
|
const pop = createPopup(
|
|
createElement('div', div => {
|
|
div.style.display = 'flex';
|
|
div.append(
|
|
createElement('span', span => {
|
|
span.style.flex = '1 1 auto';
|
|
span.innerText = r('editContacts', 'Edit Contacts');
|
|
}),
|
|
createElement('button', button => {
|
|
button.style.flex = '0 0 auto';
|
|
button.className = 'roundbtn button-add-contact';
|
|
button.backgroundColor = 'rgb(1, 199, 172)';
|
|
button.appendChild(createIcon('fa-regular', 'user'));
|
|
setTooltip(button, r('addContact', 'Add Contact'))
|
|
})
|
|
)
|
|
}),
|
|
createElement('div', div => {
|
|
div.style.height = '300px';
|
|
div.innerText = 'Contacts from Customer Record';
|
|
}));
|
|
container.append(pop);
|
|
setTimeout(() => pop.style.opacity = 1, 0);
|
|
});
|
|
})
|
|
)
|
|
)
|
|
);
|
|
this.#contacts = contacts;
|
|
// followers
|
|
const followers = createElement('div');
|
|
container.append(
|
|
createElement('div', div => {
|
|
div.className = 'contact-bar follower-bar';
|
|
div.style.display = 'none';
|
|
},
|
|
setTooltip(createElement('div', 'bar-icon',
|
|
createIcon('fa-solid', 'user-tag', {
|
|
'fill': '#fff',
|
|
'background-color': 'lightgray',
|
|
'box-sizing': 'border-box',
|
|
'border-radius': '15px',
|
|
'padding': '4px'
|
|
})
|
|
), r('copied', 'Copied')),
|
|
createElement('div', 'bar-list',
|
|
followers,
|
|
createElement('button', button => {
|
|
button.className = 'roundbtn button-edit-followers';
|
|
button.style.backgroundColor = 'rgb(48, 107, 255)';
|
|
if (readonly === true) {
|
|
button.style.display = 'none';
|
|
}
|
|
button.appendChild(createIcon('fa-solid', 'pen'));
|
|
setTooltip(button, r('editFollower', 'Edit Followers'));
|
|
button.addEventListener('click', () => {
|
|
// TODO:
|
|
});
|
|
})
|
|
)
|
|
)
|
|
);
|
|
this.#followers = followers;
|
|
// enter box
|
|
const enter = createElement('textarea');
|
|
enter.placeholder = r('typeMessage', 'Enter Message Here');
|
|
enter.maxLength = 3000;
|
|
if (readonly === true) {
|
|
enter.disabled = true;
|
|
}
|
|
enter.addEventListener('input', () => {
|
|
const val = this.#enter.value;
|
|
const s = String(nullOrEmpty(val) ? 0 : val.length) + '/3000';
|
|
this.#container.querySelector('.message-bar .prompt-count').innerText = s;
|
|
});
|
|
this.#enter = enter;
|
|
container.appendChild(
|
|
createElement('div', 'message-bar',
|
|
enter,
|
|
createElement('div', div => div.style.textAlign = 'right',
|
|
createElement('div', 'prompt-count'),
|
|
createElement('button', button => {
|
|
button.className = 'roundbtn button-send-message';
|
|
button.style.backgroundColor = 'rgb(19, 150, 204)';
|
|
button.appendChild(createIcon('fa-solid', 'paper-plane'));
|
|
setTooltip(button, r('sendMessage', 'Send Message'));
|
|
button.addEventListener('click', () => {
|
|
if (typeof this.#option.onAddMessage === 'function') {
|
|
this.#option.onAddMessage(this.#enter.value);
|
|
}
|
|
})
|
|
})
|
|
)
|
|
)
|
|
);
|
|
|
|
const message = createElement('div', 'list-bar');
|
|
this.#message = message;
|
|
container.appendChild(message);
|
|
return this.#container = container;
|
|
}
|
|
|
|
load(data, contacts, followers) {
|
|
const children = [];
|
|
if (data?.length > 0) {
|
|
for (let comm of data) {
|
|
const div = createElement('div', 'item-div');
|
|
let name;
|
|
if (comm.IsReply) {
|
|
const c = isEmail(comm.Sender) ?
|
|
contacts.find(c => c.Email === comm.Sender) :
|
|
contacts.find(c => c.MobilePhone === comm.Sender);
|
|
name = c?.Name;
|
|
}
|
|
name ??= comm.IsReply && String(comm.FormatSender) !== '' ? comm.FormatSender : comm.Sender;
|
|
let sendto = '';
|
|
if (!comm.IsReply && comm.OriPhoneNumbers?.length > 0) {
|
|
for (let oriph of comm.OriPhoneNumbers) {
|
|
let cname;
|
|
const email = isEmail(oriph);
|
|
if (contacts?.length > 0) {
|
|
let c = email ?
|
|
contacts.find(c => c.Email === oriph) :
|
|
contacts.find(c => c.MobilePhone === oriph);
|
|
if (c != null) {
|
|
cname = `${email ? c.Email : c.MobilePhone} - ${c.Name}`;
|
|
} else if (followers?.length > 0) {
|
|
c = email ?
|
|
followers.find(f => f.Email === oriph) :
|
|
followers.find(f => f.MobilePhone === oriph);
|
|
if (c != null) {
|
|
cname = `${email ? c.Email : c.MobilePhone} - ${c.Name}`;
|
|
}
|
|
}
|
|
}
|
|
sendto += (cname ?? oriph) + '\n';
|
|
}
|
|
}
|
|
if (sendto !== '') {
|
|
sendto = r('sendToColon', 'Send To :') + `\n${sendto}`;
|
|
}
|
|
div.appendChild(createElement('div', div => {
|
|
div.className = 'item-poster';
|
|
div.innerText = name;
|
|
if (!comm.IsReply && sendto?.length > 0) {
|
|
setTooltip(div, sendto);
|
|
}
|
|
}));
|
|
const content = createElement('div', 'item-content');
|
|
content.appendChild(createElement('span', span => {
|
|
if (/https?:\/\//i.test(comm.Message)) {
|
|
span.innerHTML = formatUrl(comm.Message);
|
|
} else {
|
|
span.innerText = comm.Message;
|
|
}
|
|
}));
|
|
if (comm.IsReply) {
|
|
div.classList.add('item-other');
|
|
} else {
|
|
div.classList.add('item-self');
|
|
const [status, statusmsg] = this.#getMessageStatus(comm);
|
|
if (status !== -100) {
|
|
let statustext;
|
|
switch (status) {
|
|
case 0:
|
|
statustext = r('pending', 'Pending');
|
|
content.style.backgroundColor = '#ffc107';
|
|
break;
|
|
case 1:
|
|
statustext = r('sent', 'Sent');
|
|
break;
|
|
case 9:
|
|
statustext = r('failed', 'Failed');
|
|
content.style.backgroundColor = '#ffc107';
|
|
break;
|
|
case 10:
|
|
statustext = r('optOut', 'Opt-Out');
|
|
content.style.backgroundColor = '#ffc107';
|
|
break;
|
|
case 412:
|
|
statustext = r('landline', 'Landline');
|
|
content.style.backgroundColor = '#ffc107';
|
|
break;
|
|
default:
|
|
statustext = r('undelivered', 'Undelivered');
|
|
content.style.backgroundColor = '#ffc107';
|
|
break;
|
|
}
|
|
const divstatus = createElement('div', div => {
|
|
div.className = 'item-status';
|
|
div.innerText = statustext;
|
|
if (status == -10) {
|
|
setTooltip(div, statusmsg);
|
|
}
|
|
});
|
|
content.appendChild(divstatus);
|
|
}
|
|
}
|
|
div.append(
|
|
content,
|
|
createElement('div', div => {
|
|
div.className = 'item-time';
|
|
div.innerText = comm.TimeStr;
|
|
})
|
|
);
|
|
children.push(div);
|
|
}
|
|
children[0].style.marginTop = '0';
|
|
}
|
|
this.#message.replaceChildren(...children);
|
|
this.#message.scrollTop = this.#message.scrollHeight
|
|
// setTimeout(() => this.#message.scrollTop = this.#message.scrollHeight, 0);
|
|
}
|
|
|
|
#getMessageStatus(comm) {
|
|
let status = -100; // 没有状态,页面上不显示
|
|
const ls = [];
|
|
let statusmsg = '';
|
|
if (!comm.StatusIncorrect && comm.Participator?.length > 0) {
|
|
for (let p of comm.Participator) {
|
|
if (!isEmail(p.CustomerNumber)) {
|
|
if (ls.indexOf(p.Status) < 0) {
|
|
ls.push(p.Status);
|
|
}
|
|
if (statusmsg.length > 0) {
|
|
statusmsg += '\n';
|
|
}
|
|
statusmsg += `${p.CustomerNumber}: `;
|
|
const st = ({
|
|
0: r('undelivered', 'Undelivered'),
|
|
1: r('sent', 'Sent'),
|
|
9: r('failed', 'Failed')
|
|
})[p.Status];
|
|
if (st != null) {
|
|
statusmsg += st;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (ls.length === 1) {
|
|
status = ls[0];
|
|
} else if (ls.length > 1) {
|
|
status = -10; // 多种状态
|
|
}
|
|
return [status, statusmsg];
|
|
}
|
|
}
|
|
|
|
export default CustomerCommunication; |