$(document).ready(function() { // Bulk Email Recipient Management // Select All button $('#select-all-btn').click(function() { $('.recipient-checkbox').prop('checked', true); updateRecipientCount(); }); // Deselect All button $('#deselect-all-btn').click(function() { $('.recipient-checkbox').prop('checked', false); updateRecipientCount(); }); // Remove Selected button $('#remove-selected-btn').click(function() { $('.recipient-checkbox:checked').each(function() { $(this).closest('.recipient-item').slideUp(300, function() { $(this).remove(); updateRecipientCount(); }); }); }); // Update count when individual checkboxes are changed $(document).on('change', '.recipient-checkbox', function() { updateRecipientCount(); }); // Update recipient count display function updateRecipientCount() { var checkedCount = $('.recipient-checkbox:checked').length; var totalCount = $('.recipient-checkbox').length; $('#recipient-count').text(totalCount); // Update the next button state if (checkedCount === 0) { $('#next-compose-btn').prop('disabled', true).text('Select Recipients First'); } else { $('#next-compose-btn').prop('disabled', false).text('Next: Compose Email (' + checkedCount + ' selected)'); } } // Initialize count on page load if ($('.recipient-checkbox').length > 0) { updateRecipientCount(); } // Form validation for recipients step $('#recipients-form').submit(function(e) { var checkedCount = $('.recipient-checkbox:checked').length; if (checkedCount === 0) { e.preventDefault(); alert('Please select at least one recipient before proceeding.'); return false; } }); // Dynamic recipient filtering (if needed for future enhancement) $('#filter-recipients-input').on('keyup', function() { var value = $(this).val().toLowerCase(); $('.recipient-item').filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1); }); }); // Preview email functionality $('#preview-email-btn').click(function() { var subject = $('#subject').val(); var body = $('#body').val(); if (subject.trim() === '' || body.trim() === '') { alert('Please enter both subject and body before previewing.'); return; } // Create preview modal (assuming Bootstrap modal) var modalHtml = ''; $('body').append(modalHtml); $('#email-preview-modal').modal('show').on('hidden.bs.modal', function() { $(this).remove(); }); }); // Character counter for email body $('#body').on('input', function() { var length = $(this).val().length; var counter = $('#char-counter'); if (counter.length === 0) { $(this).after('Characters: 0'); counter = $('#char-counter'); } counter.text('Characters: ' + length); }); // Auto-save draft functionality (localStorage) var draftKey = 'bulk-email-draft-' + window.location.pathname; // Load draft on page load if (localStorage.getItem(draftKey)) { var draft = JSON.parse(localStorage.getItem(draftKey)); if (draft.subject) $('#subject').val(draft.subject); if (draft.body) $('#body').val(draft.body); if (draft.subject || draft.body) { $('
').text('Draft restored from previous session.').insertBefore('form'); } } // Save draft as user types $('#subject, #body').on('input', function() { var draft = { subject: $('#subject').val(), body: $('#body').val(), timestamp: new Date().toISOString() }; localStorage.setItem(draftKey, JSON.stringify(draft)); }); // Clear draft on successful send $('form[action*="send_bulk"]').submit(function() { localStorage.removeItem(draftKey); }); });