document.addEventListener('DOMContentLoaded', function () {
const sidebar = document.getElementById('sidebar');
const mainContent = document.getElementById('main-content');
const sidebarToggler = document.getElementById('sidebar-toggler');
if (sidebarToggler) {
sidebarToggler.addEventListener('click', function () {
sidebar.classList.toggle('sidebar-collapsed');
mainContent.classList.toggle('main-content-collapsed');
});
}
});
$(document).ready(function() {
// Handler for showing the edit person modal
$('#editPersonModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal
var personId = button.data('person-id'); // Extract info from data-* attributes
var modal = $(this);
// Clear previous data
modal.find('form').trigger('reset');
modal.find('#editPersonId').val('');
modal.find('#editRoles').empty();
modal.find('#followUpSummaryContainer').empty(); // Clear summary container
// Clear file paths
modal.find('#editCompanyLogoPath, #editPersonPhotoPath, #editGainsSheetPath, #editTopWantedPath, #editTopOwnedPath').text('');
if (personId) {
// AJAX request to get person details
$.ajax({
url: '_get_person_details.php',
type: 'GET',
data: { id: personId },
dataType: 'json',
success: function(response) {
if (response.error) {
alert('Error fetching person details: ' + response.error);
return;
}
var person = response.person;
var all_functions = response.all_functions;
var person_functions = response.person_functions;
var followUpSummary = response.follow_up_summary;
if (!person) {
alert('Could not find person data.');
return;
}
// Populate the Follow-up Summary
var summaryContainer = modal.find('#followUpSummaryContainer');
if (followUpSummary) {
let summaryHtml = '
Follow-up Process Summary
';
summaryHtml += '';
if (followUpSummary.last_call_outcome) {
summaryHtml += `- Last Call Outcome
- ${followUpSummary.last_call_outcome.replace(/_/g, ' ')}
`;
}
if (followUpSummary.last_call_date) {
summaryHtml += `- Last Call Date
- ${new Date(followUpSummary.last_call_date).toLocaleString()}
`;
}
if (followUpSummary.next_contact_date) {
summaryHtml += `- Next Contact Date
- ${new Date(followUpSummary.next_contact_date).toLocaleString()}
`;
}
if (followUpSummary.final_outcome) {
summaryHtml += `- Final Status
- ${followUpSummary.final_outcome} (${followUpSummary.reason || 'N/A'})
`;
}
summaryHtml += '
';
summaryContainer.html(summaryHtml);
} else {
summaryContainer.html('No Follow-up process data found for this person.
');
}
// Populate the form fields
modal.find('#editPersonId').val(person.id);
modal.find('#editFirstName').val(person.first_name);
modal.find('#editLastName').val(person.last_name);
modal.find('#editPhone').val(person.phone);
modal.find('#editEmail').val(person.email);
modal.find('#editRole').val(person.role);
modal.find('#editBniGroup').val(person.bni_group_id);
modal.find('#editCompanyName').val(person.company_name);
modal.find('#editNip').val(person.nip);
modal.find('#editIndustry').val(person.industry);
modal.find('#editCompanySize').val(person.company_size_revenue);
modal.find('#editBusinessDescription').val(person.business_description);
// Populate file paths
if (person.company_logo_path) {
modal.find('#editCompanyLogoPath').text('Current file: ' + person.company_logo_path.split('/').pop());
}
if (person.person_photo_path) {
modal.find('#editPersonPhotoPath').text('Current file: ' + person.person_photo_path.split('/').pop());
}
if (person.gains_sheet_path) {
modal.find('#editGainsSheetPath').text('Current file: ' + person.gains_sheet_path.split('/').pop());
}
if (person.top_wanted_contacts_path) {
modal.find('#editTopWantedPath').text('Current file: ' + person.top_wanted_contacts_path.split('/').pop());
}
if (person.top_owned_contacts_path) {
modal.find('#editTopOwnedPath').text('Current file: ' + person.top_owned_contacts_path.split('/').pop());
}
// Populate functions/roles dropdown and select assigned ones
var rolesSelect = modal.find('#editRoles');
rolesSelect.empty(); // Clear existing options
if (all_functions && all_functions.length > 0) {
const groupedFunctions = all_functions.reduce((acc, func) => {
const groupName = func.group_name || 'General';
if (!acc[groupName]) {
acc[groupName] = [];
}
acc[groupName].push(func);
return acc;
}, {});
for (const groupName in groupedFunctions) {
const optgroup = $('