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(); // 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; if (!person) { alert('Could not find person data.'); return; } // Populate the form fields modal.find('#editPersonId').val(person.id); modal.find('#editFirstName').val(person.firstName); modal.find('#editLastName').val(person.lastName); 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.companyName); 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 = $('').attr('label', groupName); groupedFunctions[groupName].forEach(function(func) { var option = $('').val(func.id).text(func.name); if (person_functions && person_functions.includes(String(func.id))) { option.prop('selected', true); } optgroup.append(option); }); rolesSelect.append(optgroup); } } // Trigger change to show/hide conditional fields modal.find('#editRole').trigger('change'); // Also set up the delete button $('#deleteUserBtn').data('person-id', person.id); $('#personNameToDelete').text(person.firstName + ' ' + person.lastName); }, error: function(xhr, status, error) { alert('An error occurred while fetching person data. Please try again.'); console.error("AJAX Error:", status, error); } }); } }); // Show/hide group selection based on role for both Edit and Create modals $(document).on('change', '#editRole, #createRole', function() { const role = $(this).val(); const isMember = role === 'member'; // Find the correct context (modal) for the elements const modal = $(this).closest('.modal-content'); modal.find('.member-only-fields').toggle(isMember); modal.find('#edit-group-selection-div, #create-group-selection-div').toggle(isMember); }); // Handle Delete Person confirmation $('#confirmDeleteBtn').on('click', function() { var personId = $('#deleteUserBtn').data('person-id'); if (personId) { // Use a form submission to perform the delete var form = $('
'); form.attr("method", "post"); form.attr("action", "_delete_person.php"); var field = $(''); field.attr("type", "hidden"); field.attr("name", "id"); field.attr("value", personId); form.append(field); $(document.body).append(form); form.submit(); } }); // Set initial state for create form $('#createPersonModal').on('show.bs.modal', function () { $('#createRole').trigger('change'); }); });