/**
 * ITSWEBER CMS – Bulk Actions (v3.3.0)
 * Handles select-all, item counting, and bulk action submission.
 *
 * Usage: Add data-bulk-table to <table>, data-bulk-bar to the action bar.
 * Checkboxes: data-bulk-select-all on header checkbox, data-bulk-item on row checkboxes.
 */
(function () {
    'use strict';

    var table = document.querySelector('[data-bulk-table]');
    var bar   = document.querySelector('[data-bulk-bar]');
    if (!table || !bar) return;

    var selectAll = table.querySelector('[data-bulk-select-all]');
    var countEl   = bar.querySelector('[data-bulk-count]');
    var actionBtn = bar.querySelectorAll('[data-bulk-action]');

    function getItems() {
        return table.querySelectorAll('[data-bulk-item]');
    }

    function getChecked() {
        var items = getItems();
        var checked = [];
        for (var i = 0; i < items.length; i++) {
            if (items[i].checked) checked.push(items[i].value);
        }
        return checked;
    }

    function updateBar() {
        var checked = getChecked();
        var count = checked.length;

        // Show/hide bar
        if (count > 0) {
            bar.removeAttribute('hidden');
            bar.classList.add('is-visible');
        } else {
            bar.setAttribute('hidden', '');
            bar.classList.remove('is-visible');
        }

        // Update count
        if (countEl) {
            countEl.textContent = count;
        }

        // Update select-all state
        var items = getItems();
        if (selectAll && items.length > 0) {
            var allChecked = true;
            for (var i = 0; i < items.length; i++) {
                if (!items[i].checked) { allChecked = false; break; }
            }
            selectAll.checked = allChecked;
            selectAll.indeterminate = count > 0 && !allChecked;
        }

        // Enable/disable action buttons
        for (var j = 0; j < actionBtn.length; j++) {
            actionBtn[j].disabled = count === 0;
        }
    }

    // Select All
    if (selectAll) {
        selectAll.addEventListener('change', function () {
            var items = getItems();
            for (var i = 0; i < items.length; i++) {
                items[i].checked = selectAll.checked;
            }
            updateBar();
        });
    }

    // Individual checkboxes
    table.addEventListener('change', function (e) {
        if (e.target.hasAttribute('data-bulk-item')) {
            updateBar();
        }
    });

    // Action buttons
    for (var k = 0; k < actionBtn.length; k++) {
        (function (btn) {
            btn.addEventListener('click', function (e) {
                e.preventDefault();
                var action  = btn.getAttribute('data-bulk-action');
                var url     = btn.getAttribute('data-bulk-url');
                var confirm_msg = btn.getAttribute('data-bulk-confirm');
                var checked = getChecked();

                if (checked.length === 0) return;

                // Confirmation
                var msg = confirm_msg
                    ? confirm_msg.replace(':count', checked.length)
                    : 'Are you sure? (' + checked.length + ' items)';

                if (!confirm(msg)) return;

                // Build and submit form
                var form = document.createElement('form');
                form.method = 'POST';
                form.action = url;
                form.style.display = 'none';

                // CSRF token
                var csrfMeta = document.querySelector('meta[name="csrf-token"]');
                var csrfInput = document.querySelector('input[name="_csrf_token"]');
                var token = csrfMeta ? csrfMeta.content : (csrfInput ? csrfInput.value : '');

                var tokenField = document.createElement('input');
                tokenField.type = 'hidden';
                tokenField.name = '_csrf_token';
                tokenField.value = token;
                form.appendChild(tokenField);

                // Action field
                var actionField = document.createElement('input');
                actionField.type = 'hidden';
                actionField.name = 'bulk_action';
                actionField.value = action;
                form.appendChild(actionField);

                // IDs
                for (var i = 0; i < checked.length; i++) {
                    var idField = document.createElement('input');
                    idField.type = 'hidden';
                    idField.name = 'ids[]';
                    idField.value = checked[i];
                    form.appendChild(idField);
                }

                document.body.appendChild(form);
                form.submit();
            });
        })(actionBtn[k]);
    }

    // Init: hide bar
    bar.setAttribute('hidden', '');
})();
