freescout2ant/Public/js/crm.js
Claude 68b2258f3a
fix: Show email suggestions as clickable list instead of Awesomplete dropdown
Awesomplete is designed for typed input and doesn't reliably show
suggestions when the input is empty. Replace the approach with a
visible list-group rendered directly in the modal body:

- Add #email-suggestions container in the modal template
- Populate with clickable list items from the email search API
- On click, fill form fields and load contracts (same as Awesomplete select)
- Hide the suggestion list when user starts typing manually
- Keep original Awesomplete search untouched for manual input

https://claude.ai/code/session_01MahaxvQRjJ9bffCy4kaVNQ
2026-04-12 17:57:49 +00:00

288 lines
11 KiB
JavaScript

$(document).ready(function() {
const csrfToken = $('meta[name="csrf-token"]').attr('content');
$('#ameise-modal').on('show.bs.modal', function (e) {
const searchIcon = $(".loading-icon");
const customer_id = $('#customer_id');
const crm_button = $('#crm_button');
const archive_btn = $('#archive_btn');
const input = document.getElementById('crm_user');
const awesomeList = new Awesomplete(input, { });
let dataList = [];
// Hide email suggestions when user starts typing
input.addEventListener('input', function () {
$('#email-suggestions').hide();
searchIcon.show();
const inputValue = input.value;
fetch("/ameise/ajax", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `search=${encodeURIComponent(inputValue)}&action=crm_users_search&_token=${encodeURIComponent(csrfToken)}`,
})
.then(response => response.json())
.then(data => {
if (data.error === 'Redirect') {
window.open(data.url, '_blank');
} else {
dataList = data.crmUsers;
searchIcon.hide();
const suggestions = dataList.map(item => ({
id: item.id,
text: item.text
}));
input.setAttribute('data-list', suggestions.map(item => item.text).join(','));
awesomeList.list = suggestions.map(item => item.text);
awesomeList.evaluate();
}
})
.catch(error => $('#result').html('An error occurred while fetching data.'));
});
function selectCrmUser(selectedId, selectedText) {
let ameise_base_url = $('#ameise_base_url').val();
$('#contract-tag-dropdown, #division-tag-dropdown').empty();
customer_id.val(selectedId);
input.value = selectedText;
crm_button.show().text(selectedText).
attr('href', `${ameise_base_url}maklerportal/?show=kunde&kunde=${selectedId}`);
archive_btn.show();
$('#email-suggestions').hide();
$("#contract-tag-dropdown, #division-tag-dropdown").show();
manageContractSelects();
}
input.addEventListener('awesomplete-selectcomplete', function (e) {
const selectedValue = e.text.value;
const selectedObject = dataList.find(item => item.text === selectedValue);
selectCrmUser(selectedObject.id, selectedValue);
});
// Auto-search by customer email on modal open
const customerEmail = $('#ameise_customer_email').val();
if (customerEmail) {
searchIcon.show();
fetch("/ameise/ajax", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: `email=${encodeURIComponent(customerEmail)}&action=crm_email_search&_token=${encodeURIComponent(csrfToken)}`,
})
.then(response => response.json())
.then(data => {
if (data.error === 'Redirect') {
window.open(data.url, '_blank');
} else if (data.crmUsers && data.crmUsers.length > 0) {
searchIcon.hide();
dataList = data.crmUsers;
const $list = $('#email-suggestions-list');
$list.empty();
data.crmUsers.forEach(function(user) {
const $item = $('<li class="list-group-item"></li>')
.text(user.text)
.attr('data-id', user.id)
.on('click', function() {
selectCrmUser(user.id, user.text);
});
$list.append($item);
});
$('#email-suggestions').show();
} else {
searchIcon.hide();
}
})
.catch(error => {
searchIcon.hide();
});
}
});
$('#ameise-modal').on('hidden.bs.modal', function () {
location.reload();
});
function handleSelectChange() {
let clientId = $('#customer_id').val();
const storedData = localStorage.getItem(`apiData_${clientId}`);
const url = '/ameise/ajax';
if (!storedData) {
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
client_id: clientId,
action: 'get_contract',
_token: csrfToken
}),
success: function(data) {
if (data.error === 'Redirect') {
// Redirect in the current tab
window.open(data.url, '_blank');
} else {
const storageKey = `apiData_${clientId}`;
localStorage.setItem(storageKey, JSON.stringify(data));
$('#contract-tag-dropdown, #division-tag-dropdown').empty();
populateMultiSelectOptions(data);
}
},
error: function(xhr, status, error) {
console.error('AJAX Error:', error);
},
});
}
}
function populateMultiSelectOptions(data) {
const multiSelect = $('#contract-tag-dropdown');
const multiSelect1 = $('#division-tag-dropdown');
for (const key in data.contracts) {
if (data.contracts.hasOwnProperty(key)) {
const group = data.contracts[key];
const groupKey = group[0].key;
const $optgroup = $('<optgroup>', {
label: groupKey
});
group.forEach(option => {
const optionText = `${option.Sparte} - ${option.Versicherungsscheinnummer} - ${option.Risiko}`;
const newOption = new Option(optionText, option.id);
$optgroup.append(newOption);
});
multiSelect.append($optgroup);
}
}
data.divisions.forEach(option => {
const newOption = new Option(option.Text, option.Value);
multiSelect1.append(newOption);
});
multiSelect.select2();
multiSelect1.select2();
}
$(document).on("click", '#archive_btn', function() {
let formData = [];
formData = $('#crm_user_form').serialize();
let crm_user = {
'id': $('#customer_id').val(),
'text': $('#crm_button').text()
}
let conversationId = $(".body-conv").attr("data-conversation_id");
let csrfToken = $('meta[name="csrf-token"]').attr('content');
formData += '&_token=' + encodeURIComponent(csrfToken);
formData += '&conversation_id=' + conversationId;
formData += '&crm_user_data=' + encodeURIComponent(JSON.stringify(crm_user));
formData += '&action=' + 'crm_conversation_archive';
let combinedData = formData;
function processSelectedData(selectedData, paramName) {
if (selectedData) {
let jsonData = selectedData.map(function(option) {
return {
id: option.id,
text: option.text
};
});
let formDataObject = {};
formDataObject[paramName] = JSON.stringify(jsonData);
let jsonQueryString = $.param(formDataObject);
combinedData += (combinedData ? '&' : '') + jsonQueryString;
}
}
processSelectedData($('#contract-tag-dropdown').select2('data'), 'contracts');
processSelectedData($('#division-tag-dropdown').select2('data'), 'divisions_data');
$.ajax({
url: '/ameise/ajax',
type: 'POST',
data: combinedData,
success: function(response) {
console.log(response);
if (response.status) {
location.reload();
} else if(response.error == 'Redirect'){
window.open(response.url, '_blank');
}
},
error: function(error) {}
});
});
function manageContractSelects() {
$('#contract-tag-dropdown').select2({
placeholder: 'Verträge',
width: '350px',
tokenSeparators: [',', ' '],
createTag: function(params) {
return {
id: params.term,
text: params.term,
newTag: true
};
},
});
$('#division-tag-dropdown').select2({
placeholder: 'Sparten',
width: '350px',
tokenSeparators: [',', ' '],
createTag: function(params) {
return {
id: params.term,
text: params.term,
newTag: true
};
},
});
getContracts();
}
function getContracts() {
let clientId = $('#customer_id').val();
window.addEventListener('beforeunload', function() {
localStorage.removeItem(`apiData_${clientId}`);
});
if (clientId.trim() !== '') {
handleSelectChange();
}
}
});
window.addEventListener('DOMContentLoaded', (event) => {
let conversation = document.getElementById('conv-layout-customer');
if (document.getElementById('contracts-list')) {
document.getElementById('contracts-list').remove();
}
let conversationId = document.body.getAttribute('data-conversation_id');
if (conversation) {
fetch('/ameise/'+conversationId+'/get-contracts')
.then(response => response.text())
.then(html => {
// Create a container div to hold the HTML
if (html.trim() !== '') {
let container = document.createElement('div');
container.classList.add('conv-sidebar-block');
container.style.backgroundColor = '#f8f9f9';
container.innerHTML = html;
// Append the container to the "conversation" element
conversation.append(container);
}
})
.catch(error => {
console.log("Something went wrong:", error);
});
}
});