$(document).ready(function() { // Initialize Select2 for user selection $('.select2').select2({ theme: 'bootstrap-5', width: '100%' }); // Handle member removal $(document).on('click', '.btn-remove-member', function() { const memberRow = $(this).closest('.member-row'); memberRow.remove(); }); // Handle adding new member $('#addMemberBtn').on('click', function() { const select = $('#user_id'); const selectedOption = select.find('option:selected'); const selectedValue = select.val(); if (!selectedValue) { return; } const userId = selectedValue; const userName = selectedOption.text(); const userEmail = selectedOption.data('email'); const userAvatar = selectedOption.data('avatar'); // Check if user is already in the list if ($(`.member-row[data-user-id="${userId}"]`).length > 0) { return; } // Create new member row const memberRow = `