|
253 | 253 | <div class="ims-toolbar ims-toolbar-tight"> |
254 | 254 | <form id="bulkInquiryForm" method="post" action="<?= e(base_url('inquiries/bulk')) ?>" class="ims-bulk-form ims-bulk-form-tight"> |
255 | 255 | <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>"> |
| 256 | + |
256 | 257 | <div class="ims-toolbar-field"> |
257 | 258 | <label class="form-label mb-0"> |
258 | 259 | <span>Bulk action</span> |
259 | | - <select name="bulk_action" class="form-input form-input-sm"> |
| 260 | + <select id="bulkActionSelect" name="bulk_action" class="form-input form-input-sm"> |
260 | 261 | <option value="">Choose an action</option> |
261 | 262 | <option value="mark_unread">Mark unread</option> |
262 | 263 | <option value="mark_read">Mark read</option> |
263 | 264 | <option value="mark_spam">Mark spam</option> |
264 | 265 | <option value="move_trash">Move to trash</option> |
265 | | - <option value="assign_owner">Assign owner</option> |
266 | | - <option value="clear_owner">Clear owner</option> |
| 266 | + <option value="assign_selected">Assign owner</option> |
| 267 | + <option value="clear_assignee">Clear owner</option> |
267 | 268 | </select> |
268 | 269 | </label> |
269 | 270 | </div> |
270 | | - <div class="ims-toolbar-field"> |
| 271 | + |
| 272 | + <div id="bulkOwnerField" class="ims-toolbar-field d-none"> |
271 | 273 | <label class="form-label mb-0"> |
272 | 274 | <span>Owner</span> |
273 | | - <select name="assigned_admin_id" class="form-input form-input-sm"> |
| 275 | + <select id="bulkOwnerSelect" name="bulk_assigned_admin_id" class="form-input form-input-sm" disabled> |
274 | 276 | <option value="">Choose owner</option> |
275 | 277 | <?php foreach ($admins as $admin): ?> |
276 | 278 | <option value="<?= (int) $admin['id'] ?>"><?= e($admin['nickname'] ?: $admin['username']) ?></option> |
277 | 279 | <?php endforeach; ?> |
278 | 280 | </select> |
279 | 281 | </label> |
280 | 282 | </div> |
281 | | - <button type="submit" class="btn btn-primary ims-bulk-submit">Run bulk action</button> |
| 283 | + |
| 284 | + <div class="ims-toolbar-field ims-toolbar-meta"> |
| 285 | + <div class="form-label mb-0"> |
| 286 | + <span>Selected on this page</span> |
| 287 | + <div id="bulkSelectionSummary" class="ims-selection-summary">0 rows selected</div> |
| 288 | + </div> |
| 289 | + </div> |
| 290 | + |
| 291 | + <button id="bulkRunButton" type="submit" class="btn btn-primary ims-bulk-submit" disabled>Run bulk action</button> |
282 | 292 | </form> |
283 | 293 | </div> |
284 | 294 |
|
|
373 | 383 | <?php endif; ?> |
374 | 384 | </div> |
375 | 385 | </section> |
| 386 | + |
| 387 | +<script> |
| 388 | +document.addEventListener('DOMContentLoaded', function () { |
| 389 | + const bulkForm = document.getElementById('bulkInquiryForm'); |
| 390 | + if (!bulkForm) return; |
| 391 | + |
| 392 | + const selectAll = document.getElementById('selectAllRows'); |
| 393 | + const rowChecks = Array.from(document.querySelectorAll('.row-check')); |
| 394 | + const bulkAction = document.getElementById('bulkActionSelect'); |
| 395 | + const ownerField = document.getElementById('bulkOwnerField'); |
| 396 | + const ownerSelect = document.getElementById('bulkOwnerSelect'); |
| 397 | + const runButton = document.getElementById('bulkRunButton'); |
| 398 | + const summary = document.getElementById('bulkSelectionSummary'); |
| 399 | + |
| 400 | + const getSelectedCount = () => rowChecks.filter(function (checkbox) { |
| 401 | + return checkbox.checked; |
| 402 | + }).length; |
| 403 | + |
| 404 | + const updateHeaderCheckbox = () => { |
| 405 | + const selectedCount = getSelectedCount(); |
| 406 | + const totalCount = rowChecks.length; |
| 407 | + |
| 408 | + if (!selectAll) return; |
| 409 | + |
| 410 | + if (selectedCount === 0) { |
| 411 | + selectAll.checked = false; |
| 412 | + selectAll.indeterminate = false; |
| 413 | + } else if (selectedCount === totalCount) { |
| 414 | + selectAll.checked = true; |
| 415 | + selectAll.indeterminate = false; |
| 416 | + } else { |
| 417 | + selectAll.checked = false; |
| 418 | + selectAll.indeterminate = true; |
| 419 | + } |
| 420 | + }; |
| 421 | + |
| 422 | + const updateOwnerVisibility = () => { |
| 423 | + const needsOwner = bulkAction && bulkAction.value === 'assign_selected'; |
| 424 | + if (!ownerField || !ownerSelect) return; |
| 425 | + |
| 426 | + ownerField.classList.toggle('d-none', !needsOwner); |
| 427 | + ownerSelect.disabled = !needsOwner; |
| 428 | + |
| 429 | + if (!needsOwner) { |
| 430 | + ownerSelect.value = ''; |
| 431 | + } |
| 432 | + }; |
| 433 | + |
| 434 | + const updateToolbarState = () => { |
| 435 | + const selectedCount = getSelectedCount(); |
| 436 | + const action = bulkAction ? bulkAction.value : ''; |
| 437 | + const needsOwner = action === 'assign_selected'; |
| 438 | + const ownerReady = !needsOwner || (ownerSelect && ownerSelect.value !== ''); |
| 439 | + |
| 440 | + if (summary) { |
| 441 | + summary.textContent = selectedCount + (selectedCount === 1 ? ' row selected' : ' rows selected'); |
| 442 | + } |
| 443 | + |
| 444 | + if (runButton) { |
| 445 | + runButton.disabled = !(selectedCount > 0 && action && ownerReady); |
| 446 | + } |
| 447 | + |
| 448 | + updateHeaderCheckbox(); |
| 449 | + updateOwnerVisibility(); |
| 450 | + }; |
| 451 | + |
| 452 | + if (selectAll) { |
| 453 | + selectAll.addEventListener('change', function () { |
| 454 | + rowChecks.forEach(function (checkbox) { |
| 455 | + checkbox.checked = selectAll.checked; |
| 456 | + }); |
| 457 | + updateToolbarState(); |
| 458 | + }); |
| 459 | + } |
| 460 | + |
| 461 | + rowChecks.forEach(function (checkbox) { |
| 462 | + checkbox.addEventListener('change', updateToolbarState); |
| 463 | + }); |
| 464 | + |
| 465 | + if (bulkAction) { |
| 466 | + bulkAction.addEventListener('change', updateToolbarState); |
| 467 | + } |
| 468 | + |
| 469 | + if (ownerSelect) { |
| 470 | + ownerSelect.addEventListener('change', updateToolbarState); |
| 471 | + } |
| 472 | + |
| 473 | + bulkForm.addEventListener('submit', function (event) { |
| 474 | + const selectedCount = getSelectedCount(); |
| 475 | + const action = bulkAction ? bulkAction.value : ''; |
| 476 | + const needsOwner = action === 'assign_selected'; |
| 477 | + const ownerReady = !needsOwner || (ownerSelect && ownerSelect.value !== ''); |
| 478 | + |
| 479 | + if (!(selectedCount > 0 && action && ownerReady)) { |
| 480 | + event.preventDefault(); |
| 481 | + } |
| 482 | + }); |
| 483 | + |
| 484 | + updateToolbarState(); |
| 485 | +}); |
| 486 | +</script> |
| 487 | + |
376 | 488 | </div> |
0 commit comments