import "./style.scss"; import { createElement, createElementInit } from "../../functions"; import { r } from "../../utility/lgres"; import { isEmail, isPhone } from "../../utility"; import { setTooltip } from "../../ui/tooltip"; import { createIcon } from "../../ui/icon"; import { createCheckbox } from "../../ui/checkbox"; import { createBox } from "./lib"; 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; element != null && (element.disabled = flag === false); } get autoUpdates() { return this.#autoUpdates?.checked } set autoUpdates(flag) { const element = this.#autoUpdates; element != null && (element.checked = flag); } get #statusLink() { return this.#container.querySelector('.check-status-link') } get statusLinkEnabled() { return this.#statusLink?.disabled !== true } set statusLinkEnabled(flag) { const element = this.#statusLink; element != null && (element.disabled = flag === false); } get statusLink() { return this.#statusLink?.checked } set statusLink(flag) { const element = this.#statusLink; element != null && (element.checked = flag); } get text() { return this.#enter?.value } set text(s) { const element = this.#enter; element != null && (element.value = s); } get contacts() { return [...this.#contacts.children].map(el => { const span = el.querySelector('span'); return { 'Key': span.innerText, 'Value': span.dataset.name }; }); } set contacts(contacts) { this.#contacts.replaceChildren(); if (contacts?.length > 0) { for (let c of contacts) { if (c.OptOut || c.OptOut_BC) { continue; } const mp = String(c.MobilePhone).trim(); const email = String(c.Email).trim(); if (c.ContactPreference === '0' && !isPhone(mp) || c.ContactPreference === '1' && !isEmail(email)) { continue; } const to = c.ContactPreference === '0' ? mp : email; this.#contacts.appendChild( createElement('div', 'contact-item', createIcon('fa-light', c.ContactPreference === '0' ? 'comment-lines' : 'envelope'), createElementInit('span', span => { span.dataset.name = c.Name; span.innerText = to; }) ) ) } } } create() { // functions const checkAutoUpdate = createCheckbox({ className: 'check-auto-update', checked: this.#option.autoUpdates?.checked, enabled: this.#option.autoUpdates?.enabled, checkedNode: createIcon('fa-regular', 'redo-alt'), uncheckedNode: createIcon('fa-regular', 'ban'), onchange: () => { setTooltip(checkAutoUpdate, this.checked ? r('autoUpdateEnabled', 'Auto Updates Enabled') : r('autoUpdateDisabled', 'Auto Updates Disabled')); if (typeof this.#option.autoUpdates?.onchanged === 'function') { this.#option.autoUpdates.onchanged(this.checked); } } }); const checkLink = createCheckbox({ className: 'check-status-link', checked: this.#option.statusLink?.checked, enabled: this.#option.statusLink?.enabled, checkedNode: createIcon('fa-regular', 'link'), uncheckedNode: createIcon('fa-regular', 'unlink'), onchange: () => { setTooltip(checkLink, this.checked ? r('statusLinkIncluded', 'Status Link Included') : r('statusLinkExcluded', 'Status Link Excluded')); if (typeof this.#option.statusLink?.onchanged === 'function') { this.#option.statusLink.onchanged(this.checked); } } }); const container = createBox( createElement('div', null, createElementInit('div', div => div.innerText = r('messages', 'Customer Communication')), createElementInit('div', div => div.innerText = consts.user?.companyName)), [ setTooltip(checkAutoUpdate, r('autoUpdateDisabled', 'Auto Updates Disabled')), setTooltip(checkLink, r('statusLinkExcluded', 'Status Link Excluded')) ] ); // contacts const contacts = createElement('div'); container.append( createElement('div', 'contact-bar', createIcon('fa-solid', 'user-circle', { 'fill': 'lightgray', 'flex': '0 0 auto' }), createElementInit('div', div => div.style.flex = '1 1 auto', contacts, createElementInit('button', button => { button.className = 'roundbtn'; button.style.backgroundColor = 'rgb(1, 199, 172)'; button.appendChild(createIcon('fa-solid', 'user-edit')); setTooltip(button, r('editContacts', 'Edit Contacts')); button.addEventListener('click', () => { // TODO: }); }) ) ) ); this.#contacts = contacts; // followers const followers = createElement('div'); container.append( createElement('div', 'contact-bar follower-bar', createIcon('fa-solid', 'user-tag', { 'fill': '#fff', 'background-color': 'lightgray', 'box-sizing': 'border-box', 'border-radius': '15px', 'padding': '4px', 'flex': '0 0 auto' }), createElementInit('div', div => div.style.flex = '1 1 auto', followers, createElementInit('button', button => { button.className = 'roundbtn'; button.style.backgroundColor = 'rgb(48, 107, 255)'; 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('typeComment', 'Enter Message Here'); enter.maxLength = 3000; this.#enter = enter; container.appendChild( createElement('div', 'message-bar', enter, createElementInit('div', div => div.style.textAlign = 'right', createElementInit('button', button => { button.className = 'roundbtn'; button.style.backgroundColor = 'rgb(19, 150, 204)'; button.appendChild(createIcon('fa-solid', 'paper-plane')); setTooltip(button, r('sendMessage', 'Send Message')); button.addEventListener('click', () => { // TODO: Add text }) }) ) ) ); const message = createElement('div'); this.#message = message; container.appendChild(message); return this.#container = container; } load(data, contacts) { const children = []; if (data?.length > 0) { for (let comm of data) { const div = document.createElement('div', 'txtdiv'); let name; if (comm.IsReply) { const email = isEmail(comm.Sender); const c = contacts.find(c => email ? c.Email === comm.Sender : c.MobilePhone === comm.Sender); if (c != null) { name = c.Name; } } } children[0].style.marginTop = '0'; this.#message.append(...children); } } } export default CustomerCommunication;